索引数据备份
在磁盘创建备份目录并授权
# 创建备份目录
/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/_status
GET _snapshot/esbackup/snapshot_20230223
成功标志
索引数据恢复
删除之前的索引
DELETE /索引名
从备份过的快照恢复
POST /_snapshot/esbackup/snapshot_20230223/_restore
查看恢复状态,状态码返回200即可
GET _recovery
GET 索引名/_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 60
echo ""
curl -s -u elastic:changme -XGET "http://127.0.0.1:9200/_snapshot/esbackup/snapshot_"${time} | grep "SUCCESS" >> $logfile
if [ $? -eq 0 ]
then
echo "backup elasticsearch success" >> $logfile
else
echo "backup elasticsearch fail" >> $logfile
fi
echo ""
# 删除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