Bobcares

WeSupport

Call Us! 1-800-383-5193
Call Us! 1-800-383-5193
Call Us! 1-800-383-5193

Need Help?

Emergency Response Time custom

Our experts have had an average response time of 12.45 minutes in December 2020 to fix urgent issues.

We will keep your servers stable, secure and fast at all times for one fixed price.

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 *

Categories:

Tags:

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF