shell之删除elasticsearch30天以前的索引

在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

转载于:https://blog.51cto.com/liuzhengwei521/2300163

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值