logo
0
0
WeChat Login
🎨 style: 移除日志中的表情符号以保持一致性

elasticsearch-sync

小毛驴搬家公司 - Elasticsearch 数据迁移与同步工具,支持模板、索引和数据的同步。

小搬家公司上线!帮你把 ES 数据从源端运到目标端,支持断点续传,累了歇会儿接着搬~

功能特性

  • 模板迁移:从源 Elasticsearch 集群迁移索引模板到目标集群
  • 索引迁移:从源 Elasticsearch 集群迁移索引结构到目标集群
  • 数据迁移:从源 Elasticsearch 集群迁移数据到目标集群
  • 支持认证:支持用户名密码认证
  • 灵活配置:支持命令行参数和环境变量配置

安装

go install cnb.cool/zhiqiangwang/elasticsearch-sync@latest

直接使用

elasticsearch-sync --help

命令详解

全局选项

选项环境变量默认值描述
--input-hostINPUT_HOSThttp://127.0.0.1:9200源 Elasticsearch 服务地址(小毛驴要从这里出发搬东西)
--input-usernameINPUT_USERNAME-源 ES 认证用户名(小毛驴要进门需要的通行证)
--input-passwordINPUT_PASSWORD-源 ES 认证密码(小毛驴要进门需要的密码)
--input-typeINPUT_TYPEes5源数据来源方式(es5 直接读取,fs 从打包文件读取)
--output-hostOUTPUT_HOSThttp://127.0.0.1:9200目标 Elasticsearch 服务地址(小毛驴要把东西搬到这里)
--output-usernameOUTPUT_USERNAME-目标 ES 认证用户名(小毛驴要进门需要的通行证)
--output-passwordOUTPUT_PASSWORD-目标 ES 认证密码(小毛驴要进门需要的密码)
--output-typeOUTPUT_TYPEfs目标输出方式 (es5 或 fs)(小毛驴要把东西放到哪里)
--fs-dirFS_DIRbackups文件系统目录(当 input-type=fs 或 output-type=fs 时有效)(小毛驴要从哪个文件夹读取或写入文件)
--disable-storeDISABLE_STOREfalse禁用断点续传存储(不保存迁移进度)(小毛驴要不要记住自己搬到哪了)

子命令

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 格式)(想搬哪些宝贝?给个清单先~)-

使用示例

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. 导出到文件系统

使用 --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

文件输出目录结构:

my_export/ # --output-fs-dir 指定的目录 ├── templates/ # 模板文件目录 │ ├── test.json # 模板文件(模版名称命令) ├── indices/ # 索引结构文件目录 │ ├── test.json # 索引文件(索引名称命令) └── data/ # 数据文件目录 ├── 5b6c7d8e...data # 数据文件(内容 MD5 命名,带换行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

注意事项

  1. 确保源和目标 Elasticsearch 集群网络可达
  2. 确保目标集群有足够的存储空间
  3. 对于大型集群,建议分批迁移数据以避免性能问题
  4. 迁移过程中可能会影响源集群的性能,建议在低峰期进行

断点续传

数据迁移命令默认支持断点续传功能,迁移进度会保存在 .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