通过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  ]

你可能感兴趣的:(大数据,操作)