Automated Backup of MongoDB Containers

TL;DR We are Using a containerized MongoDB and do manual backups on our staging environment from time to time (See How to Backup and Restore a small MongoDB 2.6 Instance for more details). Since the data we gathered on staging becomes more and more valuable we decided to improve our situation.

Having said that we created a cron job...

$ crontab -e

Add the nightly task:

15 02 * * * /usr/local/bin/

As described in detail in the How to Backup and Restore a small MongoDB 2.6 Instance the backup script is rather easy:


export CONTAINER_NAME="mongodb-planets"
export DATABASE_NAME="planets"
export BACKUP_LOCATION="/home/planets/Projects/datenkollektiv/backups"

export TIMESTAMP=$(date +'%Y%m%d%H%M%S')

docker exec -t ${CONTAINER_NAME} mongodump --out /data/${DATABASE_NAME}-backup-${TIMESTAMP} --db ${DATABASE_NAME}

If we messed up something on our staging environment we drop the current database and restore a nightly build like this:

./ -d ../backups/planets-backup-20170611124301

With the following script


export CONTAINER_NAME="mongodb-planets"
export DATABASE_NAME="planets"

docker exec -i ${CONTAINER_NAME} mongo ${DATABASE_NAME} --eval "db.dropDatabase()"     



export CONTAINER_NAME="mongodb-planets"

USAGE_MESSAGE="Usage: $0 [-h] [-d backupDirectory]
    -h help
    -d {backupDirectory} to use"

while getopts hd: args
do case "$args" in
    h) echo "${USAGE_MESSAGE}"
        exit 1;;
    :) echo "${USAGE_MESSAGE}"
        exit 1;;
    *) echo "${USAGE_MESSAGE}"
        exit 1;;

shift $(($OPTIND - 1))


docker exec -i ${CONTAINER_NAME} mongorestore /data/$( basename ${BACKUP_DIRECTORY})

Sometime I need to transfer the staging data to my local development environment. Lucky me we had this use-case before: How to transfer a resource folder of a Java application to a remote server with a unix one liner

For the impatient:

$ ssh -C 'cd backups && tar -jc planets-backup-20170611124301' | tar -x -

does the trick.

Show Comments