Skip to content

Instantly share code, notes, and snippets.

@khaledosman
Last active October 15, 2018 09:00
Show Gist options
  • Save khaledosman/87b7f4bca6f55a6b3c05a65447d9157e to your computer and use it in GitHub Desktop.
Save khaledosman/87b7f4bca6f55a6b3c05a65447d9157e to your computer and use it in GitHub Desktop.
bash script for taking daily mongo backups inside docker image
#!/bin/bash
export CONTAINER_NAME=""
export DATABASE_NAME=""
export BACKUP_LOCATION="/home/ec2-user/backups"
export MONGO_USER=""
export MONGO_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=""
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
00 00 * * * ~/scripts/mongo-backup.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment