自动清理es索引脚本

因elk每天产生大量的es索引,占用服务器的磁盘空间,所以编写一个脚本用来定时清理时间久的索引,可以使用crontab定时执行脚本。

#!/bin/bash

# Elasticsearch 服务器地址
ES_HOST="http://192.168.99.82:9200"

# 当前日期
CURRENT_DATE=$(date +%Y.%m.%d)

# 获取 10 天前的日期
OLD_DATE=$(date -d "-10 days" +%Y.%m.%d)

# 获取所有索引
#all_indices=$(curl -s "$ES_HOST/_cat/indices?h=index" | grep 'index-')
all_indices=$(curl -s "$ES_HOST/_cat/indices?h=index")

# 遍历所有索引
for index in $all_indices; do
  # 提取索引日期
  index_date=$(echo $index | grep -oE '[0-9]{4}\.[0-9]{2}\.[0-9]{2}')

  # 如果索引日期比10天前的日期早,删除索引
  if [[ $index_date < $OLD_DATE ]]; then
    echo "Deleting index: $index"
    curl -X DELETE "$ES_HOST/$index"
  fi
done

在Linux中删除Elasticsearch索引,可以使用以下步骤: 1. 创建一个脚本文件,比如delete_index_over3day.sh,并使用vim编辑器打开它:\[1\] ``` vim delete_index_over3day.sh ``` 2. 在脚本文件中添加以下内容: ``` #!/bin/bash #删除索引格式:xxxx-2020-03-25 #超过N天的索引将要删除 delete_overday=3 #当前日期 time=`date` DATA=`date -d "${delete_overday} date ago" +%Y%m%d` #删除N天前的索引 curl --user account:pwd -XDELETE "http://your_ip:9200/xxxx-${DATA}" echo "定时脚本于 【$time】 清理 【xxxx-${DATA}】 索引!" ``` 请确保替换脚本中的account、pwd、your_ip和xxxx为实际的值。 3. 保存并退出脚本文件。 4. 在Linux终端中,使用以下命令运行脚本文件: ``` bash delete_index_over3day.sh ``` 这将执行脚本文件中的命令,删除指定日期之前的Elasticsearch索引。 请注意,执行此脚本需要具有适当的权限和访问Elasticsearch的凭据。 \[1\] #### 引用[.reference_title] - *1* [Linux定时脚本删除ES索引](https://blog.csdn.net/weixin_42087894/article/details/120801453)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Linux中Elasticsearch索引的简单操作](https://blog.csdn.net/little_sloth/article/details/106928348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有人拉屋米

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值