zoukankan      html  css  js  c++  java
  • 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} " 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} " 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

    赠人玫瑰,手有余香,如果我的文章有幸能够帮到你,麻烦帮忙点下右下角的推荐,谢谢!

    作者: imcati

    出处: https://www.cnblogs.com/imcati/>

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接

  • 相关阅读:
    [状压DP][二分]JZOJ 3521 道路覆盖
    字符串操作
    练习: 判断一个数是否为小数
    Python 深浅拷贝
    编码
    python中的 == 和 is 的区别
    Python3 字典的增删改查
    Python3 列表的基本操作
    初识 Python
    方法的入门
  • 原文地址:https://www.cnblogs.com/imcati/p/9774522.html
Copyright © 2011-2022 走看看