This repository was archived by the owner on Jul 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshotter.sh
64 lines (37 loc) · 1.62 KB
/
snapshotter.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
#!/bin/bash
set -eo pipefail
#dependencies: curl
#example of running docker container with elasticsearch
#docker run --name elasticsearch -v /var/backup/elasticsearch -p 9200:9200 -d elasticsearch -Des.path.repo=/var/backup/elasticsearch
SNAPSHOT_VOLUME=$1
SNAPSHOT_REPOSITORY_NAME=${SNAPSHOT_REPOSITORY_NAME-"elasticsearch-snapshots"}
SNAPSHOT_VOLUME=${SNAPSHOT_VOLUME-"/var/backup/elasticsearch"}
SNAPSHOT_NAME=${SNAPSHOT_NAME-"elasticsearch_$(date +%Y.%m.%d_%H:%M:%S)"}
ELASTICSEARCH_URL=${ELASTICSEARCH_URL-"localhost:9200"}
#check if elasticsearch url is online
while [[ -z "$(curl -s "http://${ELASTICSEARCH_URL}/" | grep "\"name\"")" ]]; do
echo "${ELASTICSEARCH_URL} seems to be down"
sleep 1;
done
#Without these 2 lines, this error when registering new snapshot repository occurs:
# "type" : "repository_exception",
# "reason" : "[backup00] failed to create repository",
# "type" : "access_denied_exception",
mkdir -p "${SNAPSHOT_VOLUME}/${SNAPSHOT_REPOSITORY_NAME}"
chmod -R 777 "${SNAPSHOT_VOLUME}/${SNAPSHOT_REPOSITORY_NAME}"
#if snapshot repository don't exists, create it
result="$(curl "http://${ELASTICSEARCH_URL}/_snapshot/")"
if [[ -z "$(echo "result" | grep "$SNAPSHOT_REPOSITORY_NAME")" ]]; then
curl -XPUT "http://${ELASTICSEARCH_URL}/_snapshot/${SNAPSHOT_REPOSITORY_NAME}?pretty" -d "
{
\"type\" : \"fs\",
\"settings\" : {
\"compress\" : \"true\",
\"location\" : \"/${SNAPSHOT_VOLUME}/${SNAPSHOT_REPOSITORY_NAME}\"
}
}
"
fi
#create snaphot!
curl -XPUT "http://${ELASTICSEARCH_URL}/_snapshot/${SNAPSHOT_REPOSITORY_NAME}/${SNAPSHOT_NAME}?wait_for_completion=true"
return 0;