文档

通过elasticsearch-dump工具迁移数据

更新时间:

通过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工具

  1. 连接ECS服务器。

  2. 安装node.js。

    1. 下载安装包。

      wget https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.xz
    2. 解压。

      tar -xf node-v16.18.0-linux-x64.tar.xz
    3. 设置环境变量。

      • 临时生效

        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
  3. 安装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>

本地文件路径。例如:/data/test.json

重要

将数据迁移至本地时,同一目录中不能存在目标文件,elasticsearch-dump工具会自动生成对应文件。

<YourQuery>

查询语句。示例:{\"query\":{\"term\":{\"_id\":\"100\"}}}

常见问题

Q:遇到报错Error: getaddrinfo ENOTFOUND elastic,怎么处理?

image.png

A:产生这个报错的原因是,HTTP认证密码包含特殊字符导致URL解析异常,解决办法是添加HTTP认证文件。

  1. 认证文件auth.ini中写入Elasticsearch实例的访问用户名和访问密码。示例如下:

    user=elastic
    password="Es#123456"
    说明

    password需要添加半角双引号("")。

  2. 执行如下命令,通过参数--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创建一个新的用户,为新用户配置所需权限后,用新用户进行迁移。

  • 本页导读 (1)
文档反馈