定时删除elasticsearch指定时间之前的数据(delete同步问题)

1、批量删除elasticsearch6.6.2 小于指定时间的数据

[root@zabbix:/root/elastic]$ cat del_hz.sh 
curl -u elastic:***** -XPOST "http://111.111.111.111:9201/Indexname/_delete_by_query" -H 'Content-Type: application/json' -d'        
{
  "query": {
    "range": {
      "@timestamp": {
"lt":"2019-04-02T13:52:01.000Z"
      }
    }
  }
}'

2、定时修改上面的脚本为指定时间

[root@zabbix:/root/elastic]$ cat modify_del.sh 
#一个小时之前(因为同步一次要30分钟,所有清理一个小时之前的数据,具体自己玩)
deltime=`date -d '-1 hours' +"%Y-%m-%d"T"%H:%M:%S".000Z`
#删除上面脚本的第6行数据
sed -i '6d' /root/elastic/del_hz.sh
#插入一行数据到上面的第6行之前
sed -i "6i\"lt\":\"$deltime\"" /root/elastic/del_hz.sh
休息下,跑删除的脚本
sleep 10
sh /root/elastic/del_hz.sh

3、定时任务

* */1 * * * sh /root/elastic/modify_del.sh  >/root/elastic/del.log 2>&1

猜你喜欢

转载自blog.csdn.net/qq_43130832/article/details/88972538