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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值