ES数据的备份、恢复、迁移-快照方式
Elasticsearch提供了快照和恢复功能,可以对整个集群或单个索引进行快照。快照是一个包含索引数据和状态的备份。你可以将快照存储在本地文件系统、远程文件系统、Amazon S3等位置。
1.设置存储库
快照仓库名称设置为"backup1",存储库路径设置为"/usr/share/elasticsearch/backup"
配置文件:elasticsearch.yml
path.repo: ["/usr/share/elasticsearch/backup"]
2.设置快照仓库
快照仓库名称:logstash_bak1
]# curl -XPUT -u'esuser:es123' -H "Content-Type: application/json" http://172.25.230.47:19201/_snapshot/logstash_bak1?pretty -d' { "type": "fs", "settings": { "location": "/elk/es/backup/logstash1" } }'
3.创建备份快照
备份索引:logstash-dev-2024.07.17
]# curl -XPUT -u'esuser:es123' -H "Content-Type: application/json" http://172.25.230.47:19201/_snapshot/logstash_bak1/snapshot_1 -d '{ "indices": "logstash-dev-2024.07.17", "ignore_unavailable": true, "include_global_state": false }'
批量备份索引: 备份开头包含logstash-dev-*的所有索引
]# curl -XPUT -u'esuser:es123' -H "Content-Type: application/json" http://172.25.230.47:19201/_snapshot/logstash_bak2/snapshot_1 -d '{ "indices": "logstash-dev-*", "ignore_unavailable": true, "include_global_state": false }'
查看快照
]# curl -XGET -u'esuser:es123' http://172.25.230.47:19201/_snapshot/logstash_bak1/snapshot_1?pretty
查看备份数据的目录
4.恢复备份
备份数据打包
将快照仓库logstash对应的数据存储目录进行打包
]# cd /elk/es/backup ]# tar cvzf logstash.tar.gz logstash
复制备份数据
将打包的备份数据复制到新主机的/es/backup目录中,并解压
]# cd /es/backup ]# tar xvf logstash2.tar.gz
建立快照仓库
在目标主机上建立相同的快照仓库
仓库路径:/usr/share/elasticsearch/backup/logstash
]# curl -XPUT -H "Content-Type: application/json" http://172.25.230.56:19201/_snapshot/logstash?pretty -d' { "type": "fs", "settings": { "location": "/usr/share/elasticsearch/backup/logstash" } }'
验证快照
验证快照中的索引是否已加载
##查看所有快照的名称 ]# curl -s -XGET http://172.25.230.56:19201/_snapshot/logstash/_all?pretty | jq -r '.snapshots[].snapshot'
##查看快照中的索引是否已加载成功 ]# curl -XGET http://172.25.230.56:19201/_snapshot/logstash/snapshot2?pretty
恢复数据
]# curl -XPOST -H "Content-Type: application/json" http://172.25.230.56:19201/_snapshot/logstash_bak2/snapshot_1/_restore -d '{ "indices": "logstash-dev-*", "ignore_unavailable": true, "include_aliases": false }'
5.查看数据恢复状态
]# curl -XGET http://172.25.230.56:19201/_recovery?pretty
如果显示"DONE",则表明索引已恢复成功;如果显示"INDEX",则表明索引正在恢复中
等待索引的分片、副本恢复正常:STARTED状态