通过elasticsearch-dump工具,您可以将阿里云Elasticsearch实例索引的settings、mapping、data等数据迁移至本地或者其他阿里云实例上。本文介绍如何安装与使用elasticsearch-dump工具。
背景信息
elasticsearch-dump是一个开源的Elasticsearch数据迁移工具,详细信息请参见elasticsearch-dump官方文档。
前提条件
已创建阿里云Elasticsearch实例,具体操作请参见创建阿里云Elasticsearch实例。
已开启目标端阿里云Elasticsearch实例的自动创建索引功能,或提前创建对应索引。开启自动创建索引功能的具体操作,请参见配置YML参数。
已创建ECS服务器,该ECS用来安装elasticsearch-dump工具。具体操作,请参见自定义购买实例。
适用场景
elasticsearch-dump工具适用于数据量较小,索引数量不多的迁移场景。
安装elasticsearch-dump工具
连接ECS服务器。
具体操作请参见使用Workbench登录Linux实例。
安装node.js。
下载安装包。
wget https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.xz解压。
tar -xf node-v16.18.0-linux-x64.tar.xz设置环境变量。
临时生效
export PATH=$PATH:/root/node-v16.18.0-linux-x64/bin/永久生效
vim ~/.bash_profile export PATH=$PATH:/root/node-v16.18.0-linux-x64/bin/ source ~/.bash_profile
安装elasticsearch-dump。
npm install elasticdump -g
使用示例
如果密码中存在特殊符号(如 #、$等),发送请求时网络会被阿里云Web应用防火墙(WAF)拦截,相应的本地执行命令也会报错。详细报错和处理方法,请参见常见问题。
迁移数据到阿里云实例
迁移指定索引的settings
elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=http://"<OtherName>:<OtherPassword>"@<OtherEsHost>/<OtherEsIndex> --type=settings迁移指定索引的mapping
elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=http://"<OtherName>:<OtherPassword>"@<OtherEsHost>/<OtherEsIndex> --type=mapping迁移指定索引的data
elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=http://"<OtherName>:<OtherPassword>"@<OtherEsHost>/<OtherEsIndex> --type=data
迁移数据到本地
迁移指定索引的settings
elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> --type=settings迁移指定索引的mapping
elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> --type=mapping迁移指定索引的data
elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> --type=data通过query迁移数据
elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> ----searchBody="<YourQuery>"
恢复本地数据到阿里云实例
恢复本地data数据到阿里云实例:
elasticdump --input=<YourLocalFile> --output=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --type=data如果您有其他的数据迁移需求,请参见elasticsearch-dump官方文档。
使用上述示例时,需要将以下字段替换为对应的值。
参数 | 说明 |
<YourEsHost>/<OtherEsHost> | 源或目标阿里云Elasticsearch实例的私网或公网访问地址,可在实例的基本信息页面获取,获取方法请参见查看实例基本信息。 说明 如果安装elasticsearch-dump工具的服务器与源和目标阿里云Elasticsearch均在同一VPC下,可使用私网访问地址;如果不在同一VPC下,需要使用公网访问地址,且需要配置公网访问白名单,详细信息请参见配置实例公网或私网访问白名单。 |
<UserName>/<OtherName> | 源或目标阿里云Elasticsearch实例的访问用户名,默认为elastic。 |
<YourPassword>/<OtherPassword> | 源或目标阿里云Elasticsearch实例的访问密码。elastic用户的密码在创建实例时设定,如果忘记可进行重置,重置密码的注意事项和操作步骤请参见重置实例访问密码。 |
<YourEsIndex>/<OtherEsIndex> | 源或目标索引名称。 |
<YourLocalFile> | 本地文件路径。例如: 重要 将数据迁移至本地时,同一目录中不能存在目标文件,elasticsearch-dump工具会自动生成对应文件。 |
<YourQuery> | 查询语句。示例: |
常见问题
Q:遇到报错Error: getaddrinfo ENOTFOUND elastic,怎么处理?
[root@i-xxx ~]# elasticdump --input=http://"xxx.public.elasticsearch.aliyuncs.com:9200/customers --output=/root/customers.json --type=settings
Tue, 04 Jul 2023 08:42:53 GMT | starting dump
(node:25448) NOTE: We are formalizing our plans to enter AWS SDK for JavaScript (v2) into maintenance mode in 2023.
Please migrate your code to use AWS SDK for JavaScript (v3).
For more information, check the migration guide at https://a.co/7PzMCcy
(Use `node --trace-warnings ...` to show where the warning was created)
Tue, 04 Jul 2023 08:43:13 GMT | Error Emitted => getaddrinfo ENOTFOUND elastic
Tue, 04 Jul 2023 08:43:13 GMT | Error Emitted => getaddrinfo ENOTFOUND elastic
Tue, 04 Jul 2023 08:43:13 GMT | Total Writes: 0
Tue, 04 Jul 2023 08:43:13 GMT | dump ended with error (get phase) => Error: getaddrinfo ENOTFOUND elasticA:产生这个报错的原因是,HTTP认证密码包含特殊字符导致URL解析异常,解决办法是添加HTTP认证文件。
认证文件
auth.ini中写入Elasticsearch实例的访问用户名和访问密码。示例如下:user=elastic password="Es#123456"说明password需要添加半角双引号("")。
执行如下命令,通过参数
--httpAuthFile指定HTTP认证文件。elasticdump --input=http://es-*****.public.elasticsearch.aliyuncs.com:9200/customers --output=/root/customers.json --httpAuthFile=/root/auth.ini --type=settings
Q:遇到报错URIError: URI malformed,怎么处理?
A:elasticsearch-dump插件无法解析特殊字符的用户名和密码,如果您的用户名或密码中包含特殊符号,可能遇到以上报错。处理方法:
方法一:修改用户名或密码,将用户名或密码中的特殊符号去掉。
方法二:登录Kibana创建一个新的用户,为新用户配置所需权限后,用新用户进行迁移。