Created
November 25, 2018 16:16
-
-
Save tangoslee/4aff349945617b08cd74513dbfdb261e to your computer and use it in GitHub Desktop.
Delete elasticsearch indexes leave top N
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/bin/sh | |
# This scripts deletes expired log-yyyy.mm.dd indexes excepts top X | |
# | |
. /etc/profile | |
# LOG | |
LOGDIR=$HOME/logs | |
LOG=$LOGDIR/delete.log | |
# Elasticsearch host | |
EL_HOST=http://localhost:9200 | |
# Top x indexes will be preserved. | |
TOP=10 | |
DATETIME=$(date +%Y-%m-%dT%T%z) | |
[ -d $LOGDIR ] || mkdir -p $LOGDIR | |
[ "$1" = "show" ] && { | |
curl -s -XGET $EL_HOST/_cat/indices | grep 'log-' | sort -r | |
exit; | |
} | |
# search expired indexes | |
indexes=$(curl -s -XGET $EL_HOST/_cat/indices | grep 'log-' | sort -r | tail -n+$((${TOP}+1)) | awk '{ print $3 }') | |
# delete index | |
for idx in $indexes | |
do | |
[ "$idx" != "" ] && { | |
result=$(curl -s -XDELETE $EL_HOST/$idx) | |
echo "$DATETIME $idx $result" | |
} | |
done >> $LOG | |
#{"acknowledged":true} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment