#!/bin/bash #经过任务计划自动删除es中30天之前的索引,以释放空间 ES_HOST='127.0.0.1'; ES_PORT=9200; HTTP_PREFIX=http://${ES_HOST}:${ES_PORT}/ 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_PREFIX}/_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