ES 集群清除索引 shell 脚本


ES 集群清除索引 Index 脚本,每个索引格式 "*-YYYY-mm-dd" ,输入截止Unix时间戳。


首先通过 indies 接口获取所有索引,然后遍历判断时间是否小于输入的 Unix 时间戳,是则执行删除索引。


#!/bin/bash
ip='192.168.1.1'
port=9200

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

   if [ ! $day ]
      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








你可能感兴趣的:(Linux,ELK)