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/backup-mongodb.sh

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

#!/bin/sh

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}
docker cp ${CONTAINER_NAME}:/data/${DATABASE_NAME}-backup-${TIMESTAMP} ${BACKUP_LOCATION}

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

./drop-mongodb.sh
./restore-mongodb.sh -d ../backups/planets-backup-20170611124301

With the following script drop-mongodb.sh:

#!/bin/sh

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

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

and restore-mongodb.sh:

#/bin/bash

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
    d) BACKUP_DIRECTORY="$OPTARG";;
    h) echo "${USAGE_MESSAGE}"
        exit 1;;
    :) echo "${USAGE_MESSAGE}"
        exit 1;;
    *) echo "${USAGE_MESSAGE}"
        exit 1;;
esac
done

shift $(($OPTIND - 1))

docker cp ${BACKUP_DIRECTORY} ${CONTAINER_NAME}:/data

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 planets@datenkollektiv.de 'cd backups && tar -jc planets-backup-20170611124301' | tar -x -

does the trick.

Show Comments