Fork me on GitHub

Elasticsearch定时删除索引第二版

该版本对于上一个版本做了升级。兼容性更好了。

#!/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 

小更新

#!/bin/bash
#######################################################
# $Name:        clean_index.sh
# $Version:     v2.0
# $Function:    delete es index
# $Author:      dongjiaxiao
# $Create Date: 2023-2-2
# $Description: shell
######################################################
#脚本的日志文件路径
CLEAN_LOG="/opt/scripts/clean_index.log"
#es地址
SERVER_PORT=127.0.0.1:9200
USER="elastic"
PASSWORD=""
#保留的时间,单位天
DELTIME=60
SECONDS=$(date -d  "$(date  +%F) -${DELTIME} days" +%s)
INDEXS=$(curl -s -u  ${USER}:${PASSWORD}  "${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 ${USER}:${PASSWORD}  -XDELETE "${SERVER_PORT}/"${del_index}"?pretty" |sed -n '2p'`
             echo "delete index:$del_index result:$delResult" >>${CLEAN_LOG}
         fi
     fi
done 
posted @ 2020-01-01 22:49  自由早晚乱余生  阅读(1925)  评论(1编辑  收藏  举报