以下脚本加入crontab -e

1

2

#每天凌晨1点执行清理es数据

0 1 * * * /data/es/rm_es.sh >> /data/es/logs//rm_es.log

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#!/bin/sh

ip='192.168.1.52'

port='9200'

#清理1个月之前的es数据

day=`date -d "1 month ago" +%Y.%m.%d`

echo ""

echo `date`

indexname=`curl -XGET -s $ip:$port/_cat/indices|awk '{print $3}'`

if [ $? -ne 0 ];then #-ne 0是如果以上命令不为真,报错就执行下面的反之执行else

    echo "Connect $ip:$port error...."

else

    echo "Connect $ip:$port Sucess,loading index ..."

fi

for in $indexname

do

   time=`echo $i|awk -F"-" '{print $NF}'|egrep "([0-9][0-9][0-9][0-9]).(0[1-9]|[1][0-2]).(0[1-9]|[1-2][0-9]|3[0-1])"`

   if [ -n "$time" ];then   #time有值且值与变量day相等的时间就执行删除

        if "$time" "$day" ];then

            echo "Delete $i"

            curl -XDELETE http://$ip:$port/$i

        fi

    fi

done

#也可以手动清理

1、curl -XGET -s 10.100.203.22:9200/_cat/indices|awk '{print $3}'  #查看所有历史索引

2、curl -XDELETE http://10.100.203.22:9200/aiflow-2021.08*   #根据需求删除aiflow索引8月的数据

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐