#!/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
elasticsearch删除 x天前的日志
最新推荐文章于 2025-01-03 00:15:00 发布
本文介绍了一个用于自动删除Elasticsearch中30天以前的索引的bash脚本,该脚本通过任务计划定期运行,以释放存储空间。脚本首先获取30天前的日期,并将其转换为时间戳进行比较,然后使用curl命令调用Elasticsearch API来删除符合条件的旧索引。
5265

被折叠的 条评论
为什么被折叠?



