1、软件包
文章所需软件包下载方式
2、简介
近发现elasticsearch近期索引文件大的吓人,清理了下之前的索引文件,发现服务器性能大大的减轻了一半,想一直保留近一个月的索引文件,但是又不想每个月手动清楚,在此写了一个小脚本。
3、查询索引
[root@op-es-data01 ~]# curl -XGET 'http://10.252.20.220:9200/_cat/indices/?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open h5-nginx42-2021.08.10 gmXnXXScSaK2uGZDqQJn_A 1 1 2537997 0 3.2gb 1.7gb
green open h5-nginx41-2021.08.06 7fy_DeCdRUWKiBHfysUvZA 1 1 5394978 0 6.4gb 3.2gb
green open h5-nginx61-2021.08.09 2-IKDoSYS_-dRGKJFLqSCQ 1 1 5887043 0 6.8gb 3.4gb
4、api删除
[root@op-es-data01 ~]# curl -XDELETE 'http://10.252.20.220:9200/h5-nginx42-2021.08*'清理掉了所有 8月份的索引文件
5、脚本加api删除(推荐)
[root@op-es-data01 ~]# vim root/es-index-clear.sh
#/bin/bash
#es-index-clear
#只保留5天内的日志索引
LAST_DATA=`date -d "-5 days" "+%Y.%m.%d"`
#删除上个月份所有的索引
curl -XDELETE 'http://10.252.20.220:9200/*-'${LAST_DATA}'*'
6、添加到任务计划
# 进入crontab编辑模式
[root@op-es-data01 ~]# crontab -e -u root
# 在crontab编辑模式中输入
0 1 * * * sh root/es-index-clear.sh > dev/null 2>&1
7、其他任务命令
# 查看任务计划
[root@op-es-data01 ~]# crontab -l
0 1 * * * sh /root/es-index-clear.sh
# 清空任务计划
[root@op-es-data01 ~]# crontab -r
欢迎大家扫码关注:

本公众号只写原创,不接广告、不接广告、不接广告。下期小伙伴想学习什么技术,可以私信发我吆。

文章转载自IT运维大爆炸,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




