背景:
在3主機es集羣上進行指定索引的備份。每天備份一次,保留指定天數的備份數據。
1. 準備工作
掛載共享目錄
mount -t nfs 192.168.25.11:/home/es-backup /home/esbackup
注意:這個共享目錄,最好是跟3主機都沒關係的的存儲主機
修改掛載目錄權限
chown -R elasticsearch.elasticsearch /home/esbackup
注意:修改權限後,看一下三臺主機權限是否一致。如果有elasticsearch用戶,在不同主機上,uid,gid不同的情況,可能要修改uid,gid。
或者能保證elasticsearch用戶有共享文件夾的讀寫權限也可以
。
如果出現要修改uid,gid的情況,注意修改後,執行以下命令,否則es會啓動異常
chown -R elasticsearch.elasticsearch /etc/sysconfig/elasticsearch
chown -R elasticsearch.elasticsearch /etc/elasticsearch
chown -R elasticsearch.elasticsearch /var/lib/elasticsearch
chown -R elasticsearch.elasticsearch /var/log/elasticsearch
修改配置文件
echo "" >> /etc/elasticsearch/elasticsearch.yml
echo "### backup index ###" >> /etc/elasticsearch/elasticsearch.yml
echo 'path.repo: ["/home/esbackup"]' >> /etc/elasticsearch/elasticsearch.yml
分別重啓es服務
儘量在業務低峯期進行重啓,以免配置出錯,影響生產環境使用
systemctl restart elasticsearch && systemctl status elasticsearch
# 每臺重啓完成後注意觀察集羣狀態
# 觀察集羣節點數,重啓之後的節點是否重新加入了集羣
curl -u elastic:changme http://192.168.25.12:9200/_cat/nodes
# master節點情況
curl -u elastic:changme http://192.168.25.12:9200/_cat/master
# 集羣健康度,一定要在集羣恢復green之後再重啓另外一臺
curl -u elastic:changme http://192.168.25.12:9200/_cat/health
集羣重啓完成之後,開始進行快照備份設置
2. 配置快照備份
打開kibana的調試工具,下面的操作命令都是在調試工具中進行的
開始備份
# 查看所有倉庫
GET _snapshot
# 查看所有索引
GET /_cat/indices
# 創建備份倉庫
PUT _snapshot/esbackup
{
"type": "fs",
"settings": {
"location": "/home/esbackup"
}
}
# 備份指定索引
PUT _snapshot/esbackup/snapshot_20191202
{
"indices": "test_index"
}
# 查看備份情況
GET _snapshot/esbackup/snapshot_20191202/_status
或者:
GET _snapshot/esbackup/snapshot_20191202
快照恢復
恢復快照的時候,記得要把原來的備份的指定索引刪掉或關閉
DELETE /test_index # 刪除
POST /test_index/_close # 關閉
利用快照,重建恢復索引
POST /_snapshot/esbackup/snapshot_20191202/_restore
還有附加的選項用來重命名索引。這個選項允許你通過模式匹配索引名稱,然後通過恢復進程提供一個新名稱。如果你想在不替換現有數據的前提下,恢復老數據來驗證內容,或者做其他處理,這個選項很有用。讓我們從快照裏恢復單個索引並提供一個替換的名稱:
# 例
POST /_snapshot/esbackup/snapshot_20191202/_restore
{
"indices": "",
"rename_pattern": "logstash-other-(.+)",
"rename_replacement": "restored_index_$1"
}
POST /_snapshot/esbackup/snapshot_20191202/_restore
{
"indices": "test-index",
"rename_pattern": "test-index",
"rename_replacement": "restored_index"
}
# 這樣利用test-index快照snapshot_20191202恢復後的索引就是restored_index
查看快照恢復情況
GET _recovery
GET test_index/_recovery
刪除指定快照
DELETE _snapshot/esbackup/snapshot_20191202
3. 備份與刪除腳本
我們用腳本來每天定時進行快照備份和定期刪除
#!/bin/bash
# 執行快照備份
time=$(date "+%Y%m%d")
logfile="/tmp/esbacklogs/"${time}".log"
curl -s -u elastic:changme -XPUT "http://192.168.25.12: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://192.168.25.12: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://192.168.25.12:9200/_snapshot/esbackup/snapshot_"${delete_time} >> $logfile
參考文章:
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_restoring_from_a_snapshot.html
https://www.elastic.co/guide/cn/elasticsearch/guide/current/backing-up-your-cluster.html
https://www.elastic.co/guide/en/elasticsearch/reference/6.8/modules-snapshots.html#_shared_file_system_repository
https://www.jianshu.com/p/a06411eaad56