ElasticSearch数据备份与恢复

2020-07-29  本文已影响0人  皮多堡

记录下ElasticSearch数据的备份与迁移过程

1.指定备份文件地址

由于这里使用docker安装的es,所以指定备份文件到挂载的宿主机目录中,如果不是docker安装,直接指定为主机的目录即可

1. 列出所有的volume
root@zssy-test:/opt/docker/elk# vim docker-compose.yml 
root@zssy-test:/opt/docker/elk# docker volume ls
DRIVER              VOLUME NAME
local               73cd0ce3970fee80b3ff3d0211fb39c44b3b2cdcfd5c46d4b51f96ba59806d6e
local               elk_elasticsearch
local               elk_kibana
2.查看对应的宿主机目录
root@zssy-test:/opt/docker/elk# docker volume inspect elk_elasticsearch
[
    {
        "CreatedAt": "2020-07-29T13:40:15+08:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "elk",
            "com.docker.compose.volume": "elasticsearch"
        },
        "Mountpoint": "/var/lib/docker/volumes/elk_elasticsearch/_data",
        "Name": "elk_elasticsearch",
        "Options": null,
        "Scope": "local"
    }
]
3. 查看dokcer-compose中指定的挂载关系
volumes:
      - "elasticsearch:/usr/share/elasticsearch"

可以看到容器内/usr/share/elasticsearch目录对应宿主机的/var/lib/docker/volumes/elk_elasticsearch/_data目录。所以,设置的备份文件必须在/usr/share/elasticsearch下,这样再宿主机下可以直接找到对应的备份文件,否则,需要自己进入到容器内部,使用docker cp 命令从容器中拷贝出来,比较麻烦。
修改elasticsearch.yml配置文件

path.repo: ["/usr/share/elasticsearch/backup"]

修改完成之后重启es,重启之后配置才会生效

2.注册快照存储库

PUT http://192.168.0.112:9200/_snapshot/ecopherebbs
{
    "type": "fs",
    "settings": {
        "location": "E://es/backup"
    }
}
GET http://192.168.0.112:9200/_snapshot

3.备份数据(执行快照)

PUT http://192.168.0.112:9200/_snapshot/ecopherebbs/snapshot_20200729?wait_for_completion=true
{
  "indices": "sjy_index,economic_index",
  "ignore_unavailable": true,
   "include_global_state": true
}

其中:

  1. wait_for_completion=true时会一直阻塞直到快照完成
  2. snapshot_20200729 就是指定的备份快照命中
  3. 请求体重indices指定需要备份的索引(多个用逗号隔开),也可以不指定,这样会备份所有的es索引库数据

执行后最终返回如下信息表示备份创建成功:

{
    "accepted": true
}
GET http://192.168.0.112:9200/_snapshot/ecopherebbs/snapshot_20200729

其中最后的 snapshot_20200729 就是上一步指定的备份快照名称
返回如下信息:

{
    "snapshots": [
        {
            "snapshot": "snapshot_20200729",
            "uuid": "6LFUlFLRRBGHah3NkopxsA",
            "version_id": 7010199,
            "version": "7.1.1",
            "indices": [
                "weather_fetch_bd_index",
                "sjy_soil_tem_index",
                "economic_income_index",
                "sjy_rock_sys_index",
                "sjy_pile_index",
                ".kibana_task_manager",
                ".kibana_1",
                "sjy_economic_index",
                "water_usage_index",
                "water_temp_index",
                "wild_animal_index",
                "plant_light_index",
                "plant_park_index",
                ".security-7",
                "check_station_index",
                "sjy_index"
            ],
            "include_global_state": true,
            "state": "SUCCESS",
            "start_time": "2020-07-29T05:44:08.321Z",
            "start_time_in_millis": 1596001448321,
            "end_time": "2020-07-29T05:44:09.928Z",
            "end_time_in_millis": 1596001449928,
            "duration_in_millis": 1607,
            "failures": [],
            "shards": {
                "total": 38,
                "failed": 0,
                "successful": 38
            }
        }
    ]
}

可以看到备份文件中所有索引库(indices)列表,总分片(shards)数,成功备份的分片数等信息

root@zssy-test:/var/lib/docker/volumes/elk_elasticsearch/_data/backup# pwd
/var/lib/docker/volumes/elk_elasticsearch/_data/backup
root@zssy-test:/var/lib/docker/volumes/elk_elasticsearch/_data/backup# ll
总用量 52
drwxrwxr-x  3 zssy root  4096 7月  29 13:44 ./
drwxrwxr-x 11 zssy root  4096 7月  29 13:40 ../
-rw-rw-r--  1 zssy root    29 7月  29 13:44 incompatible-snapshots
-rw-rw-r--  1 zssy root  1544 7月  29 13:44 index-0
-rw-rw-r--  1 zssy root     8 7月  29 13:44 index.latest
drwxrwxr-x 18 zssy root  4096 7月  29 13:44 indices/
-rw-rw-r--  1 zssy root 22268 7月  29 13:44 meta-6LFUlFLRRBGHah3NkopxsA.dat
-rw-rw-r--  1 zssy root   525 7月  29 13:44 snap-6LFUlFLRRBGHah3NkopxsA.dat
root@zssy-test:/var/lib/docker/volumes/elk_elasticsearch/_data/backup# 

4.恢复数据

POST http://192.168.0.112:9200:9200/_snapshot/ecopherebbs/snapshot_20200729/_restore
其中:snapshot_20200729就是之前备份时指定的备份名称

返回如下信息表示恢复成功:

{
    "accepted": true
}

5.最佳实践

上一篇下一篇

猜你喜欢

热点阅读