Periodic MongoDB backup script

I wrote a periodic/cron script for backing up MongoDB databases. It is based on Palle Girgensohn’s backup script for PostgreSQL from the FreeBSD PostgreSQL port.

Features

The script is very basic: it allows you to specify the databases to back up via daily_mongodb_dbs (“all” not possible), to set the user that calls mongodump (daily_mongodb_user), the path to store the backups in (daily_mongodb_backupdir) and the number of days old backups are archived (daily_mongodb_savedays).

There is no authentication support, meaning you can’t specify a username or password for authenticating in mongodb. Also backups are not compressed.

The MongoDB backup script

#!/bin/sh
#
# Define these variables in either /etc/periodic.conf or
# /etc/periodic.conf.local to override the default values.
#
# daily_mongodb_enable="YES" # enable backup
# daily_mongodb_dbs="foo bar db1 db2" # specify databases to back up

daily_mongodb_user=mongodb
daily_mongodb_backupdir="/usr/local/backups"
daily_mongodb_savedays="7"

# If there is a global system configuration file, suck it in.
if [ -r /etc/defaults/periodic.conf ]
then
        . /etc/defaults/periodic.conf
        source_periodic_confs
fi

eval backupdir=${daily_mongodb_backupdir}

rc=0

mongodb_backup() {
        # daily_mongodb_backupdir must be writeable by user daily_mongodb_user
        if [ ! -d ${backupdir} ] ; then 
                echo Creating ${backupdir}
                mkdir -m 740 ${backupdir}
                chown ${daily_mongodb_user} ${backupdir}
        fi

        echo
        echo "MongoDB backups"

        # Protect the data
        umask 077
        rc=$?
        now=`date "+%Y-%m-%dT%H:%M:%S"`

        db=$1
        while shift; do
                echo -n " $db"
                file=${backupdir}/mongodump_${db}_${now}
                su -l ${daily_mongodb_user} -c \
                       "umask 027; mongodump -d ${db} -o ${file} > /dev/null"
                [ $? -gt 0 ] && rc=3
                        db=$1
        done

        if [ $rc -gt 0 ]; then
                echo
                echo "Errors were reported during backup."
        fi

        # cleaning up old data
        find ${backupdir} -name 'mongodump_*' -a \
                -mtime +${daily_mongodb_savedays} -delete
        echo
}

case "$daily_mongodb_enable" in
        [Yy][Ee][Ss])
                if [ -n "$daily_mongodb_dbs" ]; then
                        mongodb_backup $daily_mongodb_dbs
                else
                        echo "Enabled but no databases specified."
                fi
                ;;

                *)
                ;;
esac

exit $rc

Note the umask 027 command in line 44. You may want to change that according to your needs. I want my backup files be readable by the wheel group so I can easily download them via rsync.

Usage

If you are on FreeBSD simply put the script in /usr/local/etc/periodic/daily. Activate it by adding daily_mongodb_enable=“YES” to /etc/periodic.conf. You also need to set daily_mongodb_dbs.

First published on August 29, 2013