Curl 命令方式对elasticsearch备份和恢复—— 筑梦之路

前提条件:

修改elasticsearch的配置文件elasticsearch.yml:

# 配置示例

path.repo: ["/data/es/snapshot"]


创建备份仓库:

# curl命令如下

curl -XPOST '192.168.0.100:9200/_snapshot/bro_backup' \
-H  'Content-Type: application/json' -d \
'{ 
    "type": "fs",  
    "settings": { 
       "location": "/data/es/snapshot",
       "compress": true, 
       "chunk_size": "1g", 
       "max_snapshot_bytes_per_sec": "50m", 
       "max_restore_bytes_per_sec": "50m"
      }
}'


# max_snapshot_bytes_per_sec 和max_restore_bytes_per_sec 为了限制备份和恢复时的速度

# 如果新建仓库的时候报错 "reason": "failed to create blob container"  access_denied_exception  请检查/data/es/snapshot文件夹权限

备份数据:

curl -XPUT "192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905?wait_for_completion=true"

# 如果不希望快照作为后台进程运行,可以通过添加wait_for_completion=true参数,使其在前台运行,知道备份完成。
# 如果想备份部分索引,可以加上indices 参数

curl -XPUT http://192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905 -d '
    {
        "indices": "index_1,index_2"
    }'

中止备份:

# 

curl -XDELETE http://192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905


查看备份信息:

# 

curl -XGET http://192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905
 

恢复数据:

恢复前准备:

将备份数据打包传到新机器上,并解压到/data/es/snapshot目录下

1. 修改配置

path.repo:["/data/backup/elasticsearch"],

2. 创建备份仓库

curl -XPOST 'http://192.168.0.200:9200/_snapshot/bro_backup' \
     -H  'Content-Type: application/json' \
     -d '{ 
            "type": "fs",  
            "settings": { 
               "location": "/data/es/snapshot", 
               "compress": true, 
               "chunk_size": "1g", 
               "max_snapshot_bytes_per_sec": "50m", 
               "max_restore_bytes_per_sec": "50m"
         }
}'


3. 恢复数据

curl -XPOST '192.168.0.200:9200/_snapshot/bro_backup/ss_20220905/_restore' \
     -H 'Content-Type: application/json' \
     -d'{ "ignore_unavailable": true, "include_global_state": false }'

如果只想恢复某些分片的数据,还可以在json参数里加{"indices": "game_info"},以指定只恢复game_info分片数据。

4. 查看恢复的分片数据

curl '192.168.0.200:9200/_cat/indices?v'

# elasticsearch快照方式备份和恢复

1. 配置elasticsearch.yml快照存储位置
```yaml
path.repo: ["/data/backups/my_backup"]
```
配置添加后需要重启elasticsearch

2. 注册仓库
```bash
curl -XPUT ' http://localhost:9200/_snapshot/my_backup' -d '{
    "type":"fs",
    "settings":{
        "location":"/data/backups/my_backup", 
        "compress":"true"
    }
}'
```

3. 查看仓库信息
```bash
curl -XGET ' http://localhost:9200/_snapshot/my_backup?pretty'
{
  "my_backup" : {
    "type" : "fs",
    "settings" : {
      "compress" : "true",    # 指定是否对快照文件进行压缩. 默认是 true.
      "location" : "/data/backups/my_backup" #指定快照的存储位置。必需要有
    }
  }
}
```

4. 删除仓库
```bash
curl -XDELETE 'localhost:9200/_snapshot/my_backup'
```

5. 建立快照
同一个集群中,一个仓库中能够存放多个快照。快照在集群中的名称是惟一的
```bash
#全部索引进行快照:
curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true?pretty"
#某个索引进行快照:
curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true?pretty" -d '{"indices":"customer"}'
```

6. 恢复快照
(恢复的意思是经过接口删除节点中的索引,不是删除备份的快照)
```bash
#恢复全部索引:
curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?wait_for_completion=true"
#恢复某个索引:
curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?wait_for_completion=true" -d '{"indices":"customer","ignore_unavailable":"true"}'

#注意:
#快照恢复前须要关掉索引
curl -XPOST "localhost:9200/my_index/_close"
curl -XPOST "localhost:9200/customer/_close"
 
#开启索引
curl -XPOST "localhost:9200/my_index/_open"
 
#查看索引状态
curl 'localhost:9200/_cat/indices?v'
curl -XGET "localhost:9200/_recovery/"
```

7. 查看快照状态信息
```bash
#a.列出全部当前正在运行的快照以及显示他们的详细状态信息
curl -XGET 'localhost:9200/_snapshot/_status?pretty'

#b.查看指定仓库正在运行的快照以及显示他们的详细状态信息
curl -XGET 'localhost:9200/_snapshot/my_backup/_status?pretty'
 
#c.查看指定快照的详细状态信息即便不是正在运行
curl -XGET 'localhost:9200/_snapshot/my_backup/snapshot_1/_status?pretty'
 
#d.支持同时指定多个快照ID查看多个快照的信息
curl -XGET 'localhost:9200/_snapshot/my_backup/snapshot_1,snapshot_2/_status?pretty'
```

 

参考资料:

Elasticsearch使用:Snapshot备份与恢复 - 腾讯云开发者社区-腾讯云

ES集群数据迁移 - 简书

https://www.elastic.co/guide/en/elasticsearch/reference/current/snapshot-restore.html

https://www.cnblogs.com/koktlzz/p/14521583.html

你可能感兴趣的:(数据库技术,linux系统运维,elasticsearch,大数据)