-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-restore.sh
executable file
·48 lines (40 loc) · 1.43 KB
/
auto-restore.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
#/bin/bash
# Setup the source and destination here
SOURCE_CLUSTER_NAME='Demo'
TARGET_PROJECT_ID='5b75e82ec0c6e34530147939'
TARGET_CLUSTER_NAME='QA'
strip_quotes (){
var=${1}
var="${var%\"*}"
var="${var#*\"}"
echo "${var}"
return 0
}
# If current snapshot is still taking place, wait for it to stop.
status=''
while [[ "${status}" != "completed" ]]
do
status=$(mongocli atlas backups snapshots list "${SOURCE_CLUSTER_NAME}" -o json | jq '.results[0].status')
status="${status%\"*}"
status="${status#*\"}"
echo "Waiting on backup to complete. status: ${status}"
sleep 10
done
# get last snapshot ID
snapshotId=$(mongocli atlas backups snapshots list "${SOURCE_CLUSTER_NAME}" -o json | jq '.results[0].id')
snapshotId=$(strip_quotes "$snapshotId")
# Restore snapshot to TARGET_PROJECT_ID / TARGET_CLUSTER_NAME
echo "Starting restore... (snapshotId: ${snapshotId})"
# Start the restore job
mongocli atlas backups restores start automated --clusterName "${SOURCE_CLUSTER_NAME}" --output json --snapshotId "${snapshotId}" --targetClusterName "${TARGET_CLUSTER_NAME}" --targetProjectId "${TARGET_PROJECT_ID}" > /dev/null 2>&1
# Wait for restore to finish
finishedAt='null'
let count=0
while [[ "${finishedAt}" == 'null' ]]
do
echo "Waiting for completion (${count})..."
let count=${count}+1
sleep 10
finishedAt=$(mongocli atlas backups restores list "${SOURCE_CLUSTER_NAME}" --output json | jq '.results[0].finishedAt')
done
echo "Done."