Elasticsearch 索引的备份与恢复实现

测试环境:一个es7.2集群,名称为test。集群中有一个节点,ip为192.168.52.127。存储库选择本地文件系统。
具体实现
1.注册Repository
首先,在elasticsearch.yml中添加path.repo,启动elasticsearch。
cluster.name: test
node.name: node-1
path.data: /home/elk/elasticsearch-7.2.0/data
path.logs: /home/elk/elasticsearch-7.2.0/logs
http.port: 9200
network.host: 0.0.0.0
discovery.seed_hosts: ["192.168.52.127"]
cluster.initial_master_nodes: ["node-1"]
path.repo: ["/home/elk/backup"]

然后,在kibana中执行:
PUT /_snapshot/my_fs_backup
{
    "type": "fs",
    "settings": {
        "location": "/home/elk/backup",
        "compress": true
    }
}
2.创建索引,写入数据
PUT test
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 0
    }
}
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "msg" : "value1" }
{ "index" : { "_index" : "test", "_id" : "2" } }
{ "msg" : "value2" }
{ "index" : { "_index" : "test", "_id" : "3" } }
{ "msg" : "value3" }
{ "index" : { "_index" : "test", "_id" : "4" } }
{ "msg" : "value4" }
{ "index" : { "_index" : "test", "_id" : "5" } }
{ "msg" : "value5" }
3.为上述索引创建一个snapshot
PUT /_snapshot/my_fs_backup/snapshot_test_1?wait_for_completion=true
{
  "indices": "test",
  "ignore_unavailable": true,
  "include_global_state": false
}
可以查看存储库路径,/home/elk/backup,里面有内容。
4.删除上述索引
DELETE test
5.恢复上述索引
POST /_snapshot/my_fs_backup/snapshot_test_1/_restore
{
  "indices": "test",
  "ignore_unavailable": true,
  "include_global_state": true
}
6.查看检查索引数据
GET test/_search
GET test/_settings

参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.2/modules-snapshots.html 

拓展
如果将上述存储库的内容拷贝到另一个集群配置的存储库路径,那么该索引数据也能恢复吗?
另一个集群环境:一个es7.2集群,名称为test-1。集群中有一个节点,ip为192.168.52.128。存储库选择本地文件系统。
1)在test-1集群中,执行上述1的步骤,即注册Repository
首先,在elasticsearch.yml中添加path.repo,启动elasticsearch。
cluster.name: test-1
node.name: node-2
path.data: /home/elk/elasticsearch-7.2.0/data
path.logs: /home/elk/elasticsearch-7.2.0/logs
http.port: 9200
network.host: 0.0.0.0
discovery.seed_hosts: ["192.168.52.128"]
cluster.initial_master_nodes: ["node-2"]
path.repo: ["/home/elk/backup"]
然后,在kibana中执行:
PUT /_snapshot/my_fs_backup_1
{
    "type": "fs",
    "settings": {
        "location": "/home/elk/backup",
        "compress": true
    }
}
2)确保不存在索引test,然后执行恢复
POST /_snapshot/my_fs_backup_1/snapshot_test_1/_restore
{
  "indices": "test",
  "ignore_unavailable": true,
  "include_global_state": true
}
3)查看验证索引test。经验证,恢复后的索引test的数据和配置与期望的一致。
GET test/_search
GET test/_settings

你可能感兴趣的:(ELK)