Created
February 11, 2019 19:11
-
-
Save jasonmimick/fc6992b693b472e059cb3efecaee3ee1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Simple demonstration of using the MongoDB Atlas API | |
# to download a backup. | |
if [ "$#" -ne 3 ] | |
then | |
echo "restore-backup.sh - simple example to download a backup from a MongoDB Atlas cluster" | |
echo "Usage: ./restore-backup.sh <atlasuser:atlas-apikey> <group-id> <cluster-name>" | |
exit 1 | |
fi | |
USERNAME=$(echo ${1} | cut -d':' -f1) | |
APIKEY=$(echo ${1} | cut -d':' -f2) | |
GROUP_ID=${2} | |
CLUSTER_NAME=${3} | |
echo "Detected USERNAME=${USERNAME}, APIKEY=${APIKEY}" | |
echo " GROUP_ID=${GROUP_ID}, CLUSTER_NAME=${CLUSTER_NAME}" | |
TEMPDIR=$(mktemp -d) | |
echo "Working in ${TEMPDIR}" | |
# Find the latest snapshot, it's the first one | |
SNAPSHOTS="${TEMPDIR}/snapshots.json" | |
curl --user "${USERNAME}:${APIKEY}" --output ${SNAPSHOTS} --digest --silent --header "Accept: application/json" --header "Content-Type: application/json" --request GET "https://cloud.mongodb.com/api/atlas/v1.0/groups/${GROUP_ID}/clusters/${CLUSTER_NAME}/snapshots" | |
echo "SNAPSHOTS=${SNAPSHOTS}" | |
cat ${SNAPSHOTS} | |
SNAPSHOT_ID=$(cat ${SNAPSHOTS} | jq -r '.results[0].id') | |
echo "Found SNAPSHOT=${SNAPSHOT_ID}" | |
# Create restore jos | |
RESTORE_JOB_REQUEST=$(cat <<RESTORE_JOB_JSON | |
{ | |
"delivery" : { | |
"expirationHours" : "1", | |
"maxDownloads" : "1", | |
"methodName" : "HTTP" | |
}, | |
"snapshotId" : "${SNAPSHOT_ID}" | |
} | |
RESTORE_JOB_JSON | |
) | |
echo "Submitting restore job request to cluster ${CLUSTER_NAME}\n${RESTORE_JOB_REQUEST}" | |
RESTORE_JOB_RESULT=${TEMPDIR}/restore-job-submit-result.json | |
curl --user "${USERNAME}:${APIKEY}" --digest --header "Accept: application/json" --silent --header "Content-Type: application/json" --output ${RESTORE_JOB_RESULT} --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/${GROUP_ID}/clusters/${CLUSTER_NAME}/restoreJobs" --data "${RESTORE_JOB_REQUEST}" | |
echo "Submitted new restore job. Result:" | |
cat ${RESTORE_JOB_RESULT} | |
RESTORE_JOB_ID=$(cat ${RESTORE_JOB_RESULT} | jq -r '.results[0].id') | |
echo "Checking status for restore job id: ${RESTORE_JOB_ID}" | |
# Poll for restore job to complete | |
STATUS="Not started" | |
while : | |
do | |
RESTORE_JOB_STATUS=${TEMPDIR}/restore-job-status.$(date +"%d-%m-%Y-%H-%M-%s").json | |
curl --user "${USERNAME}:${APIKEY}" --digest --header "Accept: application/json" --silent --header "Content-Type: application/json" --output ${RESTORE_JOB_STATUS} --request GET "https://cloud.mongodb.com/api/atlas/v1.0/groups/${GROUP_ID}/clusters/${CLUSTER_NAME}/restoreJobs/${RESTORE_JOB_ID}" | |
echo "Restore job status: " | |
cat ${RESTORE_JOB_STATUS} | |
STATUS=$(cat ${RESTORE_JOB_STATUS} | jq -r '.statusName') | |
echo "Found STATUS=${STATUS}" | |
if [ "${STATUS}" == "FINISHED" ]; then | |
echo "Restore job complete" | |
DOWNLOAD_URL=$(cat ${RESTORE_JOB_STATUS} | jq -r '.delivery.url') | |
echo "Found download url: ${DOWNLOAD_URL}" | |
break | |
fi | |
done | |
# Download backup | |
curl -OL ${DOWNLOAD_URL} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment