elasticsearch 索引备份恢复

备份脚本 es_backup.sh :

#!/bin/bash
#备份昨天数据,删除30天前索引 host=`hostname`
address="[email protected]" 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

 

转载于:https://www.cnblogs.com/imcati/p/9774522.html

你可能感兴趣的:(elasticsearch 索引备份恢复)