elasticsearch-sync
小毛驴搬家公司 - Elasticsearch 数据迁移与同步工具,支持模板、索引和数据的同步。
小搬家公司上线!帮你把 ES 数据从源端运到目标端,支持断点续传,累了歇会儿接着搬~
功能特性
- 模板迁移:从源 Elasticsearch 集群迁移索引模板到目标集群
- 索引迁移:从源 Elasticsearch 集群迁移索引结构到目标集群
- 数据迁移:从源 Elasticsearch 集群迁移数据到目标集群
- 支持认证:支持用户名密码认证
- 灵活配置:支持命令行参数和环境变量配置
安装
go install cnb.cool/zhiqiangwang/elasticsearch-sync@latest
直接使用
elasticsearch-sync --help
命令详解
全局选项
| 选项 |
环境变量 |
默认值 |
描述 |
--input-host |
INPUT_HOST |
http://127.0.0.1:9200 |
源 Elasticsearch 服务地址(小毛驴要从这里出发搬东西) |
--input-username |
INPUT_USERNAME |
- |
源 ES 认证用户名(小毛驴要进门需要的通行证) |
--input-password |
INPUT_PASSWORD |
- |
源 ES 认证密码(小毛驴要进门需要的密码) |
--input-type |
INPUT_TYPE |
es5 |
源数据来源方式(es5 直接读取,fs 从打包文件读取) |
--output-host |
OUTPUT_HOST |
http://127.0.0.1:9200 |
目标 Elasticsearch 服务地址(小毛驴要把东西搬到这里) |
--output-username |
OUTPUT_USERNAME |
- |
目标 ES 认证用户名(小毛驴要进门需要的通行证) |
--output-password |
OUTPUT_PASSWORD |
- |
目标 ES 认证密码(小毛驴要进门需要的密码) |
--output-type |
OUTPUT_TYPE |
fs |
目标输出方式 (es5 或 fs)(小毛驴要把东西放到哪里) |
--fs-dir |
FS_DIR |
backups |
文件系统目录(当 input-type=fs 或 output-type=fs 时有效)(小毛驴要从哪个文件夹读取或写入文件) |
--disable-store |
DISABLE_STORE |
false |
禁用断点续传存储(不保存迁移进度)(小毛驴要不要记住自己搬到哪了) |
子命令
1. templates
模板迁移(魔法师复制模板)
魔法师来啦!帮你在目标集群复制和源集群一样的魔法模板~
elasticsearch-sync templates [全局选项]
2. indices
索引迁移(施工队建楼)
施工队来啦!帮你在目标集群搭建和源集群一样的索引结构~
elasticsearch-sync indices [全局选项] [--indices 索引名1,索引名2]
| 选项 |
描述 |
--indices |
指定要搭建的索引(不指定就全搭,小心累坏建筑工人) |
3. data
数据迁移(小毛驴搬家公司)
小搬家公司上线!从源 ES 集群把数据偷偷运到目标集群~
elasticsearch-sync data [全局选项] [--indices 索引名1,索引名2] [--size 1000] [--query '{"query":{...}}']
| 选项 |
描述 |
默认值 |
--indices |
指定要搬家的索引(不指定就全搬,小心累坏小毛驴) |
所有开放的索引 |
--size |
每次搬运的文档数量(不要贪多,小毛驴一次扛不动太多) |
1000 |
--query |
查询条件 (JSON 格式)(想搬哪些宝贝?给个清单先~) |
- |
4. aliases
别名同步(邮递员送信)
邮递员来啦!帮你在目标集群投递和源集群一样的索引别名~
elasticsearch-sync aliases [全局选项] [--indices 索引名]
| 选项 |
描述 |
--indices |
指定单个索引名称(不指定就同步所有索引的别名) |
使用示例
1. 迁移所有模板
./elasticsearch-sync templates \
--input-host http://source-es:9200 \
--input-username admin \
--input-password password \
--output-host http://target-es:9200 \
--output-username admin \
--output-password password
2. 迁移指定索引结构
./elasticsearch-sync indices \
--input-host http://source-es:9200 \
--output-host http://target-es:9200 \
--indices index1,index2
3. 迁移所有数据
./elasticsearch-sync data \
--input-host http://source-es:9200 \
--output-host http://target-es:9200 \
--size 2000
4. 按条件迁移数据
使用 --query 参数指定查询条件,只迁移符合条件的数据:
# 迁移 status 为 active 的文档
./elasticsearch-sync data \
--input-host http://source-es:9200 \
--output-host http://target-es:9200 \
--indices my_index \
--query '{"query":{"match":{"status":"active"}}}'
# 迁移指定时间范围内的文档
./elasticsearch-sync data \
--input-host http://source-es:9200 \
--output-host http://target-es:9200 \
--indices logs \
--query '{"query":{"range":{"timestamp":{"gte":"2024-01-01","lte":"2024-12-31"}}}}'
# 使用 term 查询迁移特定类型的文档
./elasticsearch-sync data \
--input-host http://source-es:9200 \
--output-host http://target-es:9200 \
--indices products \
--query '{"query":{"term":{"category":"electronics"}}}'
5. 同步所有索引的别名
./elasticsearch-sync aliases \
--input-host http://source-es:9200 \
--input-username admin \
--input-password password \
--output-host http://target-es:9200 \
--output-username admin \
--output-password password
6. 同步指定单个索引的别名
./elasticsearch-sync aliases \
--input-host http://source-es:9200 \
--output-host http://target-es:9200 \
--indices my_index
7. 导出到文件系统
使用 --output-type=fs 将数据导出到本地文件系统:
# 导出模板到文件
./elasticsearch-sync templates \
--input-host http://source-es:9200 \
--output-type fs \
--fs-dir ./my_export
# 导出索引结构到文件
./elasticsearch-sync indices \
--input-host http://source-es:9200 \
--output-type fs \
--fs-dir ./my_export \
--indices index1,index2
# 导出数据到文件
./elasticsearch-sync data \
--input-host http://source-es:9200 \
--output-type fs \
--fs-dir ./my_export \
--indices my_index \
--size 1000
# 导出别名到文件
./elasticsearch-sync aliases \
--input-host http://source-es:9200 \
--output-type fs \
--fs-dir ./my_export
文件输出目录结构:
my_export/ # --output-fs-dir 指定的目录
├── templates/ # 模板文件目录
│ ├── test.json # 模板文件(模版名称命令)
├── indices/ # 索引结构文件目录
│ ├── test.json # 索引文件(索引名称命令)
├── data/ # 数据文件目录
│ ├── 5b6c7d8e...data # 数据文件(内容 MD5 命名,带换行json数据)
└── aliases/ # 别名文件目录
├── test.json # 别名文件
环境变量配置
除了命令行参数外,还可以通过环境变量进行配置:
# 设置源 ES 配置
export INPUT_HOST="http://source-es:9200"
export INPUT_USERNAME="admin"
export INPUT_PASSWORD="password"
# 设置目标 ES 配置
export OUTPUT_HOST="http://target-es:9200"
export OUTPUT_USERNAME="admin"
export OUTPUT_PASSWORD="password"
# 执行命令
./elasticsearch-sync templates
注意事项
- 确保源和目标 Elasticsearch 集群网络可达
- 确保目标集群有足够的存储空间
- 对于大型集群,建议分批迁移数据以避免性能问题
- 迁移过程中可能会影响源集群的性能,建议在低峰期进行
断点续传
数据迁移命令默认支持断点续传功能,迁移进度会保存在 .data 目录中。如果迁移过程中断,下次执行相同命令时会自动从断点继续。
断点续传特性
- 自动保存进度:每批次数据迁移完成后自动保存进度
- 自动恢复:重新执行命令时自动从上次断点继续
- 任务列表:记录待迁移的索引列表,避免遗漏
禁用断点续传
如果不需要断点续传功能(例如一次性迁移或测试场景),可以通过以下方式禁用:
# 使用命令行参数
./elasticsearch-sync data --disable-store
# 或使用环境变量
export DISABLE_STORE=true
./elasticsearch-sync data
进度文件说明
断点续传的进度文件存储在用户主目录下的 .elasticsearch-sync/data 目录中:
~/.elasticsearch-sync/data/ # 进度存储目录
├── job.json # 待迁移索引列表
├── progress_index1.json # index1 的迁移进度
└── progress_index2.json # index2 的迁移进度
许可证
Apache License 2.0