索引数据备份

在磁盘创建备份目录并授权

# 创建备份目录/home/esbackup# 授权chmod 777 /home/esbackup

修改配置文件elasticsearch.yml

echo 'path.repo: ["/home/esbackup"]' >> /etc/elasticsearch/elasticsearch.yml

重启elasticsearch(我是docker创建的,所以上述两条命令在docker中执行,可提前挂载)

docker restart elasticsearch

打开kibana控制台-开发者选项

新建备份仓库

PUT _snapshot/esbackup {"type": "fs", "settings": {"location": "/home/esbackup" }}

查看备份仓库

GET _snapshot

备份置顶索引

snapshot_20230223这个名随便起,一般以时间为名

# 备份指定索引PUT _snapshot/esbackup/snapshot_20230223{"indices": "索引名"}

查看备份情况

# 查看备份情况GET _snapshot/esbackup/snapshot_20230223/_statusGET _snapshot/esbackup/snapshot_20230223

成功标志

索引数据恢复

删除之前的索引

DELETE /索引名

从备份过的快照恢复

POST /_snapshot/esbackup/snapshot_20230223/_restore

查看恢复状态,状态码返回200即可

GET _recoveryGET 索引名/_recovery

删除指定快照

DELETE _snapshot/esbackup/快照名

每日备份的shell脚本,供参考

#!/bin/bash# 执行快照备份time=$(date "+%Y%m%d")logfile="/tmp/esbacklogs/"${time}".log"curl -s -u elastic:changme -XPUT "http://127.0.0.1:9200/_snapshot/esbackup/snapshot_"${time} -H 'Content-Type: application/json' -d'{"indices": "test_index, test_index2"}'# 快照备份需要一定时间,60s后检查备份情况sleep 60echo ""curl -s -u elastic:changme -XGET "http://127.0.0.1:9200/_snapshot/esbackup/snapshot_"${time} | grep "SUCCESS" >> $logfileif [ $" />-eq 0 ]thenecho "backup elasticsearch success" >> $logfileelseecho "backup elasticsearch fail" >> $logfilefiecho ""# 删除10天前的快照delete_time=$(date "+%Y%m%d" -d " -10 day")curl -s -u elastic:changme -XDELETE "http://127.0.0.1:9200/_snapshot/esbackup/snapshot_"${delete_time} >> $logfile