####久违的博客,疫情难生活也难,最近一人支撑公司运维“耐抗”,忽略了博客的更新,来一波最近遇到的数据迁移工作笔记!!!


背景:因服务器整体规划资源调整,将迁移旧的Elasticsearch上的数据到新的服务器上

准备工作:部署新服务器上的Elasticsearch版本需要和旧版本一致;保证运行正常前提下进行数据迁移;


离线迁移

离线迁移需要先停止老集群的写操作,将数据迁移完毕后在新集群上进行读写操作。适合于业务可以停服的场景。

离线迁移大概有以下几种方式:

  1. elasticsearch-dump

  2. snapshot

  3. reindex

  4. logstash



elasticsearch-dump方式

适用场景---适合数据量不大,迁移索引个数不多的场景


1、查看已有索引

curl http://127.0.0.1:9200/_cat/indices

Elasticsearch数据迁移_Elasticsearch


2、安装备份(esdump)工具

curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash

yum -y install nodejs

#如果上述yum命令执行有误,清空一下yum仓库

yum clean all

#创建一个备份命令目录,或直接在ES安装目录安装

mkdir esdump && cd esdump

npm install elasticdump

#安装完成后即可使用(以下是我新建目录的命令位置)

/usr/local/esdump/node_modules/elasticdump/bin/elasticdump


3、 主要参数说明

--input: 源地址,可为ES集群URL、文件或stdin,可指定索引,格式为:{protocol}://{host}:{port}/{index}

--input-index: 源ES集群中的索引

--output: 目标地址,可为ES集群地址URL、文件或stdout,可指定索引,格式为:{protocol}://{host}:{port}/{index}

--output-index: 目标ES集群的索引

--type: 迁移类型,默认为data,表明只迁移数据,可选settings, analyzer, data, mapping, alias

--limit:每次向目标ES集群写入数据的条数,不可设置的过大,以免bulk队列写满


4、官方文档参考

Elasticsearch数据迁移_数据迁移_02


5、 迁移单个索引

1)通过elasticdump命令将集群172.16.12.10中的索引迁移至集群192.168.5.16;

2)注意第一条命令先将索引的settings先迁移,如果直接迁移mapping或者data将失去原有集群中索引的配置信息如分片数量和副本数量等;

3)或可以直接在目标集群中将索引创建完毕后再同步mapping与data;

操作步骤:

####settings类型

./elasticdump --input=http://172.16.12.10:9200/date--output=http://192.168.5.16:9200/date --type=settings

###mapping类型

./elasticdump --input=http://172.16.12.10:9200/date --output=http://192.168.5.16:9200/date --type=mapping

###data类型

./elasticdump --input=http://172.16.12.10:9200/date --output=http://192.168.5.16:9200/date --type=data


6、迁移所有索引:

    ###以下操作通过elasticdump命令将将集群172.16.12.10中的所有索引迁移至集群192.168.5.16;

    ###注意此操作并不能迁移索引的配置如分片数量和副本数量,必须对每个索引单独进行配置的迁移,或者直接在目标集群中将索引创建完毕后再迁移数据;

      elasticdump --input=http://172.16.12.10:9200 --output=http://192.168.5.16:9200

7、检查192.168.5.16数据是否迁移完毕    

    curl http://192.168.5.16:9200/_cat/indices


到此,完成迁移,后续搬砖中。。。。