定时删除ES索引shell脚本

#!/bin/bash
######################################################
# Version:     v1.0
# Function:    clean es log index
# Author:      lzy
# CreateDate: 2021-03-12
# Description: 定期删除ES日志索引
######################################################
# 执行过程日志文件
CLEAN_LOG="/work/bin/clean_es_index.log"
# 索引前缀
INDEX_PRFIX="btb-"
# es 请求地址
SERVER_PORT="http://10.68.10.92:9200"
#查出es所有索引列表
INDEXS=$(curl -s "${SERVER_PORT}/_cat/indices?v" |grep "${INDEX_PRFIX}"|awk '{print $3}')
# 指定删除多少天以前的索引
DELTIME=10
# 当前时间戳
SECONDS=$(date -d  "$(date  +%F) -${DELTIME} days" +%s)
if [ ! -f  "${CLEAN_LOG}" ]; then
  touch "${CLEAN_LOG}"
fi
echo "----------------------------clean time is $(date +%Y-%m-%d_%H:%M:%S) ------------------------------" >>${CLEAN_LOG}
for del_index in ${INDEXS}
do
  del_index_date=$(echo ${del_index} | awk -F "-" '{print $NF}')
  indexDate=$(echo ${del_index_date} | cut -d '.' -f 1)-$(echo ${del_index_date} | cut -d '.' -f 2)-$(echo ${del_index_date} | cut -d '.' -f 3)
  indexSecond=$( date -d ${indexDate} +%s )
  if [ $(( $SECONDS - $indexSecond )) -gt 0 ]; then
      echo "${del_index}" >> ${CLEAN_LOG}
      delResult=`curl -s  -XDELETE "${SERVER_PORT}/"${del_index}"?pretty" |sed -n '2p'`
      echo "clean time is $(date)" >>${CLEAN_LOG}
      echo "delResult is ${delResult}" >>${CLEAN_LOG}
  fi
done

上一篇:Clean Code - 注释


下一篇:快速入门makefile