elasticsearch 索引備份恢復


備份腳本 es_backup.sh :

#!/bin/bash
#備份昨天數據,刪除30天前索引 host=`hostname`
address="xxx@xxx.com" es_user=$1 es_passwd=$2 #獲取昨天日期(備份使用) date_yesterday=`date -d "-1 day" +%Y.%m.%d` #獲取當前時間戳 date_now=`date +%s` #獲取一個月前的日期 date_month_ago=`date -d@$[ $date_now - 2592000 ] "+%Y.%m.%d"` for i in txxxx zhaoxxx #指定備份索引 do #判斷倉庫是否存在,不存在則創建 code=`curl -XGET -u$es_user:$es_passwd -s -w "%{http_code}\n" http://127.0.0.1:9200/_snapshot/"$i" -o /dev/null -I` [ ! -d /S3/elasticsearch/"$i" ] && /usr/bin/sudo mkdir /S3/elasticsearch/"$i" && /usr/bin/sudo chown -R elasticsearch.elasticsearch /S3/elasticsearch/ if [ "$code" -ne 200 ];then curl -XPUT -u$es_user:$es_passwd http://127.0.0.1:9200/_snapshot/"$i" -d ' { "type": "fs", "settings": { "location": "/S3/elasticsearch/'$i'" } } ' [ $? -eq 0 ]&& echo "創建倉庫: $i" else echo "倉庫:$i 已存在!" fi #備份昨天數據 curl -XPUT -u$es_user:$es_passwd http://127.0.0.1:9200/_snapshot/"$i"/"$i"-"$date_yesterday"?wait_for_completion=true -d ' { "indices": "'$i'-'$date_yesterday'" }' [ $? -ne 0 ]&& echo "$time $host $i-$date_yesterday backup failed!" |mail -s "ES Backup Information" $address
#刪除上個月當天的數據 curl -XDELETE -u$es_user:$es_passwd http://127.0.0.1:9200/"$i"-"$date_month_ago"|| echo "上個月前一天的數據不存在!" done

 

恢復腳本 es_restore.sh:

#!/bin/bash
#恢復指定時間段內索引數據 es_user=$1 es_passwd=$2 while : do #讀取用戶輸入 read -p "請輸入你想要恢復的服務(eg: xxxxx, q 退出): " service #指定恢復數據的索引名 echo "輸入為: $service " if [ "$service" = "q" ];then exit fi while : do read -p "請輸入開始的日期(eg: 2018-05-01, q 返回上一級):" start_date if [ "$start_date" = "q" ];then break fi read -p "請輸入結束的日期(eg: 2018-05-30, q 返回上一級):" end_date if [ "$end_date" = "q" ];then break fi start_date_toSecond=`date -d $start_date +%s` end_date_toSecond=`date -d $end_date +%s` #收集所有日期 function gen_date { index=0 while [ "$start_date_toSecond" -le "$end_date_toSecond" ] do curr_date=`date -d@$start_date_toSecond +%Y.%m.%d` date_arr[index]=$curr_date start_date_toSecond=$[ $start_date_toSecond+86400 ] let index++ done } gen_date $start_date $end_date n=0 while [ "$n" -lt "${#date_arr[@]}" ] do echo ${date_arr[$n]}     code=`curl -XPOST  -u$es_user:$es_passwd  -s -w "%{http_code}\n" http://127.0.0.1:9200/_snapshot/"$service"/"$service"-${date_arr[$n]}/_restore?wait_for_completion=true  -d  '{"ignore_unavailable": "true", "include_global_state": false ,"index_settings": { "index.number_of_replicas": 0 }}' -o /dev/null` let n++ echo "$code" if [ "$code" -eq 200 ];then echo "$service_${date_arr[$n]} 導入成功" else echo "$service_${date_arr[$n]} 導入失敗" fi done done done

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM