Last Updated: January 23, 2017
· jwd2a

Easy automatic nightly backups for MongoDB

Backing up your MongoDB database nightly is a good practice to ensure the safety of your data should something happen to your server. Obviously, keeping a copy of everything somewhere other than your database server is a great way to make sure that, in the event something happens, you can restore and get back running quickly.

Many hosts will offer backup services as a paid service, buy why pay when you can easily set up automatic nightly backups of your database on your own, in just a few minutes.

Here’s the brief overview of what we’re going to do:

  • Create a script that automatically creates a backup directory for the current date, and exports your MongoDB database to that directory.
  • Put that script on a server that can run cron scripts for you
  • Set up cron to nightly run the script to execute the backup

Step 0: Get Your Ducks in a Row

Before we jump into writing the script, we’re going to need to create a spot on our server for the backup files. For instance, let’s say you want to keep your backups at the root of your web server, in a folder called db_backups.

Simply create that directory on the server:

mkdir /db_backups

Step 1: Write the Script

Now that that’s out of the way, we’re going to write a little shell script that creates a directory for the current day, then dumps MongoDB into it. That script looks like this:

DIR=`date +%m%d%y`
mkdir $DEST
mongodump -h <your_database_host> -d <your_database_name> -u <username> -p <password> -o $DEST

This is a really simple little shell script that simply creates a directory name using the date (in the format MMDDYY), puts together the full path for the to-be-created directory, creates said directory, and finally, uses Mongo’s mongodump utility to export the database into the directory.

Create this script and save it somewhere on your server, perhaps in ~/scripts or a similar spot you’ll recognize.

Step 2: Set Up cron to Run the Script Nightly

Now that we have our backup script ready to roll, we just need to get it running nightly to perform the backups. This is easily done by using cron to run the script.

On your server, simply open the crontab like this:

sudo crontab -e

In that file, underneath all the comments at the top, enter the following line:

45 1 * * * ../../scripts/

In cron, each line represents a registered entry. The format of each entry tells Cron how to act, and features 5 different fields for specifying the day and time to run the command. These fields go in this order: minute, hour, day of month, month, day of week. An asterisk means it’ll run on every division of the interval (so, if you put an asterisk in the second field, it’ll run every hour).

So, in our entry above we’re telling cron to run our script (the path you see in the entry), at 1:45am every night. Easy! Just save your crontab and exit, and you’ll be off to the races.

Bonus Points

Other things you might want to do to enhance this script: Remove directories that are 30 days old. If you don’t have a need to keep more than 30 days of backups, then simply remove old directories at the same time you’re doing your nightly backup.

Secure your script by using environment variables for password. Putting your plaintext db password into a script like this isn’t the most secure idea in the world. Consider using a dynamic environment variable to populate your password, should your script end up in front of the wrong eyes. You might also consider connecting to Mongo via ssh, but that’s outside the scope of this article.

I hope this helps you understand better how to backup your MongoDB database nightly! It’s an easy thing to set up, and will give you great peace of mind knowing that all your data is safe and sound should a disaster strike.