Elasticsearch 数据迁移
介绍
es 迁移方式主要有两种:
- 快照(Snapshot)迁移(推荐,适合大数据量)
- elasticdump 迁移(适合小规模或无法配置共享存储)
今天主要介绍 第一种方式 Snapshot (快照)迁移
涉及技术栈、工具
curl 、elasticsearch 、xshell、xftp
注意
版本问题
es 版本迁移支持向后兼容(从低版本迁移到高版本
),但不支持从高版本迁移到低版本
可以运行以下命令查询es相关信息
curl -X GET "http://localhost:9200"
D:\tool\curl\curl-8.7.1_7-win64-mingw\bin>curl -X GET "http://localhost:9200"
{
"name" : "USER-20240420RI",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "t7liD1i2Rduf64ngt86IQg",
"version" : {
"number" : "7.6.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date" : "2020-02-29T00:15:25.529771Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
我是从7.6.1迁移到 7.6.2 版本可以直接进行数据迁移
磁盘问题
Windows 服务器:快照需要额外磁盘空间(建议 1.5 倍数据量)。
Linux 服务器: 确保足够空间(建议 2 倍数据量)。
(Snapshot)迁移
1. 配置文件(windows)
编辑 elasticsearch.yml(通常在 es安装地址的 Elasticsearch\config\):
path.repo: ["D:\\shared\\backup"]
2. 重启ES服务(windows)
net stop Elasticsearch
net start Elasticsearch
3. 注册快照存储库(windows)
curl -X PUT "http://localhost:9200/_snapshot/my_backup" -H 'Content-Type: application/json' -d '{
"type": "fs",
"settings": {
"location": "D:\\shared\\backup",
"compress": true
}
}'
返回 以下内容则算注册成功
{"acknowledged":true}
也可以运行以下代码进行验证
D:\tool\curl\curl-8.7.1_7-win64-mingw\bin>curl -X GET "localhost:9200/_snapshot/my_backup?pretty"
{
"my_backup" : {
"type" : "fs",
"settings" : {
"compress" : "true",
"location" : "D:\\shared\\backup"
}
}
}
1. 配置文件(linux)
编辑 elasticsearch.yml(通常在 es安装地址的 Elasticsearch\config\):
path.repo: ["/home/xxx/es_backup"]
由于es 都有对应的elasticsearch 用户,需要把文件夹权限开通
sudo mkdir -p /home/xxx/es_backup
sudo chown -R elasticsearch:elasticsearch /home/xxx/es_backup
2.重启(linux)
sudo systemctl restart elasticsearch
3. 注册快照存储库(linux)
curl -X PUT "http://localhost:9200/_snapshot/my_backup" -H 'Content-Type: application/json' -d '{
"type": "fs",
"settings": {
"location": " /home/xxx/es_backup",
"compress": true
}
}'
创建快照
备份所有索引:
curl -X PUT "http://localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true" -H 'Content-Type: application/json' -d '{
"indices": "_all",
"ignore_unavailable": true,
"include_global_state": false
}'
如果只迁移特定索引:
curl -u username:pwd -X PUT "http://<windows_server_ip>:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true" -H 'Content-Type: application/json' -d '{
"indices": "index1,index2",
"ignore_unavailable": true
}'
传输快照
我是直接使用xftp直接把文件替换了
恢复快照
curl -u username:pwd -X POST "http:///<windows_server_ip>/_snapshot/my_backup/snapshot_1/_restore?wait_for_completion=true" -H 'Content-Type: application/json' -d '{
"indices": "index1,index2",
"ignore_unavailable": true
}'