Crontab命令定时执行shell脚本删除ES索引

这里实现描述一个crontab的实际应用的例子:如何定时删除es集群的索引?

一、准备工作:

1、找一个linux服务器,确保上面安装了Crontab,以及会使用crontab命令,如何安装和使用请看我的另一篇文章:

http://www.cnblogs.com/wynjauu/articles/9370948.html

2、elasticsearch的curator知识请看另一篇文章:

http://www.cnblogs.com/wynjauu/articles/9372659.html

http://www.elastic.co/guide/en/elasticsearch/client/curator/5.5/filtertype_age.html

https://www.jianshu.com/p/7975fa063d99

二、实现

我用的是5.5.2版本的es集群,首先在/opt/software/curator/shell/es5目录下面制作了一个.sh文件,里面是要执行的脚本。

脚本的具体内容是:

#!/bin/bash
#curator_cli --host 10.37.149.88 --port 9600 --timeout 150 --logfile /opt/software/curator/logs/show_indices_appserver.log show_indices --filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":1},{"filtertype":"pattern","kind":"prefix","value":"dsa"},{"filtertype":"pattern","kind":"regex","value":"dsa-webserver-201[6,7]-0[4,6,7]-0[4,5,7,8]-*","exclude": "True"}]'
echo "---------------------------------start-delete-indices--------------------------------------------"
curator_cli --host 10.x.x.171 --port 9600 --timeout 150 --logfile /opt/software/curator/logs/delete_indices_es5bpm.log delete_indices --filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"weeks","unit_count":8},{"filtertype":"pattern","kind":"prefix","value":"ctbpm"}]'

 

简单说明一下:在10.x.x.171需要删除以ctbpm开头的距离当前时间8周外的索引(即保存8周的索引),执行的日志打印在:

/opt/software/curator/logs/delete_indices_es5bpm.log

然后:通过# crontab -e命令,再使用在Esc+i(vi编译的命令)添加定时执行的记录:

*/5 * * * * /opt/software/curator/shell/es5/delete_indices_es5bpm.sh

这里我是测试而已,用的是每五分钟执行一次。也可以执行./delete_indices_estbpm.sh立即执行,注意在执行./delete_indices_estbpm.sh命令是

要是提示linux perminssion denied 权限问题时,执行:chmod 777 delete_indices_estbpm.sh可配置权限。

然后可以通过head插件查看es的索引可以被定时删除了。

三、其他实例

网上另一个博客也是es+crontab+linux做定时的运用:https://www.cnblogs.com/java-zhao/p/5900590.html

本文版权归作者所有,欢迎转载,请在文章页面明显位置给出原文连接:https://www.cnblogs.com/wynjauu/articles/9372621.html

 

posted @ 2018-07-26 16:35  舞羊  阅读(491)  评论(0编辑  收藏  举报