Sign inSign up

hejiazhen/elasticdump

By hejiazhen

•Updated over 6 years ago

项目地址:https://github.com/taskrabbit/elasticsearch-dump

Image
0

157

hejiazhen/elasticdump repository overview

使用方式:

# 拉取镜像
docker push hejiazhen/elasticdump:6.27.3
# 启动容器
docker run -d --name elasticdump --network host hejiazhen/elasticdump:6.27.3
# 进入容器
docker exec -it elasticdump bash
## 方式一: 直接从集群A同步到集群B
# 同步analyzer
elasticdump --input=http://source_es/your_index_name --output=http://dest_es/your_index_name --type=analyzer
# 同步alias
elasticdump --input=http://source_es/your_index_name --output=http://dest_es/your_index_name --type=alias
# 同步mapping
elasticdump --input=http://source_es/your_index_name --output=http://dest_es/your_index_name --type=mapping
# 同步数据
elasticdump --input=http://source_es/your_index_name --output=http://dest_es/your_index_name --type=data --limit=2000

## 方式二: 备份到本地文件然后再导入
# 导出analyzer
elasticdump --input=http://source_es/your_index_name --output=/path/to/your_index_name-analyzer.json --type=analyzer
# 导出alias
elasticdump --input=http://source_es/your_index_name --output=/path/to/your_index_name-alias.json --type=alias
# 导出mapping
elasticdump --input=http://source_es/your_index_name --output=/path/to/your_index_name-mapping.json --type=mapping
# 导出数据
elasticdump --input=http://source_es/your_index_name --output=/path/to/your_index_name-data.json --type=data --limit=2000
# 在目标集群创建索引
curl -XPUT http://dest_es/your_index_name
# 导入analyzer
elasticdump --input=/path/to/your_index_name-analyzer.json --output=http://dest_es --type=analyzer
# 导入alias
elasticdump --input=/path/to/your_index_name-alias.json --output=http://dest_es --type=alias
# 导入mapping
elasticdump --input=/path/to/your_index_name-mapping.json --output=http://dest_es --type=mapping
# 导入数据
elasticdump --input=/path/to/your_index_name-data.json --output=http://dest_es --type=data --limit=2000

## 方式三:使用脚本
cd /data/ && bash esdump.sh

Dockerfile

FROM node:alpine
WORKDIR /data
ADD esdump.sh /data/
RUN npm config set registry https://registry.npm.taobao.org && \ 
    npm install [email protected] && \
    apk add --no-cache bash vim curl && \
    rm -rf /var/cache/apk/* && \
    chmod +x /data/esdump.sh
ENV PATH /data/node_modules/elasticdump/bin:$PATH
ENTRYPOINT ["/usr/bin/tail", "-f", "/dev/null"]

esdump.sh

#!/bin/bash

export BATCH=2000
export WORKDIR="$(cd $(dirname "$0") && pwd)"
# 需要同步的类型: settings,analyzer,alias,mapping,data
# 因为analyzer是包含在settings中的,所以这两个只需要配置其中一个即可
export TYPES=("settings" "alias" "mapping" "data")


help_msg(){
    echo -e """Usage: """
    echo -e """        bash $(basename $0) --source="\<source addr\>" --dest="\<dest addr\>" --index="\<index name\>""""
    echo -e """Example: """
    echo -e """        bash $(basename $0) --source="172.16.20.12:9201" --dest="172.16.20.28:9201" --index="test-index""""
    echo -e """Options: """
    echo -e """        --source        源ES地址"""
    echo -e """        --dest          目标ES地址"""
    echo -e """        --index         要同步的索引名称,当为all时表示全部索引"""
    exit 1
}

get_opt(){
    for opt in "${@}";do
        key="${opt%=*}"
        value="${opt#*=}"
        case $key in
            --source)
                if [[ "${value}" =~ ^http ]];then
                    SOURCE_ES="${value}"
                else
                    SOURCE_ES="http://${value}"
                fi
            ;;
            --dest)
                if [[ "${value}" =~ ^http ]];then
                    DEST_ES="${value}"
                else
                    DEST_ES="http://${value}"
                fi
            ;;
            --index)
                INDEX="${value}"
            ;;
        esac
    done
}

get_opt "${@:1}"
if [[ "X${SOURCE_ES}" == "X" ]] || [[ "X${DEST_ES}" == "X" ]] || [[ "X${INDEX}" == "X" ]];then
    help_msg
fi

source_info=$(curl -XGET "${SOURCE_ES}/" 2>&1)
echo ${source_info} | grep -q "You Know, for Search"
if [[ $? -ne 0 ]];then
    echo "${SOURCE_ES} status not OK"
    exit 1
fi
dest_info=$(curl -XGET "${DEST_ES}/" 2>&1)
echo ${dest_info} | grep -q "You Know, for Search"
if [[ $? -ne 0 ]];then
    echo "${DEST_ES} status not OK"
    exit 1
fi

case ${INDEX} in
    all|ALL)
        source_indices=($(curl -XGET "${SOURCE_ES}/_cat/indices" 2>/dev/null | awk '{print $3}'))
        dest_indices=($(curl -XGET "${DEST_ES}/_cat/indices" 2>/dev/null | awk '{print $3}'))
        for index in ${source_indices[@]};do
            if [[ "${dest_indices[@]}" =~ "${index}" ]];then
                echo "目标集群中已存在索引: ${index},跳过同步该索引."
                continue
            fi
            echo "开始同步索引index: ${index}"
            for tp in ${TYPES[@]};do
                elasticdump --input="${SOURCE_ES}/${index}" --output="${DEST_ES}/${index}" --type=${tp} --noRefresh --limit ${BATCH}
            done
        done
    ;;
    *)
        source_indices=($(curl -XGET "${SOURCE_ES}/_cat/indices" 2>/dev/null | awk '{print $3}'))
        dest_indices=($(curl -XGET "${DEST_ES}/_cat/indices" 2>/dev/null | awk '{print $3}'))
        if [[ ! "${source_indices[@]}" =~ "${INDEX}" ]];then
            echo "源es集群${SOURCE_ES} 中未发现该索引: ${INDEX}."
            exit 3
        fi
        if [[ "${dest_indices[@]}" =~ "${INDEX}" ]];then
            echo "目标集群中已存在索引: ${INDEX},跳过同步该索引."
            exit 2
        fi
        echo "开始同步索引index: ${INDEX}"
        for tp in ${TYPES[@]};do
            elasticdump --input="${SOURCE_ES}/${INDEX}" --output="${DEST_ES}/${INDEX}" --type=${tp} --noRefresh --limit ${BATCH}
        done
    ;;
esac

Tag summary

Content type

Image

Digest

Size

64.9 MB

Last updated

over 6 years ago

docker pull hejiazhen/elasticdump:6.27.3