简体   繁体   English

使用bash脚本删除logstash索引

[英]Remove logstash Index/s using a bash script

I am looking for a way to remove old Logstash indexes using a script, my logstash indexs are named logstash-2016.02.29, logstash-2016.03.01 ... at the moment I use an extention in chrome called Sense to remove the indexes. 我正在寻找一种使用脚本删除旧Logstash索引的方法,我的logstash索引分别命名为logstash-2016.02.29, logstash-2016.03.01 ...目前,我在Chrome中使用一种称为Sense的扩展来删除索引。 see screen shot, or I can also use curl to remove the indexes, curl -XDELETE 'http://myIpAddress:9200/logstash-2016.02.29' 请参阅屏幕截图,或者我也可以使用curl删除索引, curl -XDELETE 'http://myIpAddress:9200/logstash-2016.02.29'

在此处输入图片说明

I would like to write a script that would run daily and remove logstash index older than 2 weeks from Elasticsearch. 我想编写一个脚本,该脚本每天运行一次,并从Elasticsearch中删除超过2周的logstash索引。 Is this possible and if so how can I do it using the date from the name of the index? 这是可能的,如果可以的话,如何使用索引名称中的日期呢?

G G

Just use the find command: 只需使用find命令:

find . logstash* -mtime +14 -type f -delete

This searches in the current directory and below, for all files whose name starts with "logstash", that are older than 14 days, and then deletes them. 它将在当前目录中及其下方搜索名称以“ logstash”开头的所有文件(超过14天),然后将其删除。

If the file times are totally unreliable, and you have to use the filenames, try something like this: 如果文件时间完全不可靠,并且您必须使用文件名,请尝试以下操作:

#!/bin/bash
testdate=$(date -d '14 days ago' '+%Y%m%d')
for f in ./logstash-[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]; do
    dt=$(basename "${f//.}")
    dt=${dt#logstash-}
    [ $dt -le $testdate ] && rm -f "$f"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM