通过crontab定时删除ES历史数据

ES定期删除2天前的数据:
1、进入脚本目录

cd  /install/timershell/

2、编辑删除脚本delete_es_data_2days_ago.sh

vim delete_es_data_2days_ago.sh

3、编写删除脚本

#!/bin/bash

###################################

###################################
function delete_indices() {
    comp_date=`date -d "2 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
        format_date=`echo $1| sed 's/-/\./g'`

        curl -XDELETE http://localhost:9200/*$format_date
    fi
}

curl -XGET http://localhost:9200/_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
~         

4、赋予执行权限

chmod 777 delete_es_data_2days_ago.sh

5、定时执行
[1] 进入 /etc/目录

cd /etc/

[2] 编辑

crontab -e 

[3] 添加以下代码

0 0 * * * /bin/bash /appcom/install/timershell/es_delete_2day_ago.sh

[4] 重启定时

sudo service crond restart

出现如下信息则说明重启成功:

[app@centos etc]$ sudo service crond restart
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值