blog

Automating MongoDB Backups: A Guide

Lukas Vileikis

Published

If you’re a database administrator, chances are that you have heard that once things become related to your database, you have a couple of options to choose from. You can choose from MySQL, MS SQL, Oracle and other database management systems. One of your options is NoSQL – data in NoSQL databases provide a mechanism for data storage and retrieval that is a little bit different than tabular relations in relational databases. NoSQL is a non-relational database, it does not need a database model (relational databases need one), it does not provide ACID compliance and data inserting in NoSQL is different than in MySQL. One of the most frequent choices in the NoSQL world is MongoDB – in this blog post we will tell you how you should go about automating your MongoDB backup processes.

Backing up Data in MongoDB

MongoDB provides you with a couple of utilities that can help you backup (and restore) your data –

mongobackup

and

mongorestore

. Here’s how to use

mongobackup

to achieve your backup goals:

mongodump --gzip --archive=backup.gz

The above command backs up all of your data and immediately packs it into an archive named backup.gz.

You can also restore all of the data in a backup by using this command:

mongorestore --gzip --archive=backup.gz

You can also specify a –drop parameter to overwrite the restored data:

mongorestore --drop --gzip --archive=backup.gz

Automating MongoDB Backups

So far what we have noticed is that MongoDB has two utilities – mongobackup and mongorestore. They both certainly have their place in the MongoDB world, but running those commands all the time can become very time consuming and chances are that as a DBA you have thought about optimizing that – chances are that you have also thought about automating your MongoDB backups. Thankfully, that is not very hard to do – to do that you simply need to create a cronjob. Here’s a simple guide how to automate backups in MongoDB:

  • Create a directory where you will store your backups:
mkdir -p ~/mongo/backups
  • Schedule a regular backup to run every midnight (or at a different time if you so desire) using a cronjob with
mongodump

:

0 0 * * * mongodump –gzip –archive=~/mongo/backups/backup.gz

Obviously, this is not a very complex backup solution and if you want to achieve something more out of your MongoDB instances, you might want to look into solutions that are more suitable for this task.

Automating MongoDB Backups with Backup Ninja

MongoDB backup processes can also be automated by using Backup Ninja. Backup Ninja lets you take logical backups for MongoDB effortlessly and automatically using a web-based GUI tool. To add to that, you can schedule and manage all from within a single console – isn’t that convenient?

To automate your backup processes in MongoDB, simply choose MongoDB as your database server choice:

As far as your MongoDB backups are concerned, you can also choose whether you want to take partial or full backups of your MongoDB related data, you can select a backup method, also select a server to perform the backup on and also include or exclude certain tables from being backed up too:

Backup Ninja also allows you to observe the status of your backups (this feature is not limited to MongoDB – it can be very useful for other storage engines too):

Summary

To summarize, automating MongoDB backups isn’t a very complex task. One of the first options that comes to mind as far as MongoDB backup automation is concerned is scheduling a regular backup to run every midnight (or at a different time if you so desire) using a cronjob with mongodump, but as a developer or a DBA you also have other options allowing you to automate your MongoDB backup tasks using certain software solutions that might be more suitable for this task – one of those solutions is Backup Ninja. If you want to automate your MongoDB backup tasks with Backup Ninja, take a look at what options Backup Ninja offers in the MongoDB space and decide whether them for yourself.

Subscribe below to be notified of fresh posts