定时删除10天的es索引脚本

定时shell脚本如下

ip='127.0.0.1'
port=9200
endUnixTime=`date -d "10 day ago" +%s`
indices=`curl -XGET "http://${ip}:${port}/_cat/indices" | awk '{print $3}'`
for index in ${indices[*]}
do
   indexTime=${index##*logs-}
   year=`echo ${indexTime} | cut -d "." -f1`
   month=`echo ${indexTime} | cut -d "." -f2`
   day=`echo ${indexTime} | cut -d "." -f3`

   if [ ! $year ]
      then
          echo "ignore index:$index"
          continue
   fi

   indexUnixTime=`date -d "$year$month$day" +%s`
   if [ $indexUnixTime -lt $endUnixTime ]
      then
         echo "Delete index:$index"
         curl -XDELETE "http://${ip}:${port}/$index"
   fi
done

你可能感兴趣的:(elk,elasticsearch,shell)