ElasticSearch 快照 备份、恢复数据

文章目录

    • ElasticSearch 设置备份文件地址
    • 注册快照存储库
    • 查看快照存储库保存结果
    • 创建快照
      • 异步创建
      • 指定索引进行快照
      • 查看全部快照
    • 在服务器查看备份的数据
    • 恢复数据
      • 本机恢复
      • 其他服务器恢复
    • 常见问题
      • 报错 doesn't match any of the locations specified by path.repo because this setting is empty

ElasticSearch 设置备份文件地址

  • 在elasticsearch.yml 新增
path.repo: ["/home/es/snapshot"]

注册快照存储库

ecopherebbs是我们定义的快照名,可以自定义

PUT _snapshot/ecopherebbs
{"type": "fs","settings": {"location": "/home/es/snapshot"}
}

保存成功
在这里插入图片描述

查看快照存储库保存结果

GET _snapshot

返回结果

{"ecopherebbs" : {"type" : "fs","settings" : {"compress": true,"location" : "/home/es/snapshot"}}
}

在这里插入图片描述

创建快照

异步创建

  • 创建
PUT _snapshot/ecopherebbs/snapshot_20210631
{"ignore_unavailable": true,"include_global_state": true
}
  • 查看状态
GET _snapshot/ecopherebbs/snapshot_20210631

处理中

{"snapshots" : [{"snapshot" : "snapshot_20210631","uuid" : "PTBkWV-pQ8CQ7l4RNxISfA","version_id" : 7090399,"version" : "7.9.3","indices" : ["t_e_s_t___2_0_2_1_0_4_2_1_0_9_2_9_index","test0627","test110",".kibana-event-log-7.9.3-000001","fang_fa_index","filebeat-7.9.3-2021.06.29-000001","gateway_requests","my_index","ilm-history-2-000003",".kibana-event-log-7.9.3-000002",".kibana_1"],"data_streams" : [ ],"include_global_state" : true,"state" : "IN_PROGRESS","start_time" : "2021-06-30T05:26:10.530Z","start_time_in_millis" : 1625030770530,"end_time" : "1970-01-01T00:00:00.000Z","end_time_in_millis" : 0,"duration_in_millis" : 0,"failures" : [ ],"shards" : {"total" : 0,"failed" : 0,"successful" : 0}}]
}

处理完成

{"snapshots" : [{"snapshot" : "snapshot_20210631","uuid" : "PTBkWV-pQ8CQ7l4RNxISfA","version_id" : 7090399,"version" : "7.9.3","indices" : ["t_e_s_t___2_0_2_1_0_4_2_1_0_9_2_9_index","test0627","test110",".kibana-event-log-7.9.3-000001","fang_fa_index","filebeat-7.9.3-2021.06.29-000001","gateway_requests","my_index","ilm-history-2-000003",".kibana-event-log-7.9.3-000002",".kibana_1"],"data_streams" : [ ],"include_global_state" : true,"state" : "SUCCESS","start_time" : "2021-06-30T05:26:10.530Z","start_time_in_millis" : 1625030770530,"end_time" : "2021-06-30T05:26:16.937Z","end_time_in_millis" : 1625030776937,"duration_in_millis" : 6407,"failures" : [ ],"shards" : {"total" : 200,"failed" : 0,"successful" : 200}}]
}

指定索引进行快照

PUT _snapshot/ecopherebbs/snapshot_20210630?wait_for_completion=true
{"indices": "index1,index2","ignore_unavailable": true,"include_global_state": true
}
  1. wait_for_completion=true时会一直阻塞直到快照完成,kibana执行时超时为30秒,超时后快照依然会在后台执行
  2. snapshot_20200729 就是指定的备份快照命中
  3. 请求体重indices指定需要备份的索引(多个用逗号隔开),也可以不指定,这样会备份所有的es索引库数据

查看全部快照

GET _snapshot/ecopherebbs/_all?pretty

在服务器查看备份的数据

每次执行快照都会生成一个 meta和一个snap文件

[es@test12 snapshot]$ ls -lrt
总用量 1404
drwxrwxr-x. 62 es es   4096 6月  30 13:23 indices
-rw-rw-r--.  1 es es 460329 6月  30 13:24 meta--WCM58RpQNm5bEywCjO_cg.dat
-rw-rw-r--.  1 es es   1596 6月  30 13:24 snap--WCM58RpQNm5bEywCjO_cg.dat
-rw-rw-r--.  1 es es   1596 6月  30 13:26 snap-PTBkWV-pQ8CQ7l4RNxISfA.dat
-rw-rw-r--.  1 es es 460329 6月  30 13:26 meta-PTBkWV-pQ8CQ7l4RNxISfA.dat
-rw-rw-r--.  1 es es    302 6月  30 13:52 snap-N3zRMDnDTEqoiShEwjKosA.dat
-rw-rw-r--.  1 es es 460329 6月  30 13:52 meta-N3zRMDnDTEqoiShEwjKosA.dat
-rw-rw-r--.  1 es es  24990 6月  30 13:52 index-2
-rw-rw-r--.  1 es es      8 6月  30 13:52 index.latest

恢复数据

本机恢复

POST _snapshot/ecopherebbs/snapshot_20210631_02/_restore

执行恢复要将之前的索引删除,否则会报错

  • 或者在恢复时执行新索引名称 官网描述
POST /_snapshot/my_backup/snapshot_1/_restore
{"indices": "index_1", "rename_pattern": "index_(.+)", "rename_replacement": "restored_index_$1" 
}

其他服务器恢复

注意:版本要相同,版本不同很有可能会导致失败

  1. 配置第一步设置的目录
    path.repo: ["/home/es/snapshot"]
  2. 创建仓库
PUT _snapshot/ecopherebbs
{"type": "fs","settings": {"location": "/home/es/snapshot-test"}
}
  1. 执行恢复索引
POST _snapshot/ecopherebbs/snapshot_20210631_02/_restore

常见问题

报错 doesn’t match any of the locations specified by path.repo because this setting is empty

{"error" : {"root_cause" : [{"type" : "repository_exception","reason" : "[ecopherebbs] location [/home/es/snapshot] doesn't match any of the locations specified by path.repo because this setting is empty"}],"type" : "repository_exception","reason" : "[ecopherebbs] failed to create repository","caused_by" : {"type" : "repository_exception","reason" : "[ecopherebbs] location [/home/es/snapshot] doesn't match any of the locations specified by path.repo because this setting is empty"}},"status" : 500
}
  • 解决方法:

在为es集群环境做灾备和恢复时候,首先需要创建创建一个仓库,并往仓库中存放快照(每个快照中会区分不同的索引)。但是在创建仓库的时候,要求仓库的地址必须在每个集群环境中的elasticsearch.yml中进行配置

个人公众号(大数据学习交流): hadoopwiki

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注