Popular among both enterprises and startups, MongoDB is a database that is perfectly suited for web-apps that need to scale up once the user base increases. MongoDB is different from traditional relational databases because it uses json like objects to store data, instead of tables in relational databases.
In this post, we will learn to backup and restore a MongoDB database. In all software products there is an import and export feature, which in database terms, deals with human-readable format. On the other hand, the backup and restore operations use MongoDB specific data, which preserve the MongoDB attributes.
So, when migrating the database, we should prefer backup and restore over import and export. But we should also keep in mind that our source and target systems need to be are compatible, which means that both should be Windows or both should be a Linux based system like Ubuntu/Mac.
Prerequisites
We are using Windows 10 in this tutorial. Please make sure you have downloaded the MongoDB Community Server and installed it. It is a very easy setup and you will find lot of good articles on the internet detailing this out. Please ensure that you have added it in the Environment variable in your PC.
Backup Considerations
In a production environment, backups act as a snapshot of the database at a certain point. Large and complex databases do fail or can be hacked. If that happens, we can use the last backup file to restore the database to the point, before it failed. These are some of the factors which should be taken into consideration when doing a recovery.
1. Recovery Point Objective
We should know the objective of the recovery point, which means how much data we are willing to lose during a backup and restoration. A continuous backup is preferred for critical data like bank information and backups should be taken several times during the day. On the other hand, if the data doesn’t change frequently, then the backup can be taken every 6 months.
2. Recovery Time Objective
This tells how quickly the restoration can be done. During restoration the application will be down for some time; and this downtime should be minimized, or else customers will be inconvenienced and it may result in loss of business or loss of customer trust.
3. Database and Snapshot Isolation
This refers to the distance between the primary database server and the backup server. If they are close enough i.e., in the same building, then the recovery time reduces. However, in the event of a physical event such as a fire, there is a likelihood of it having been destroyed along with the primary database.
4. Restoration Process
We should always test our backups in test servers to see if they will work, in case a restoration is required.
5. Available Storage
Backup of database generally takes a lot of space and in most cases, it will never be required. So, we should try to minimize the space taken on the disk, by archiving the database into a zip file.
6. Complexity of Deployment
The backup strategy should be easy to set and should be automated, so that we don’t have to remember to take the backup after regular intervals.
Understanding the Basics
The first thing that we should know is that MongoDB uses json and bson(binary json) formats for storing data. So, people coming from a JavaScript background can relate to objects for json, which have a key-value pair. Also, json is the preferred format in which we receive or send data to an API endpoint.
You can check the json data of a MongoDB database in any tool or online editors. Even the famous Windows application Notepad++ has a json viewer.
Here’s a snapshot of what a json document would look like:
As we can see from the above example, json is very convenient to work with, especially for developers. But it doesn’t support all the data types available in bson. So, for backup and restoring, we should use binary bson.
The second thing to keep in mind is that MongoDB automatically creates databases and collection names if they don’t exist during restore operations.
Third, since MongoDB is a document-based database, in many use cases we store large amounts of data in one collection, such as the whole post of an article. MongoDB is also used extensively in large databases and big data. So, reading and inserting the data can consume a lot of CPU, memory and disk space. We should always run the backups during the non-peak hours like night.
As already mentioned earlier, we can use import and export functions for backup and restoration of MongoDB databases, but we should use commands like mongodump and mongorestore to backup and restore respectively.
MongoDB backup
We will first cover backing up the MongoDB database. For this we use the mongodump command.
First open the Windows command prompt and go to the location where MongoDB is installed. If you have chosen the default setting, while installing MongoDB though the pop-up it will be installed in a location like
C:\Program Files\MongoDB\Server\4.4\bin
The version number may change if you are reading this blog in the future. Also, note that it’s better to run the command prompt in the Admin mode. So, once we open the command prompt, we need to change the directory to MongoDB bin folder by giving the below command.
cd C:\Program Files\MongoDB\Server\4.4\bin
Now, enter mongod and press Enter. It will show some json text.
Now, we can backup to any location. For this post I am backing up on my Desktop in a Backup folder, which I have created through the command line.
Now, we have to run mongodump command, but it should be also present in our MongoDB bin folder. If it is not present, we need to download it from and install it.
After this, copy the entire exe files from the download to the MongoDB bin folder.
MongoDB Backup with no option
Now, run the mongodump command from the bin directory. Here, we are not giving any argument so the backup of the whole database will be taken in the same bin directory.
MongoDB Backup to an output directory
Now, run the mongodump command from the bin directory. Here, the argument –out specifies the directory in which the data backup will be maintained. In our case we are giving the Backup folder that we had earlier created in the Desktop.
mongodump --out C:\Users\pc\Desktop\Backup
Now, go to the desktop and you can find the backup that has been created in our Backup folder.
MongoDB Backup of a specific database
MongoDB also allows us to backup a specific database from a collection of databases in mongodump using the –db option. I have an ‘example’ database, so to backup only that, I will use the below command.
mongodump --db example --out C:\Users\pc\Desktop\Backup
As you can see in the below output only the example database was backed up.
MongoDB Backup a specific collection
Now, if we want to only backup a specific collection, we need to use the –collection option and give the collection name. Also, note that the database name is mandatory in this case, as MongoDB needs to know which is the database to search for in the collection. I have a products collection within the example database, so to backup only that I will use the below command.
mongodump --db example --out C:\Users\pc\Desktop\Backup –collection products
As, you can see in the below output only the products collection from example database was backed up.
MongoDB Backup from remote MongoDB instances
We can get the backup from remote MongoDB instances also. I have a lot of MongoDB databases for my personal projects on MongoDB atlas, which is the free to use Cloud database for MongoDB. To get a backup of remote databases, we have to use the connection string with –uri parameter. I used the below command.
mongodump --uri "mongodb+srv://xxxx:xxxxxxxxxxx@cluster0.suvl2.mongodb.net/xxxxxDB?retryWrites=true&w=majority" --out C:\Users\pc\Desktop\Backup
You can see in the below output the backup of the remote instance.
MongoDB Backup procedures
We should try to make the backup procedure as automated as possible. One of the best ways is to use a cron job, so that it can run every day. As, discussed earlier it is best to run the backup at night when the database has the least load.
Setting up a cron job is easier on a Linux or a Mac because its Windows equivalent is not as good. Alternatively, you can do install MongoDB in WSL2 for Windows which supports Ubuntu.
Let’s suppose that for a Linux host which has a MongoDB instance running, you want to run the backup at 04:04 am daily. For this, you should open the cron editor in the terminal, by running the below command in the terminal.
sudo crontab –e
Now, in the cron editor, you need to add a command like below for our case.
4 4 * * * mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`
Restoring and migrating a MongoDB database
When we restore the MongoDB database from a backup, we will be able to take the exact copy of the MongoDB information, including the indexes. We restore MongoDB by using the command mongorestore, which works only with the binary backup produced by mongodump.
Now, we have already taken the backup of example database earlier and it is in our Backup folder. We will use the below command to restore it. In the arguments we will specify the name of the database first with –db option. After that with –drop, we make sure that the example database is first dropped. And in the final argument, we specify the path of our backup
mongorestore --db example --drop C:\Users\pc\Desktop\Backup\example
Now, if we check in the terminal, we have our example database restored properly.
Conclusion
In this article, we have learned about MongoDB backup and restore. We have learned the different options for creating the backups, and why and when backups are required. Keep learning!