[root@elk-01 scripts]# cat /usr/local/src/scripts/es-index-clear.sh
#/bin/bash
#es-index-clear
#只保留365天内的日志索引
LAST_DATA=`date -d "-365 days" "+%Y.%m.%d"`
for i in `cat /usr/local/src/scripts/index_name.txt`
do
#删除上个月份所有的索引
curl -XDELETE -u elastic:'123456' http://My_es_ip:9200/${i}-${LAST_DATA}
done
[root@elk-01 scripts]# cat index_name.txt
nginx-access
logs
metrics
mysql-err
mysql-slow
system-messages
system-secure
crontab -e添加定时任务:
0 1 * * * /usr/local/src/scripts/es-index-clear.sh
每天的凌晨一点清除索引。