This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
forked from sfrek/es_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshots.sh
executable file
·202 lines (191 loc) · 6.53 KB
/
snapshots.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
ACTION=${1}
REPONAME=${2}
SNAPSHOT=${3}
HOST=$(netstat -ltpn | grep 9200 | awk '{print $4}' | awk -F':' '{print $1}')
[ -z ${HOST} ] && HOST=localhost
APP=$(basename $0)
APP=${APP%.sh}
ls_snapshot_indexes(){
local SNAPSHOT=${1}
curl -s -XGET http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT} | jq -r ' .snapshots | .[] | .indices | .[] ' | sort
}
ls_snapshots(){
curl -s -XGET "http://${HOST}:9200/_snapshot/${REPONAME}/_all" | jq -r ' .snapshots | .[] | .snapshot' | sort
}
ls_repos() {
curl -s -XGET "http://${HOST}:9200/_snapshot" | jq -r 'keys | .[]'
}
close_index(){
local INDEX=${1}
NODES=$( curl -s -XGET 192.168.5.13:9200/_nodes | \
jq -r '.nodes | .[] | .http | .publish_address' | \
grep -v 127.0.0.1 | sed -r 's:inet\[\/(.*)\]:\1:' )
for NODE in ${NODES}
do
echo -en "\t\e[01;46m${NODE}\e[00m\t"
curl -s -XPOST "http://${NODE}/${INDEX}/_close"
echo -e "\e[00m"
done
}
index_cat_info(){
local INDEX=${1}
STATUS=$( curl -s -o /dev/null -w "%{http_code}" -XGET "http://${HOST}:9200/_cat/indices/${INDEX}" )
if [ ${STATUS} -eq "200" ]
then
curl -s -XGET "http://${HOST}:9200/_cat/indices/${INDEX}?h=s,h,dc,ss"
else
echo "missing"
fi
}
[ ! -d ${REPONAME} ] && mkdir -p ${REPONAME}
case ${ACTION} in
ls)
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${SNAPSHOT} \e[01;37m]\e[00m"
[ ! -z ${SNAPSHOT} ] && \
ls_snapshot_indexes ${SNAPSHOT}
echo -e "\e[00m"
;;
compare)
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${SNAPSHOT} \e[01;37m]\e[00m"
if [ ! -z ${SNAPSHOT} ]
then
for INDEX in $(ls_snapshot_indexes ${SNAPSHOT})
do
echo -en "\t\e[01;37m[\e[01;36m ${INDEX} \e[01;37m] \e[01;35m"
index_cat_info ${INDEX}
done
fi
echo -e "\e[00m"
;;
get|GET)
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION}"
for SNAPSHOT in $(ls_snapshots)
do
echo -e "\t\e[01;37m[\e[01;36m ${SNAPSHOT} \e[01;37m]\e[00m"
[[ "${3}" == "all" ]] && \
ls_snapshot_indexes ${SNAPSHOT} | xargs -i echo -en "\t\t\e[01;35m"{}"\e[00m\n"
done
echo -e "\e[00m"
;;
delete|DELETE)
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${SNAPSHOT} \e[01;37m]\e[00m"
[ ! -z ${SNAPSHOT} ] && \
curl -s -XDELETE "http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT}"
echo -e "\e[00m"
;;
get_states)
if [ ! -z "${REPONAME}" ]
then
if [ -z "${SNAPSHOT}" ]
then
curl -s -XGET "http://${HOST}:9200/_snapshot/${REPONAME}/_all" | jq " .[] | .[] | [ .snapshot, .state ]"
else
CURL_STATUS=$( curl -s -XGET -w %{http_code} -o ${REPONAME}/${SNAPSHOT}.json "http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT}" )
JQ_STATE=$( cat ${REPONAME}/${SNAPSHOT}.json | jq -r " .[] | .[] | .state " 2>> /dev/null )
fi
fi
echo ${JQ_STATE}
;;
state|STATE)
if [ ! -z "${REPONAME}" ]
then
[ ! -d ${REPONAME} ] && mkdir -p ${REPONAME}
if [ -z "${SNAPSHOT}" ]
then
curl -s -XGET "http://${HOST}:9200/_snapshot/${REPONAME}/_all" | jq -r " .[] | .[] | .state"
else
CURL_STATUS=$( curl -s -XGET -w %{http_code} -o ${REPONAME}/${SNAPSHOT}.json "http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT}" )
JQ_STATE=$( cat ${REPONAME}/${SNAPSHOT}.json | jq -r " .[] | .[] | .state " 2>> /dev/null )
fi
fi
echo ${JQ_STATE}
;;
create|CREATE)
ARGS=(${@})
INDEX=${ARGS[@]:3}
CURL_COMMAND=${REPONAME}/${APP}_${1}_command.curl
CURL_PAYLOAD=${REPONAME}/${APP}_${1}_payload.json
CURL_RESULT=${REPONAME}/${APP}_${1}_response.json
if [ ! -z "${REPONAME}" ] && [ ! -z "${INDEX}" ] && [ ! -z ${SNAPSHOT} ]
then
[ ! -d "${REPONAME}" ] && mkdir ${REPONAME}
echo '{"indices": "'${INDEX[@]// /,}'","ignore_unavailable": "true","include_global_state": false}' > ${CURL_PAYLOAD}
echo "curl -s -XPUT -o /dev/null -w %{http_code} 'http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT}?wait_for_completion=true'" > ${CURL_COMMAND}
echo -en "\e[01;33msnapshot: \e[01;35m${ACTION} ${REPONAME}/${SNAPSHOT} \e[01;37m[\e[01;36m ${INDEX} \e[01;37m]\e[00m "
curl -s -XPUT -o ${CURL_RESULT} "http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT}?wait_for_completion=true" \
-d '{"indices": "'${INDEX[@]// /,}'","ignore_unavailable": "true","include_global_state": false}'
RETURN=$(cat ${CURL_RESULT} | jq -r .snapshot.state 2>> /dev/null)
# RETURN:
# empty: error in jq ${CURL_RESULT} isn't a file
# 'null': ${CURL_RESULT} is a json but it's a error response from elasticsearch
# 'SUSCCESS|FAILED|STARTED': snapshot status
echo "${RETURN}"
[ -z "${RETURN}" ] && exit 3
[ "${RETURN}" == "null" ] && exit 4
else
echo "FAILLLLLLLLLLLLLLLLLL.. in ${ACTION}"
exit 1
fi
;;
restore|RESTORE)
INDEX=${3}
if [ ! -z ${SNAPSHOT} ]
then
if [ ! -z ${INDEX} ]
then
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${SNAPSHOT} \e[01;37m] \e[01;37m[\e[01;36m ${INDEX} \e[01;37m]\e[00m"
curl -s -XPOST "http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT}/_restore?wait_for_completion=true" \
-d '{
"indices": "'${INDEX}'",
"include_aliases": false,
"include_global_state": false,
"index_settings": {
"index.nummber_of_shards": 1,
"index.number_of_replicas": 0
},
"ignore_index_settings": [
"index.refresh_interval"
]
}' | jq .
else
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${SNAPSHOT} \e[01;37m]\e[00m"
curl -s -XPOST "http://${HOST}:9200/_snapshot/${REPONAME}/${SNAPSHOT}/_restore?wait_for_completion=true" \
-d '{
"include_aliases": false,
"include_global_state": false,
"index_settings": {
"index.nummber_of_shards": 1,
"index.number_of_replicas": 0
},
"ignore_index_settings": [
"index.refresh_interval"
]
}' | jq .
fi
else
echo "FAILLLLLLLLLLLLLLLLLL.. in ${ACTION}"
fi
;;
close) # _snapshoted_indices)
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION}\e[00m"
for SNAPSHOT in $(ls_snapshots)
do
echo -e "\t\e[01;37m[\e[01;36m ${SNAPSHOT} \e[01;37m]\e[00m"
for INDEX in $(ls_snapshot_indexes ${SNAPSHOT})
do
echo -e "\t\t\e[01;37m[\e[01;34m ${INDEX} \e[01;37m]\e[00m"
close_index ${INDEX}
done
done
;;
repos)
echo -e "\e[01;33msnapshot: \e[01;35m${ACTION}"
ls_repos | xargs -i echo -en "\t\e[01;35m"{}"\e[00m\n"
;;
*)
echo "$0 <action> [<repo name>] [extras]"
echo " action: [status|ls|compare|get|delete|create|restore|close]"
echo "$0 $1 in progressss...."
;;
esac