forked from dariommr/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reindex.sh
167 lines (157 loc) · 4.77 KB
/
reindex.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#GETTING ARGUMENTS
usage() {
echo "USAGE: reindex.sh (-a|--all yes | -f|--file /path/file) -s|--server https://address:port -u|--user user -p|--pass password"
echo " -f or --file Specify the path of the file that contains the list of Indices to reindex. No puede ser utilizado con -a"
echo " -a or --all Specify if you need to reindex all the indices. No puede ser utilizado con -f"
echo " -s or --server Specify the URL of the Elasticsearch server with protocol and port"
echo " -u or --user Specify the administrator user of Elasticsearch"
echo " -p or --pass Specify the password of the administratior user of Elasticsearch"
echo " -h or --help Display this help"
}
if [[ $# -eq 0 ]] ; then
usage
exit
fi
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f|--file)
FILE="$2"
shift # past argument
shift # past value
;;
-a|--all)
ALL="$2"
shift # past argument
shift # past value
;;
-s|--server)
SERVER="$2"
shift # past argument
shift # past value
;;
-u|--user)
USER="$2"
shift # past argument
shift # past value
;;
-p|--pass)
PASS="$2"
shift # past argument
shift # past value
;;
-h|--help)
HELP="yes"
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ $HELP == "yes" ]] || [[ -z $USER ]] || [[ -z $PASS ]] || [[ -z $SERVER ]]; then
usage
exit
fi
if [[ ! -z $ALL ]] && [[ ! -z $FILE ]] ; then
usage
exit
fi
if [ $ALL == "yes" ] ; then
echo -e "\nThe -a|--all feature is not available yet\n"
usage
exit
fi
#REINDEX FUNCTION
reindex() {
ENDPR="/_reindex?wait_for_completion=false"
HEAD='Content-Type: application/json'
PART_QRY='{"source": {"index": "%s"},"dest": {"index": "%s"}}'
FULL_QRY=$(printf "$PART_QRY" "$INDEX" "$INDEX_BKP")
curl -s -u $USER:$PASS -k -X POST $SERVER$ENDPR -H "$HEAD" -d "$FULL_QRY" >>/tmp/tasks.json
TMP=$(tail -n 1 /tmp/tasks.json)
echo -e -n "\n" >>/tmp/tasks.json
RES=$(echo $TMP | sed -e 's/{//g' | sed -e 's/}//g' | sed -e 's/"//g' | sed -e 's/task://g')
}
#GET TASK STATUS FUNCTION
task_state() {
TSK=$(echo "$LINE" | awk -F ',' '{print $2}')
ENDPT="/_tasks/$TSK"
if [[ $(curl -s -u $USER:$PASS -k -X GET $SERVER$ENDPT) == *'"completed":true'* ]]; then STATUS="Completed"; else STATUS="Not Completed"; fi
}
#====== MAIN PROGRAM ========
echo "PHASE [1] IN PROGRESS"
echo "Reindex to temporary indices"
while IFS= read -r INDEX; do
INDEX_BKP=$INDEX"-bkp"
reindex
echo $INDEX","$RES >>/tmp/tasks.lst
echo "Reindexing $INDEX to $INDEX_BKP"
done < $FILE
#GET TASKS STATUS, WHEN COMPLETED, REINDEX AGAIN
TIMEOUT=3600 #TIMEOUT UNTIL THE STATUS CHECK THROWS AN ERROR
echo -e "\nPHASE [2] IN PROGRESS"
echo "Reindex to final indices"
while IFS= read -r LINE; do
readarray -d , -t strarr <<<"$LINE"
IX="${strarr[0]}"
TSK="${strarr[1]}"
STATUS="Not Completed"
#WAIT TASK FOR COMPLETION
SECONDS=0
while [ "$STATUS" == "Not Completed" ] && [ $SECONDS -lt $TIMEOUT ]
do
task_state
sleep 5
done
echo -e "\nReindex to $IX-bkp is $STATUS"
if [[ $STATUS == "Not Completed" ]]; then
echo "[ERROR] The reindex of $$IX-bkp was Not Completed (timeout reached) and you need to check the Status of the task $TSK and continue manually"
continue
fi
#DELETE THE ORIGINAL INDEX
echo "Deleting the Index $IX"
if [[ $(curl -s -u $USER:$PASS -k -X DELETE $SERVER"/"$IX) == *'"acknowledged":true'* ]]; then DEL="Completed"; else DEL="Failed"; fi
sleep 10
echo "Delete $DEL"
INDEX=$IX"-bkp"
INDEX_BKP=$IX
reindex
echo $INDEX","$RES >>/tmp/tasksp2.lst
echo "Reindex to $IX is In Progress"
done < /tmp/tasks.lst
echo -e "\nPHASE [3] IN PROGRESS"
echo "Cleaning temporary data"
while IFS= read -r LINE; do
readarray -d , -t strarr <<<"$LINE"
IX="${strarr[0]}"
TSK="${strarr[1]}"
STATUS="Not Completed"
#WAIT TASK FOR COMPLETION
SECONDS=0
while [ "$STATUS" == "Not Completed" ] && [ $SECONDS -lt $TIMEOUT ]
do
task_state
sleep 5
done
echo "Reindex to $IX-bkp is $STATUS"
if [[ $STATUS == "Not Completed" ]]; then
echo "[ERROR] The reindex of $$IX-bkp was Not Completed (timeout reached) and you need to check the Status of the task $TSK and continue manually"
continue
fi
#DELETE THE ORIGINAL INDEX
echo "Deleting the Index $IX"
if [[ $(curl -s -u $USER:$PASS -k -X DELETE $SERVER"/"$IX) == *'"acknowledged":true'* ]]; then DEL="Completed"; else DEL="Failed"; fi
sleep 10
echo "Delete $DEL"
done < /tmp/tasksp2.lst
echo -e "\nRemoving temporary files"
#REMOVE TEMP FILES
rm -rf /tmp/tasks.json
rm -rf /tmp/tasks.lst
rm -rf /tmp/tasksp2.lst
echo -e "\nFINISHED\n"