Created
May 25, 2018 14:35
-
-
Save jeroenvollenbrock/cff02c155ccb38b6b3f10f0b06fb0934 to your computer and use it in GitHub Desktop.
MongoDB Atlas backup downloader
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/sh -e | |
# backup sync script for mongo atlas | |
# requires api whitelist access | |
# depends on curl, jq and shasum (if verify is enabled) | |
USER=${ATLAS_USER:=$1} | |
KEY=${ATLAS_KEY:=$2} | |
GROUP=${ATLAS_GROUP:=$3} | |
CLUSTER=${ATLAS_CLUSTER:=$4} | |
SKIP_VERIFY=${ATLAS_SKIP_VERIFY:=$5} | |
if [ -z "$USER" ] || [ -z "$KEY" ] || [ -z "$GROUP" ] || [ -z "$CLUSTER" ]; then | |
echo "Usage: $0 atlas_user atlas_key atlas_group_id atlas_cluster_name [skip_verify_backup]" | |
echo "" | |
echo "Downloads the latest atlas backup to the current working directory and verifies the files" | |
echo "Requires curl, jq and shasum to be installed" | |
exit 1 | |
fi | |
export LC_ALL=C | |
# Select a snapshot | |
SNAPSHOT_ID=$( | |
curl -s --show-error --fail --fail-early --digest -u "${USER}:${KEY}" "https://cloud.mongodb.com/api/atlas/v1.0/groups/${GROUP}/clusters/${CLUSTER}/snapshots?itemsPerPage=1" \ | |
| jq -r ".results[0].id" | |
) | |
echo "Requesting Snapshot:" ${SNAPSHOT_ID} | |
# Request a restore job | |
RESTORE_IDS=$(curl -s --show-error --fail --fail-early -X POST --digest -u "${USER}:${KEY}" --header "Content-Type: application/json" \ | |
"https://cloud.mongodb.com/api/atlas/v1.0/groups/${GROUP}/clusters/${CLUSTER}/restoreJobs" \ | |
--data "{\"delivery\":{\"methodName\":\"HTTP\",\"expirationHours\":1,\"maxDownloads\":1},\"snapshotId\" : \"${SNAPSHOT_ID}\"}" \ | |
| jq -r ".results[] | .id") | |
echo "Attempting to download" $RESTORE_IDS | |
# Loop the restore jobs and download all "ready" files | |
RESTORE_IDS_PENDING="$RESTORE_IDS" | |
while [ ! -z "$RESTORE_IDS_PENDING" ]; do | |
RESTORE_IDS_DEFER="" | |
RESTORE_URLS="" | |
for RESTORE_ID in $RESTORE_IDS_PENDING; do | |
JOB=$(curl -s --show-error --fail --fail-early --digest -u "${USER}:${KEY}" --header "Content-Type: application/json" \ | |
"https://cloud.mongodb.com/api/atlas/v1.0/groups/${GROUP}/clusters/${CLUSTER}/restoreJobs/${RESTORE_ID}") | |
STATUS=$(echo "$JOB" | jq -r ".delivery.statusName") | |
if [ "$STATUS" == "READY" ]; then | |
URL=$(echo "$JOB" | jq -r ".delivery.url") | |
RESTORE_URLS="${RESTORE_URLS}${URL} " | |
elif [ "$STATUS" == "NOT_STARTED" ] || [ "$STATUS" == "IN_PROGRESS" ]; then | |
RESTORE_IDS_DEFER="${RESTORE_IDS_DEFER}${RESTORE_ID} " | |
else | |
echo "Failed to restore!" $STATUS | |
exit 1 | |
fi | |
done | |
if [ ! -z "$RESTORE_URLS" ]; then | |
echo "Starting Download batch" | |
curl --fail-early --fail -O --remote-header-name $RESTORE_URLS | |
else | |
sleep 5 | |
fi | |
RESTORE_IDS_PENDING="$RESTORE_IDS_DEFER" | |
done | |
# Verify the downloaded files | |
if [ -z "$SKIP_VERIFY" ]; then | |
for RESTORE_ID in $RESTORE_IDS; do | |
curl -s --show-error --fail --fail-early --digest -u "${USER}:${KEY}" --header "Content-Type: application/json" \ | |
"https://cloud.mongodb.com/api/atlas/v1.0/groups/${GROUP}/clusters/${CLUSTER}/restoreJobs/${RESTORE_ID}" \ | |
| jq -r ".hashes[] | .hash+\" ?\"+.fileName" | shasum -c | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment