该版本对于上一个版本做了升级。兼容性更好了。
#!/bin/bash
#######################################################
# $Name: clean_index.sh
# $Version: v2.0
# $Function: delete es index
# $Author: dongjiaxiao
# $Create Date: 2019-12-31
# $Description: shell
######################################################
#脚本的日志文件路径
CLEAN_LOG="/home/djx/scripts/clean_index.log"
#es地址
SERVER_PORT=172.17.1.212:9200
#保留的时间,单位天
DELTIME=45
SECONDS=$(date -d "$(date +%F) -${DELTIME} days" +%s)
INDEXS=$(curl -s -u elastic:changeme "${SERVER_PORT}/_cat/indices?v"|awk '{print $3}')
echo "-------------------$(date +%F_%T)---------------" >>${CLEAN_LOG}
for del_index in ${INDEXS}
do
timeString=$( echo ${del_index} |awk -F"-" '{print $NF}'|egrep "[0-9]*\.[0-9]*\.[0-9]*")
if [ -n "$timeString" ]
then
indexDate=${timeString//./-}
indexSecond=$( date -d ${indexDate} +%s )
if [ $(( $SECONDS- $indexSecond )) -gt 0 ]
then
delResult=`curl -s -u elastic:changeme -XDELETE "${SERVER_PORT}/"${del_index}"?pretty" |sed -n '2p'`
echo "delete index:$del_index result:$delResult" >>${CLEAN_LOG}
fi
fi
done