Bobcares

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL EXTENSION

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL EXTENSION

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

Backup, Restore and Migrate a MongoDB database on CentOS 8

by | Dec 26, 2020

Are you looking for steps to backup, restore, and migrate a MongoDB database on CentOS 8? Take a look at this blog for steps.

Here at Bobcares, we have seen several such MongoDB related queries as part of our Server Management Services for web hosts and online service providers.

Today we’ll see how to backup, restore, and migrate a MongoDB database on CentOS 8.

 

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

Now let’s take a look at how our Support Engineers 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 queries? – We are here to help you.]

 

Conclusion

In today’s writeup, we saw how our Support Engineers back up, restore and migrate a MongoDB database on CentOS 8.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.