elasticsearch批量删除索引

查看索引

        在elasticsearch节点上使用curl -XGET 'http://XX.XX.X.XX:9200/_cat/shards'查看索引

[root@192-168-x-x scripts]# curl -XGET 'http://192.168.x.x:9200/_cat/shards' | more
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  0   0   0  0   0   0   0 --:--:-- --:--:-- --:--:--     0freetrip-2019.01.29   2 r STARTED   34  59kb 192.168.x.x node3-192.168.x.x
freetrip-2019.01.29                2 p STARTED     34    59kb 192.168.x.x node1-192.168.x.x
freetrip-2019.01.29                1 p STARTED     47 122.7kb 192.168.x.x node3-192.168.x.x
freetrip-2019.01.29                1 r STARTED     47 122.7kb 192.168.x.x node1-192.168.x.x
freetrip-2019.01.29                3 p STARTED     58  90.5kb 192.168.x.x node2-192.168.x.x
freetrip-2019.01.29                3 r STARTED     58  90.5kb 192.168.x.x node1-192.168.x.x
freetrip-2019.01.29                4 r STARTED     41  45.5kb 192.168.x.x node2-192.168.x.x
freetrip-2019.01.29                4 p STARTED     41  45.5kb 192.168.x.x node3-192.168.x.x
freetrip-2019.01.29                0 p STARTED     36  97.9kb 192.168.x.x node2-192.168.x.x
freetrip-2019.01.29                0 r STARTED     36  97.9kb 192.168.x.x node3-192.168.x.x

过滤索引

        将过滤出来要删除的索引存到临时文件夹里面(过滤条件根据实际情况修改)

[root@192-168-x-x ~]# curl -XGET 'http://192.168.x.x:9200/_cat/shards' | grep 192.168.x.x | grep 2018 | awk '{print $1}' |uniq > elk-index.tmp
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 81832  100 81832    0     0   142k      0 --:--:-- --:--:-- --:--:--  142k
[root@192-168-3-163 ~]# cat elk-index.tmp 
otter-canal-195-2018.12.10
logstash-nginx-chm-accesslog-2018.11.20
logstash-nginx-oms-accesslog-2018.10.14
laravel-chm248-online-2018.12.10
otter-canal-195-2018.11.20
logstash-nginx-oms-accesslog-2018.11.20
otter-communication-195-2018.11.23
otter-communication-195-2018.12.10
otter-node-195-2018.12.10

批量删除



for i in `cat elk-index.tmp`
do 
      curl -XDELETE  http://192.168.3.163:9200/$i 
done

定时删除

        使用脚本加定时任务,每一天删除前三天的索引

[root@192-168-x-x ~]# cat /home/scripts/del_elasticseatch_index.sh 
#!/bin/bash
curl -XGET 'http://192.168.x.x:9200/_cat/shards' |grep 192.168.x.x | awk '{print $1}' |grep `date -d "5 days ago" +%Y.%m.%d` | uniq > /tmp/index_name.tmp

for index_name in `cat /tmp/index_name.tmp`
do
   echo $index_name
    curl -XDELETE  http://192.168.x.x:9200/$index_name
    echo "${index_name} delete success" >> /home/scripts/del_elasticseatch_index.log
done

定时任务

[root@192-168-x-x ~]# crontab -l
0 3 * * * bash /home/scripts/del_elasticseatch_index.sh

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值