-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_cleaner
executable file
·54 lines (48 loc) · 1.39 KB
/
index_cleaner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/ksh
##Potaje6
##Lazy script to delete indexes of elasticsearch older than X days
##ToDo add some testing to make sure that numeric vars are numeric and anyd dry-run mode
#curl -X GET 'grasa:9200/_cat/indices?v'| grep i77-* | sort -r | awk '{ print $3 }'
#date +%Y%m%d
#curl -X GET 'grasa:9200/_cat/indices?v'| grep i77-* | sort -r | awk '{ print $3 }' | sed 's/i77-//g' | tr -d \.
THRESHOLD=90
ELASTIHOST=grasa
EPOCHDAYS=$(date -d "$THRESHOLD days ago" +%s)
PREFIX=grasa-
print_uso(){
cat <<EOF
Script para hacer backups
-d threshold of days, indexes with date in the name older than that amout will be deleted. Default $THRESHOLD
-h elasticsearch host to work with. Default $ELASTIHOST
-p pattern of the indexes for example grasa-2019/04/30, pattern will be "grasa-" with the hyphen too. Default $PREFIX
EOF
}
##Get in, we're France!
while [ $# -gt 0 ];
do
case $1 in
"-d") THRESHOLD=$2
shift
;;
"-h") ELASTIHOST="$2"
shift
;;
"-p") PREFIX=$2
shift
;;
"--help") print_uso
exit
;;
*)
shift
esac
done
curl -X GET "$ELASTIHOST:9200/_cat/indices?v" 2>/dev/null |\
grep $PREFIX* |\
sort -r |\
awk '{ print $3 }' |\
while read INDEX ; do INDEX_DATE=$(echo $INDEX|sed "s/$PREFIX//g" | tr \. /) EPOCHINDEX=$(date -d "$INDEX_DATE" +%s)
if [ $EPOCHDAYS -gt $EPOCHINDEX ]; then
curl -X DELETE "http://$ELASTIHOST:9200/$INDEX"
fi
; done