×


Backup, Restore and Migrate a MongoDB database on CentOS 8

Are you looking for steps to backup, restore, and migrate a MongoDB database on CentOS 8?

This guide is for you.


MongoDB is a document-oriented NoSQL database used for high volume data storage.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to fix  MongoDB related tasks.

In this context, we shall look into the steps to perform backup, restore, and migrate a MongoDB database on CentOS 8.


How to backup, restore, and migrate a MongoDB database on CentOS 8

Now let us take a look at how to perform backup and restore the MongoDB database.


Step 1: Using JSON and BSON in MongoDB

MongoDB makes use of JSON and BSON (binary JSON) formats for storing its information. JSON is a human-readable format that is perfect for exporting and importing data. We can manage exported data with any tool that supports JSON, including a simple text editor.

An example JSON document looks like this:

{“address”:[
{“building”:”1007″, “street”:”Park Ave”},
{“building”:”1008″, “street”:”New Ave”},
]}


JSON is convenient to work with, but it does not support all the data types available in BSON. So for backing up and restoring, it is better to use the binary BSON.

Moreover, we needn’t worry about creating a MongoDB database explicitly. Because if the database we specify for import does not already exist, it will automatically get created.


Step 2: Using mongodump to Back Up a MongoDB Database

An essential argument to mongodump is –db. It specifies the name of the database we want to back up. In case, if we don’t mention any database name then the mongodump backs up all of the databases.

The second important argument is –out. It defines the directory into which the data will be dumped.

For example, let’s back up the newdb database and store it in the /var/backups/mongobackups directory. Ideally, we will have each of our backups in a directory with the current date like /var/backups/mongobackups/10-29-20.

First, we create that directory /var/backups/mongobackups:

$ sudo mkdir -p /var/backups/mongobackups

Next, we run mongodump.

$ sudo mongodump –db newdb –out /var/backups/mongobackups/`date +”%m-%d-%y”`

We will see an output as below:

2020-11-29T19:22:36.886+0000 writing newdb.restaurants to
2020-11-29T19:22:36.969+0000 done dumping newdb.restaurants (25359 documents)

In the above directory path, we have used date +”%m-%d-%y” which automatically gets the current date. This will create backups inside the directory like /var/backups/11-29-20/.


As a general rule, we must make regular backups. Thus, we can set the mongodump command as a cron job so that it runs regularly, e.g., every day at 03:03 AM.

In order to accomplish this open crontab, cron’s editor:

$ sudo crontab -e

Inside the crontab prompt, we insert the below mongodump command:

3 3 * * * mongodump –out /var/backups/mongobackups/`date +”%m-%d-%y”`

In the above command, we omit the –db argument because we want to have all of our databases backed up.

Depending on our MongoDB database sizes, we may soon run out of disk space with too many backups. That’s why we recommended to clean the old backups regularly or to compress them.

Here is the command that we run to delete all the backups older than seven days.

$ find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;


Similar to the previous mongodump command, we can also add this as a cron job. It should run just before we start the next backup, e.g., at 03:01 AM. For this purpose, open crontab again:

$ sudo crontab -e

After that, we insert the below line:

3 1 * * * find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;

Then we save and close the file.


Step 3: Using mongorestore to Restore and Migrate a MongoDB Database

In order to restore the MongoDB database from a previous backup, we have the exact copy of our MongoDB information taken at a particular time, including all the indexes and data types. For restoring MongoDB, we make use of the command mongorestore.

Now let's continue with our example with the newdb database and see how we can restore it from the previously taken backup. First, we will specify the name of the database with the –nsInclude argument. Next, we will be using newdb.* to restore all collections. To restore a single collection such as restaurants, we use newdb.restaurants instead.


Then, we use –drop to make sure that the target database is first dropped so that the backup is restored in a clean database.

Finally, we restore using the below command.

$ sudo mongorestore –db newdb –drop /var/backups/mongobackups/11-29-20/newdb/

[Need any further assistance with MongoDB Tasks? – We are here to help you.]


Conclusion

MongoDB is highly scalable, using shards. Horizontal scalability is a big plus in most #NoSQL databases. MongoDB is no exception. It is also highly reliable due to its replica sets, and the data is replicated in more nodes asynchronously.

This article will guide you on the steps to perform #backup, #restore and #migrate a #MongoDB #database on #CentOS 8.