ES数据迁移

公司最近需要把uat环境数据迁移到生产环境,其中有一个就行es数据迁移,在这里记录下迁移办法。

快照模式迁移

因为安装es集群的时候也安装了kibana,可以通过kibana图形界面去备份快照,之前的文章有写过(点击查看),从A集群把备份的快照数据拷贝到新的集群快照数据保存目录下,然后通过kibana把快照进行还原就行,

对还原进行设置:

还原就行:

ElasticSearch-dump模式

安装

安装node

注:node 版本不低于 v10.0.0**

1
2
wget https://nodejs.org/dist/v14.17.3/node-v14.17.3-linux-x64.tar.xz -O /opt/node-v14.17.3-linux-x64.tar.xz
tar -xvf /opt/node-v14.17.3-linux-x64.tar.xz

配置环境变量

1
2
3
4
5
6
7
vim ~/.bashrc
# 追加以下内容
#node
export NODE_HOME=/opt/node-v14.17.3-linux-x64
export PATH=$NODE_HOME/bin:$PATH
# 刷新
source ~/.bashrc

查看是否出现版本

1
2
3
4
[root@localhost ~]# node -v
v14.17.3
[root@localhost ~]# npm -v
6.14.13
安装 Elasticdump
1
npm install elasticdump

出现安装成功提示:

1
2
+ elasticdump@6.72.0
added 112 packages from 198 contributors and audited 112 packages in 19.171s

安装成功后会在当前目录生成node_modules目录,里面包含 elasticdump 主目录

bin目录下面有两个可执行文件elasticdump(单索引操作)multielasticdump(多索引操作)

为了方便使用最好配置个环境变量

1
2
3
4
5
6
7
vim ~/.bashrc
# 追加以下内容
#node
export DUMP_HOME=/root/node_modules/elasticdump
export PATH=$DUMP_HOME/bin:$PATH
# 刷新
source ~/.bashrc

使用

elasticdump 使用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copy an index from production to staging with analyzer and mapping:
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=analyzer
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=mapping
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=data

# Backup index data to a file:
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index_mapping.json \
--type=mapping
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.json \
--type=data

# Backup and index to a gzip using stdout:
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=$ \
| gzip > /data/my_index.json.gz

# Backup the results of a query to a file
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=query.json \
--searchBody="{\"query\":{\"term\":{\"username\": \"admin\"}}}"

# Specify searchBody from a file
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=query.json \
--searchBody=@/data/searchbody.json

# Copy a single shard data:
elasticdump \
--input=http://es.com:9200/api \
--output=http://es.com:9200/api2 \
--input-params="{\"preference\":\"_shards:0\"}"

# Backup aliases to a file
elasticdump \
--input=http://es.com:9200/index-name/alias-filter \
--output=alias.json \
--type=alias

# Import aliases into ES
elasticdump \
--input=./alias.json \
--output=http://es.com:9200 \
--type=alias

# Backup templates to a file
elasticdump \
--input=http://es.com:9200/template-filter \
--output=templates.json \
--type=template

# Import templates into ES
elasticdump \
--input=./templates.json \
--output=http://es.com:9200 \
--type=template

# Split files into multiple parts
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.json \
--fileSize=10mb

# Import data from S3 into ES (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input "s3://${bucket_name}/${file_name}.json" \
--output=http://production.es.com:9200/my_index

# Export ES data to S3 (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input=http://production.es.com:9200/my_index \
--output "s3://${bucket_name}/${file_name}.json"

# Import data from MINIO (s3 compatible) into ES (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input "s3://${bucket_name}/${file_name}.json" \
--output=http://production.es.com:9200/my_index
--s3ForcePathStyle true
--s3Endpoint https://production.minio.co

# Export ES data to MINIO (s3 compatible) (using s3urls)
elasticdump \
--s3AccessKeyId "${access_key_id}" \
--s3SecretAccessKey "${access_key_secret}" \
--input=http://production.es.com:9200/my_index \
--output "s3://${bucket_name}/${file_name}.json"
--s3ForcePathStyle true
--s3Endpoint https://production.minio.co

# Import data from CSV file into ES (using csvurls)
elasticdump \
# csv:// prefix must be included to allow parsing of csv files
# --input "csv://${file_path}.csv" \
--input "csv:///data/cars.csv"
--output=http://production.es.com:9200/my_index \
--csvSkipRows 1 # used to skip parsed rows (this does not include the headers row)
--csvDelimiter ";" # default csvDelimiter is ','
multielasticdump 使用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# backup ES indices & all their type to the es_backup folder
multielasticdump \
--direction=dump \
--match='^.*$' \
--input=http://production.es.com:9200 \
--output=/tmp/es_backup

# Only backup ES indices ending with a prefix of `-index` (match regex).
# Only the indices data will be backed up. All other types are ignored.
# NB: analyzer & alias types are ignored by default
multielasticdump \
--direction=dump \
--match='^.*-index$'\
--input=http://production.es.com:9200 \
--ignoreType='mapping,settings,template' \
--output=/tmp/es_backup

常用参数:

1
2
3
4
5
--direction  dump/load 导出/导入
--ignoreType 被忽略的类型,data,mapping,analyzer,alias,settings,template
--includeType 包含的类型,data,mapping,analyzer,alias,settings,template
--suffix 加前缀,es6-${index}
--prefix 加后缀,${index}-backup-2018-03-13

实战

源es地址:http://192.168.1.140:9200
源es索引名:source_index
目标es地址:http://192.168.1.141:9200
目标es索引名:target_index

迁移
在线迁移

直接将两个ES的数据同步

单索引:

1
2
3
4
5
6
7
8
9
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=http://192.168.1.141:9200/target_index \
--type=mapping
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=http://192.168.1.141:9200/target_index \
--type=data \
--limit=2000 # 每次操作的objects数量,默认100,数据量大的话,可以调大加快迁移速度
离线迁移

单索引:

将源es索引数据导出为json文件,然后再导入目标es

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 导出
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=/data/source_index_mapping.json \
--type=mapping
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=/data/source_index.json \
--type=data \
--limit=2000
# 导入
elasticdump \
--input=/data/source_index_mapping.json \
--output=http://192.168.1.141:9200/source_index \
--type=mapping
elasticdump \
--input=/data/source_index.json \
--output=http://192.168.1.141:9200/source_index \
--type=data \
--limit=2000

全索引:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 导出
multielasticdump \
--direction=dump \
--match='^.*$' \
--input=http://192.168.1.140:9200 \
--output=/tmp/es_backup \
--includeType='data,mapping' \
--limit=2000
# 导入
multielasticdump \
--direction=load \
--match='^.*$' \
--input=/tmp/es_backup \
--output=http://192.168.1.141:9200 \
--includeType='data,mapping' \
--limit=2000 \
备份

单索引:

将es索引备份成gz文件,减少储存压力:

1
2
3
4
5
elasticdump \
--input=http://192.168.1.140:9200/source_index \
--output=$ \
--limit=2000 \
| gzip > /data/source_index.json.gz

ES命令

查看集群健康:

1
curl -X GET "http://localhost:9200/_cat/health?v"

查看集群节点:

1
curl -X GET "http://localhost:9200/_cat/nodes?v"

列出所有索引:

1
curl -X GET "http://localhost:9200/_cat/indices?v"
Thank you for your accept. mua!
-------------本文结束感谢您的阅读-------------