forked from orangejulius/elasticsearch-bash-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_snapshot.sh
58 lines (49 loc) · 1.54 KB
/
load_snapshot.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
#!/bin/bash -ex
##
# loads a snapshot onto an elasticsearch cluster
##
set -euo pipefail
cluster_url="${cluster_url:-http://localhost:9200}"
base_path=${base_path:-elasticsearch}
es_repo_name="${es_repo_name:-pelias_snapshot}"
s3_bucket="${s3_bucket:-}"
snapshot_name="${snapshot_name:-}"
read_only="${read_only:-true}"
# check all required variables are set
if [[ "$s3_bucket" == "" ]]; then
echo "s3_bucket not set, no snapshot will be loaded"
exit 1
fi
# check jq is installed
if ! [[ -x "$(command -v jq)" ]]; then
echo 'Error: jq is not installed.' >&2
exit 1
fi
# create elasticsearch snapshot repository
curl -XPOST "$cluster_url/_snapshot/$es_repo_name" \
-H 'Content-Type: application/json' \
-d "{
\"type\": \"s3\",
\"settings\": {
\"bucket\": \"$s3_bucket\",
\"read_only\": $read_only,
\"base_path\" : \"$base_path\",
\"max_snapshot_bytes_per_sec\" : \"1000mb\",
\"max_restore_bytes_per_sec\" : \"1000mb\"
}
}"
## autodetect snapshot name if not specified
if [[ "$snapshot_name" == "" ]]; then
snapshot_name=$(curl -s -XGET --fail "$cluster_url/_snapshot/$es_repo_name/_all" | jq -r .snapshots[0].snapshot)
echo "autodetected snapshot name is $snapshot_name"
fi
## import new snapshot
## The resulting index name will include the full snapshot
curl -XPOST "$cluster_url/_snapshot/$es_repo_name/$snapshot_name/_restore" \
-H 'Content-Type: application/json' \
-d "{
\"indices\": \"pelias\",
\"rename_pattern\": \"pelias\",
\"rename_replacement\": \"$snapshot_name\"
}"
echo "loading snapshot into index $snapshot_name"