elasticsearch数据迁移
npm install elasticdump -g
# 导出
elasticdump \
--input=http://source-es:9200/my_index \
--output=/path/to/my_index_mapping.json \
--type=mapping
elasticdump \
--input=http://source-es:9200/my_index \
--output=/path/to/my_index_data.json \
--type=data
# 导入
elasticdump \
--input=/path/to/my_index_mapping.json \
--output=http://target-es:9200/my_index \
--type=mapping
elasticdump \
--input=/path/to/my_index_data.json \
--output=http://target-es:9200/my_index \
--type=data
查看索引可以通过浏览器插件es-client
导入导出脚本
#!/bin/bash
# 日志文件
LOG_FILE="es_data_migration.log"
# 启动 Node 容器
echo "$(date '+%Y-%m-%d %H:%M:%S') 启动 Node 容器..." | tee -a $LOG_FILE
#docker run -d --name es-node-trans harbor.primelifescience.com.cn/prime/node:20.10.0-alpine3.18-prime
docker run -d --name es-node-trans harbor.primelifescience.com.cn/prime/node:20.10.0-alpine3.18-prime tail -f /dev/null
# 等待容器启动
sleep 10
# 在容器中安装 elasticdump
echo "$(date '+%Y-%m-%d %H:%M:%S') 在容器中安装 elasticdump..." | tee -a $LOG_FILE
docker exec es-node-trans sh -c "npm install elasticdump -g"
# 定义导出和导入的函数
function export_data() {
local index_name=$1
echo "$(date '+%Y-%m-%d %H:%M:%S') 导出 $index_name 的 mapping..." | tee -a $LOG_FILE
docker exec es-node-trans sh -c "elasticdump --input=http://172.24.1.172:19200/$index_name --output=./${index_name}_mapping.json --type=mapping" | tee -a $LOG_FILE
echo "$(date '+%Y-%m-%d %H:%M:%S') 导出 $index_name 的数据..." | tee -a $LOG_FILE
docker exec es-node-trans sh -c "elasticdump --input=http://172.24.1.172:19200/$index_name --output=./${index_name}.json --type=data" | tee -a $LOG_FILE
}
function import_data() {
local index_name=$1
local target_host=$2
echo "$(date '+%Y-%m-%d %H:%M:%S') 导入 $index_name 的 mapping 到 $target_host..." | tee -a $LOG_FILE
docker exec es-node-trans sh -c "elasticdump --input=./${index_name}_mapping.json --output=$target_host/$index_name --type=mapping" | tee -a $LOG_FILE
echo "$(date '+%Y-%m-%d %H:%M:%S') 导入 $index_name 的数据到 $target_host..." | tee -a $LOG_FILE
docker exec es-node-trans sh -c "elasticdump --input=./${index_name}.json --output=$target_host/$index_name --type=data" | tee -a $LOG_FILE
}
# 导出数据
export_data "index-test"
export_data "index-uat"
export_data "index-prod"
# 导入数据
import_data "index-test" "http://t:19200"
import_data "index-uat" "http://u:19200"
import_data "index-prod" "http://p:19200"
# 清理容器
echo "$(date '+%Y-%m-%d %H:%M:%S') 清理容器..." | tee -a $LOG_FILE
docker stop es-node-trans
docker rm es-node-trans
echo "$(date '+%Y-%m-%d %H:%M:%S') 数据迁移完成!" | tee -a $LOG_FILE
基于Logstash
cat > logstash-es-migration.conf <<EOF
input {
elasticsearch {
hosts => ["http://source-es:9200"]
index => "*" # 迁移所有索引
size => 1000 # 每次读取的文档数
scroll => "5m"
docinfo => true
}
}
output {
elasticsearch {
hosts => ["http://target-es:9200"]
index => "%{[@metadata][_index]}"
document_id => "%{[@metadata][_id]}"
}
}
EOF
bin/logstash -f logstash-es-migration.conf