码迷,mamicode.com
首页 > 系统相关 > 详细

shell之删除elasticsearch30天以前的索引

时间:2018-10-15 20:45:48      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:条件   删除   空间   转换   read   超出   desc   也会   time   

在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

shell之删除elasticsearch30天以前的索引

标签:条件   删除   空间   转换   read   超出   desc   也会   time   

原文地址:http://blog.51cto.com/liuzhengwei521/2300163

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!