-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-import
More file actions
executable file
·278 lines (247 loc) · 8.51 KB
/
index-import
File metadata and controls
executable file
·278 lines (247 loc) · 8.51 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
# default value
es_host=127.0.0.1
es_port=9200
repo=backup_repo
es_container=elasticsearch
register_path="/usr/share/elasticsearch/backup"
from_step=0
is_eh_set=false
is_ep_set=false
is_ec_set=false
while (($#)); do
case $1 in
"--eshost" | "-eh")
shift
es_host=$1
is_eh_set=true
shift
;;
"--esport" | "-ep")
shift
es_port=$1
is_ep_set=true
shift
;;
"--escontainer" | "-ec")
shift
es_container=$1
is_ec_set=true
shift
;;
"--index" | "-i")
shift
index=$1
shift
;;
"--repo" | "-r")
shift
repo=$1
shift
;;
"--snapshot" | "-s")
shift
snapshot=$1
shift
;;
"--register-path" | "-rp")
shift
register_path=$1
shift
;;
"--target" | "-t")
shift
target=$1
shift
;;
"--from-step" | "-f")
shift
from_step=$1
shift
;;
"--help" | "-h")
echo "Required options:"
echo " --snapshot <snapshot>, -s <snapshot>"
echo " Snapshot name."
echo " --target <path>, -t <path>"
echo " Target path of backup folder."
echo " --index <index>, -i <index>"
echo " Index that specified to be imported."
echo
echo "Optional options:"
echo " --eshost <host>, -eh <host>"
echo " Elasticsearch service host. Default: 127.0.0.1"
echo " --esport <port>, -ep <port>"
echo " Elasticsearch service port. Default: 9200"
echo " --escontainer <container name>, -ec <continer name>"
echo " Elasticsearch docker container name. Default: elasticsearch"
echo " --repo <repository>, -r <repository>"
echo " Snapshot repository name. Default: backup_repo"
echo " --register-path <path>, -rp <path>"
echo " Path of snapshot repository. Used for registering repository if repository doesn't exists. Default: /usr/share/elasticsearch/backup"
echo " --from-step <step>, -f <step>"
echo " Start from which step. Default: 1"
exit 0
;;
*)
echo "unknown argument '$1'"
echo "Use --help (or -h) to get the usage information."
exit 1
esac
done
if [ -z "$snapshot" ]; then
echo "Option --snapshot is required."
exit 1
fi
if [ -z "$target" ]; then
echo "Option --target is required."
exit 1
fi
if [ -z "$index" ]; then
echo "--index is required."
exit 1
fi
# checking docker container using elasticsearch image
if [ $from_step -le 1 ]; then
if $is_ec_set && $is_eh_set && $is_ep_set; then
echo "[Step 1] Container name, host, port are specified. Skip step 1."
else
echo "[Step 1] Checking running Elasticsearch container."
image_list=$(docker ps --format "{{.Image}}")
es_count=$(echo $image_list | grep "elasticsearch" | wc -l)
if [ $es_count -gt 1 ]; then
echo "[Step 1] Warning! You have more than 1 running container using image contains string 'elasticsearch'!"
echo "[Step 1] Use values: container name: '$es_container', host: $es_host, port: $es_port"
elif [ $es_count -eq 0 ]; then
echo "[Step 1] Warning! You don't have any running container using image contains string 'elasticsearch'!"
echo "[Step 1] Use values: container name: '$es_container', host: $es_host, port: $es_port"
else
es_image=$(docker ps --format "{{.Names}} {{.Ports}} {{.Image}}" | grep "elasticsearch")
if ! $is_ec_set; then
es_container=$(echo $es_image | cut -d" " -f 1)
fi
es_network=$(echo $es_image | cut -d" " -f 2 | cut -d"-" -f 1)
if ! $is_eh_set; then
es_host=$(echo $es_network | cut -d":" -f 1)
fi
if ! $is_ep_set; then
es_port=$(echo $es_network | cut -d":" -f 2)
fi
echo "[Step 1] Found 1 running Elasticsearch container. Container name: '$es_container', host: $es_host, port: $es_port"
fi
fi
fi
# check if the index exists.
if [ $from_step -le 2 ]; then
echo "[Step 2] Check if the index exists."
resp=$(curl --silent --fail-with-body "http://$es_host:$es_port/_cat/indices/$index")
if [ "$(echo $resp | grep " $index ")" != "" ]; then
echo "[Step 2] Index '$index' exists. You can remove it by 'DELETE http://$es_host:$es_port/$index'."
exit 1
else
echo "[Step 2] Index '$index' doesn't exist."
fi
fi
# remove existing repository
if [ $from_step -le 3 ]; then
echo "[Step 3] Check if the repository '$repo' is registered."
curl --silent --fail-with-body "http://$es_host:$es_port/_snapshot/$repo"
if [ $? -eq 0 ]; then
echo
echo "[Step 3] Repository has already registered. Unregister it."
curl -X DELETE --silent --fail-with-body "http://$es_host:$es_port/_snapshot/$repo"
if [ $? -eq 0 ]; then
echo
echo "[Step 3] Unregister succeed."
else
echo
echo "[Step 3] Unregister failed. You can retry from this step with option '--from-step 3'."
exit 1
fi
else
echo "[Step 3] Repository hasn't registered."
fi
fi
# clean folder
if [ $from_step -le 4 ]; then
echo "[Step 4] Remove snapshot files at snapshot repository."
docker exec -i $es_container rm -rf "$register_path"
if [ $? -eq 0 ]; then
echo "[Step 4] Remove succeed."
else
echo "[Step 4] Remove failed. You can retry from this step with option '--from-step 4'."
exit 1
fi
docker exec -i $es_container mkdir "$register_path"
fi
# copy files
if [ $from_step -le 5 ]; then
echo "[Step 5] Copy files from '$target' to snapshot repository folder."
docker cp $target/. $es_container:$register_path/
if [ $? -eq 0 ]; then
echo "[Step 5] Copy succeed."
else
echo "[Step 5] Copy failed. You can retry from this step with option '--from-step 5'."
exit 1
fi
fi
# modify ownership
if [ $from_step -le 6 ]; then
echo "[Step 6] Modify ownership of snapshot repository folder."
docker exec -i $es_container chown -R elasticsearch:root $register_path
if [ $? -eq 0 ]; then
echo "[Step 6] Modify succeed."
else
echo "[Step 6] Modify failed. You can retry from this step with option '--from-step 6'."
exit 1
fi
fi
# register repository
if [ $from_step -le 7 ]; then
echo "[Step 7] Register repository."
curl --fail-with-body -X PUT "http://$es_host:$es_port/_snapshot/$repo" \
-H "Content-Type: application/json" \
-d "{\"type\":\"fs\",\"settings\":{\"location\":\"$register_path\"}}"
if [ $? -eq 0 ]; then
echo
echo "[Step 7] Register succeed."
else
echo
echo "[Step 7] Register failed. You can retry from this step with option '--from-step 7'."
exit 1
fi
fi
# get snapshot details
if [ $from_step -le 8 ]; then
echo "[Step 8] check if snapshot is loaded successfully."
resp=$(curl --silent --fail-with-body "http://$es_host:$es_port/_snapshot/$repo/$snapshot")
if [ $? -ne 0 ]; then
echo "[Step 8] Getting snapshot details failed. You can retry from this step with option '--from-step 8'."
echo "[Step 8] Error response:"
echo $resp
exit 1
fi
if [ ! -z "$(echo $resp | grep $snapshot)" ]; then
echo "[Step 8] Snapshot is loaded."
else
echo "[Step 8] Snapshot doesn't exist. You can retry from this step with option '--from-step 8'."
exit 1
fi
fi
# restore
if [ $from_step -le 9 ]; then
echo "[Step 9] Start restoring."
curl --fail-with-body -X POST "http://$es_host:$es_port/_snapshot/$repo/$snapshot/_restore?wait_for_completion=true" \
-H "Content-Type: application/json" \
-d "{\"indices\":\"$index\"}"
if [ $? -eq 0 ]; then
echo
echo "[Step 9] Restore succeed."
else
echo
echo "[Step 9] Restore failed. You can retry from this step with option '--from-step 9'."
exit 1
fi
fi
echo
echo "Done!!"