es快照 备份与恢复
es快照存储备份与恢复
1. 修改es配置文件config下的elasticsearch.yml,增加本地保存路径
path.repo: ["/e/es_store/"] #如果想保存到多个路径可以逗号隔开
# 修改完之后,记得重启Es
# 如果本地没有该目录,创建该目录,目录权限也要设置正确
2. 创建基于本地的快照仓库
# 创建仓库
curl -X PUT "http://localhost:9200/_snapshot/my_backup&pretty" -H 'Content-Type: application/json' -d' { "type": "fs", "settings": { "compress" : "true", "location": "/es/es_store/" } }' # 查看仓库信息 curl -XGET "http://localhost:9200/_snapshot/my_backup?pretty" # 删除仓库 curl -XDELETE 'http://localhost:9200/_snapshot/my_backup'
3. 创建快照
# 创建快照 curl -XPUT ’http://localhost:9200/_snapshot/my_backup/snapshot_2?wait_for_completion=true‘ # 使用当前日期来命名快照,日期格式:snapshot-2018.11.29 curl -X PUT "http://localhost:9200/_snapshot/my_backup/%3Csnapshot-%7Bnow%2Fd%7D%3E" # 查看索引 curl -XGET 'http://localhost:9200/_cat/indices?v' # 指定索引创建快照 curl -XPUT 'http://localhost:9200/_snapshot/my_backup/snapshot_1' -H 'Content-Type: application/json' -d '{ "indices": "nginx-ingress-controller-log-2018.11.27,nginx-ingress-controller-log-2018.11.28", "ignore_indices": "missing" }' # 查看所有快照 curl -XGET "http://localhost:9200/_snapshot/my_backup?pretty" # 查看指定快照 curl -XGET "localhost:9200/_snapshot/my_backup/_all?pretty"
4. 恢复快照
# 恢复快照(所有索引) curl -XPOST http://localhost:9200/_snapshot/my_backup/snapshot_1/_restore # 恢复快照(指定索引) curl -XPOST "http://localhost:9200/_snapshot/my_backup/snapshot-2018.11.29/_restore?wait_for_completion=true" -H 'Content-Type: application/json' -d' { "indices":"nginx-ingress-controller-log-2018.11.29", "ignore_unavailable":"true" }' # 注意:恢复快照的时候需要停止要恢复的索引 # 快照恢复前需要关掉索引 curl -XPOST "http://localhost:9200/my_index/_close"curl -XPOST "http://localhost:9200/customer/_close" # 开启索引 curl -XPOST "http://localhost:9200/my_index/_open" # 查看索引状态 curl -XGET 'http://localhost:9200/_cat/indices?v'curl -XGET "http://localhost:9200/_recovery/"
5. 常用命令:
# 查看仓库信息 curl -XGET "localhost:9200/_snapshot/my_backup?pretty" # 查看所有快照 curl -XGET "localhost:9200/_snapshot/my_backup/_all?pretty" # 删除快照 curl -XDELETE "localhost:9200/_snapshot/my_backup/snapshot_2" # 查看仓库列表 curl -X GET "localhost:9200/_cat/repositories?v" # 获取所有已注册快照仓库 curl -X GET "localhost:9200/_snapshot/_all?pretty"
6. 定时快照脚本
es_store.sh#!/bin/bash# 创建快照,根据当天日期
data1=`date "+%Y%m%d"` data2="http://0.0.0.0:9200/_snapshot/my_backup/snapshot_${data1}" echo '-----------' echo $data1 echo $data2 echo '----------' curl -XPUT $data2
# 删除30天前的快照 deldata1=`date -d "30 day ago" +"%Y%m%d"` deldata2="http://0.0.0.0:9200/_snapshot/my_backup/snapshot_${deldata1}" curl -XDELETE $deldata2 echo "------------" echo $deldata1 echo $deldata2 echo "-------"
# 恢复前一天快照,先关闭索引,此处为演示,手动进行,不写进定时脚本中 curl -XPOST "http://0.0.0.0:9200/defects/_close" curl -XPOST "http://0.0.0.0:9200/alarm/_close" curl -XPOST "http://0.0.0.0:9200/issue/_close"
restore_data1=`date -d "1 day ago" +"%Y%m%d"` restore_data2="http://0.0.0.0:9200/_snapshot/my_backup/snapshot_${restore_data1}/_restore" echo '-----------' echo $restore_data1 echo $restore_data2 echo '----------' curl -XPOST $restore_data2
curl -XPOST "http://0.0.0.0:9200/defects/_open" curl -XPOST "http://0.0.0.0:9200/alarm/_open" curl -XPOST "http://0.0.0.0:9200/issue/_open"