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
作者:理想三旬
本人承接各种项目维护和python项目开发, 如果觉得文章写得不错,或者帮助到您了,请点个赞,加个关注哦。运维学习交流群:544692191
本文版权归作者所有,欢迎转载,如果文章有写的不足的地方,或者是写得错误的地方,请你一定要指出,因为这样不光是对我写文章的一种促进,也是一份对后面看此文章的人的责任。谢谢。