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. see screen shot, or I can also use curl to remove the indexes, 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. Is this possible and if so how can I do it using the date from the name of the index?
G
Just use the find command:
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.