Created
October 15, 2018 08:57
-
-
Save khaledosman/9e46c59097a10d64d0ee45329f8cfd04 to your computer and use it in GitHub Desktop.
Takes Mongo Backups and Uploads them to S3 from a docker container
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 | |
| export CONTAINER_NAME="docker-container-name" | |
| export DATABASE_NAME="database-name" | |
| export BACKUP_LOCATION="/home/ec2-user/backups" | |
| export MONGO_USER="mongo authentication user" | |
| export MONGO_PASSWORD="mongo authnetication password" | |
| export TIMESTAMP=$(date +'%Y%m%d%H%M%S') | |
| export DUMP_PATH=/data/${DATABASE_NAME}-backup-${TIMESTAMP} | |
| export ZIP_PATH=${DATABASE_NAME}-backup-${TIMESTAMP}.zip | |
| export S3_BUCKET="s3-bucket" | |
| docker exec -t ${CONTAINER_NAME} mongodump --out ${DUMP_PATH} --db ${DATABASE_NAME} -u ${MONGO_USER} -p ${MONGO_PASSWORD} --authenticationDatabase admin | |
| docker cp ${CONTAINER_NAME}:${DUMP_PATH} ${BACKUP_LOCATION} | |
| zip -r ${ZIP_PATH} ${BACKUP_LOCATION} | |
| docker exec -t ${CONTAINER_NAME} rm -rf ${DUMP_PATH} | |
| aws s3 cp ${ZIP_PATH} s3://${S3_BUCKET} | |
| rm -rf ${BACKUP_LOCATION} ${ZIP_PATH} | |
| ============================================================================================================== | |
| crontab -e | |
| MAILTO=<email> | |
| 0 * * * * /home/ec2-user/mongo_backup.sh > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment