Created
August 7, 2018 17:40
-
-
Save jweyrich/8db5cd599416afe908a638e6aade9f1b to your computer and use it in GitHub Desktop.
Backup a MongoDB cluster and upload it to S3
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 | |
# | |
# DESCRIPTION: | |
# Make a gzipped backup from a MongoDB cluster >=3.2.0, | |
# preferrable from a slave, and upload it to the | |
# specified S3 bucket. | |
# | |
# REQUIREMENTS: | |
# sudo apt-get install -y awscli | |
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 | |
# /bin/echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list | |
# sudo apt-get update | |
# sudo apt-get install -y mongodb-org-tools | |
# | |
WORKDIR=$(dirname "$0") | |
source $WORKDIR/exit_handlers.sh # see https://gist.github.com/jweyrich/a2ad1cbc6d87725f4f99dfea188a7cf0 | |
function array_join { local IFS="$1"; shift; /bin/echo "$*"; } | |
AWSCLI_PROFILE="backup-mongodb" # CONFIGURATION | |
REPLICA_SET="rs0" # CONFIGURATION | |
HOSTS=( \ | |
"mongo01.example.com:27017" \ | |
"mongo02.example.com:27017" \ | |
"mongo03.example.com:27017" \ | |
"mongo04.example.com:27017" \ | |
"mongo05.example.com:27017" \ | |
) # CONFIGURATION | |
HOST_STRING=$(array_join , "${HOSTS[@]}") | |
# Folder where backup files are temporarily stored | |
BAK_DEST=/backup # CONFIGURATION | |
# S3 bucket name where backups will be stored | |
S3_BUCKET=CHANGE_ME # CONFIGURATION | |
# Number of days to keep archives | |
KEEP_DAYS=1 # CONFIGURATION | |
# Variables | |
ARCHIVE_DATETIME=$(/bin/date +%F-%H%M) | |
ARCHIVE_PATH=${BAK_DEST}/mongodb-${ARCHIVE_DATETIME}.gz | |
# Delete backups older than $KEEP_DAYS days | |
/bin/echo "Deleting backups older than ${KEEP_DAYS} days..." | |
find ${BAK_DEST} -type f -mtime +${KEEP_DAYS} -name "mongodb-*.gz" -delete; | |
# Dump databases | |
/bin/echo "Dumping databases to: ${ARCHIVE_PATH}" | |
# Mongo >= 3.2.0 | |
/usr/bin/mongodump \ | |
--host="${REPLICA_SET}/${HOST_STRING}" \ | |
--readPreference="secondaryPreferred" \ | |
--archive="${ARCHIVE_PATH}" \ | |
--gzip | |
S3_ARCHIVE_URL="s3://${S3_BUCKET}/backup/mongodb/${ARCHIVE_DATETIME}.gz" | |
/bin/echo "Uploading backup to S3: ${S3_ARCHIVE_URL}" | |
#/usr/bin/s3cmd put ${ARCHIVE_PATH} ${S3_ARCHIVE_URL} | |
/usr/bin/aws --profile="${AWSCLI_PROFILE}" s3 cp ${ARCHIVE_PATH} ${S3_ARCHIVE_URL} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment