zoukankan      html  css  js  c++  java
  • 定时删除10天前的Es索引

    说明

    主要用在索引名为 xxxx-yyyy.MM.dd 这种,可以自定义修改下边的脚本

    删除索引shell

    创建 delete_es_indices_over_10_day.sh

    #!/bin/bash
    
    ###################################
    #删除早于十天的ES集群的索引
    ###################################
    function delete_indices() {
        comp_date=`date -d "10 day ago" +"%Y-%m-%d"`
        date1="$1 00:00:00"
        date2="$comp_date 00:00:00"
    
        t1=`date -d "$date1" +%s` 
        t2=`date -d "$date2" +%s` 
    
        if [ $t1 -le $t2 ]; then
            echo "$1时间早于$comp_date,进行索引删除"
            #转换一下格式,将类似2017-10-01格式转化为2017.10.01
            format_date=`echo $1| sed 's/-/./g'`
            curl -XDELETE http://10.2.7.70:9204/*$format_date
        fi
    }
    
    curl -XGET http://10.2.7.70:9204/_cat/indices | awk -F" " '{print $3}' | awk -F"-" '{print $NF}' | egrep "[0-9]*.[0-9]*.[0-9]*" | sort | uniq  | sed 's/./-/g' | while read LINE
    do
        #调用索引删除函数
        delete_indices $LINE
    done
    

    赋执行权限 sudo chmod +x delete_es_indices_over_10_day.sh

    如果换服务器,那么请替换ip:port到自己的es服务器地址

    添加定时任务

    这里设置每天执行一次

    执行crontab -e 修改当前系统的定时任务,下边是添加这一行,每天晚11:30删除一次过期10天以上的索引

    30 23 * * * sh /home/hellxz/delete_es_indices_over_10_day.sh
    

    引用文章:
    定期删除elasticsearch集群10天以上的索引
    linux设置定时任务的方法(自己总结)

  • 相关阅读:
    luogu 2962 [USACO09NOV]灯Lights
    bzoj 1923
    bzoj 1013
    bzoj 3513
    bzoj 4259
    bzoj 4503
    CF 632E
    bzoj 3527
    bzoj 3160
    bzoj 2179
  • 原文地址:https://www.cnblogs.com/hellxz/p/11213266.html
Copyright © 2011-2022 走看看