您的位置:首页 > 运维架构 > Shell

shell之删除elasticsearch30天以前的索引

2018-10-15 16:34 423 查看

在elasticsearch的运维工作中,由于es每天会产生大量的日志,如果一直保存不进行删除的话,再大的磁盘空间也会不够用,由此需要删除满足条件的index,从而释放磁盘空间;
我们公司的es要求只保留30天的日志即可,超出30天的index则自动进行删除;
es-index-delete-30days-ago.sh

#!/bin/bash
##############################################
#Author: Liuzhengwei - 1135960569@qq.com
#QQ:1135960569
#Last modified: 2018-10-15 16:29
#Filename:es-index-delete-30days-ago
#Description: 通过任务计划自动删除es中30天以前的索引,以释放空间
##############################################
source /etc/profile
#定义删除30天以前的函数
delete_indices(){
check_day=`date -d '-30 days' '+%F'`
index_day=$1
#将日期转换为时间戳
check_day_timestamp=`date -d "$check_day" +%s`
index_day_timestamp=`date -d "$index_day" +%s`
#当索引的时间戳值小于当前日期30天前的时间戳时,删除此索引
if [ ${index_day_timestamp} -lt ${check_day_timestamp} ];then
#转换日期格式
format_date=`echo $1 | sed 's/-/\./g'`
curl -XDELETE http://10.78.1.184:9200/*$format_date
fi
}

curl -XGET http://10.78.1.184: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

任务计划,每天执行一次:

0 2 * * * /server/scripts/es-index-delete-30days-ago.sh &> /dev/null
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自动清理 es