elasticsearch服务器定时自动清理日志索引

本文介绍了如何创建一个定时脚本来自动清理Elasticsearch服务器上的旧日志索引。通过进入Elasticsearch索引目录,使用curl调用API查看并删除索引,然后编写shell脚本实现批量模糊删除。最后,将脚本添加到Linux定时任务中,确保定期执行。
摘要由CSDN通过智能技术生成

突然想到项目中每天要生产各个环境的索引...时间一长会越来越多.

这里写一个定时自动清理elasticsearch的脚本  

 

首先.我们进入到elasticsearch的索引目录中去   发现索引数据是真多啊...

root@iZbp1ig4nql7imcw4ssn7vZ:/home/idouall# cd /opt/elasticsearch-6.4.3/data/nodes/0/indices/
root@iZbp1ig4nql7imcw4ssn7vZ:/opt/elasticsearch-6.4.3/data/nodes/0/indices# pwd
/opt/elasticsearch-6.4.3/data/nodes/0/indices
root@iZbp1ig4nql7imcw4ssn7vZ:/opt/elasticsearch-6.4.3/data/nodes/0/indices# ll
total 140
drwxrwxr-x 35 idouall idouall 4096 Feb 27 16:32 ./
drwxrwxr-x  4 idouall idouall 4096 Feb 20 21:42 ../
drwxrwxr-x  8 idouall idouall 4096 Feb 27 10:19 335HxTLoTEyVGtNBI3QnUw/
drwxrwxr-x  8 idouall idouall 4096 Feb 24 18:06 8vzxHNv3SOSYEmyleo6G-A/
drwxrwxr-x  8 idouall idouall 4096 Feb 27 10:19 93mpD1vsQ2OeiZoTVHw4WA/
drwxrwxr-x  8 idouall idouall 4096 Feb 26 15:10 9OhuYCcKRoywBY-9RcfWLg/
drwxrwxr-x  8 idouall idouall 4096 Feb 27 10:19 ahtHtEOoQ32tKLys8-PPfQ/
drwxrwxr-x  8 idouall idouall 4096 Feb 26 15:08 AmsG8zAZRv2-mxhQe6se6g/
drwxrwxr-x  8 idouall idouall 4096 Feb 25 09:15 bfhdi1FMRIOwQ1m1KtKGXA/
drwxrwxr-x  8 idouall idouall 4096 Feb 26 09:41 ChxRHY7SQsGBqbDI3q7AEg/
drwxrwxr-x  8 idouall idouall 4096 Feb 24 15:46 CKAnJBrdQd2A_gT4YW898g/
drwxrwxr-x  8 idouall idouall 4096 Feb 27 10:19 dEQDNpMlRtidwIaoZbWXuQ/
drwxrwxr-x  8 idouall idouall 4096 Feb 24 15:46 Dsct-CMWQK6Fd6mdRra-Yg/
drwxrwxr-x  8 idouall idouall 4096 Feb 26 08:00 DuoIvH0sRcmwcqFPJrUWOw/
drwxrwxr-x  8 idouall idouall 4096 Feb 27 10:19 FRHlFniRS8CprAwM-RHfqw/
drwxrwxr-x  8 idouall idouall 4096 Feb 26 16:45 IrtW2PWqSc2s1L8OSVJh0A/
drwxrwxr-x  8 idouall idouall 4096 Feb 25 09:15 JNaCO72jSrKVWCpVUowVjA/
drwxrwxr-x  8 idouall idouall 4096 Feb 24 17:25 JOONjvP2QSavERnM1oLIEA/
drwxrwxr-x  8 idouall idouall 4096 Feb 27 10:19 k3E5EExhSBKf2uR97AKVVw/
drwxrwxr-x  8 idouall ido
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于Elasticsearch定时清理过期索引,您可以使用Elasticsearch的Curator插件来实现。Curator是一个用于管理Elasticsearch索引的工具,可以方便地执行索引的删除、备份等操作。 以下是一些步骤可以用来设置定时清理过期索引的任务: 1. 安装Curator:您可以通过pip命令安装Curator: ``` pip install elasticsearch-curator ``` 2. 创建配置文件:创建一个YAML格式的配置文件,用于定义索引清理任务。例如,创建一个名为`curator_config.yml`的文件,并添加以下内容: ```yaml --- # 连接Elasticsearch的信息 client: hosts: - 127.0.0.1 port: 9200 url_prefix: use_ssl: False certificate: client_cert: client_key: ssl_no_validate: False http_auth: timeout: 30 master_only: False # 清理任务配置 actions: 1: action: delete_indices description: "Delete indices older than 30 days" filters: - filtertype: pattern kind: prefix value: your_index_prefix- - filtertype: age source: name direction: older timestring: '%Y.%m.%d' unit: days unit_count: 30 ``` 在上述配置中,`hosts`字段指定了Elasticsearch的地址和端口,`filters`字段定义了过滤器规则,这里以索引名的前缀和索引的创建时间进行过滤。 3. 创建定时任务:使用cron表达式来定义定时任务,可以在Linux系统的crontab中添加以下命令: ``` curator --config /path/to/curator_config.yml --dry-run ``` 上述命令中的`--dry-run`参数用于测试运行,可以在实际运行之前先检查将要执行的操作。 4. 运行定时任务:如果测试运行没有问题,可以将上述命令添加到crontab中,并设置合适的执行频率。例如,每天凌晨执行一次: ``` 0 0 * * * curator --config /path/to/curator_config.yml ``` 通过以上步骤,您就可以设置定时清理过期索引的任务了。请确保对配置文件和定时任务的设置进行适当调整,以满足您的需求。希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值