Complete News World in United States

How To Back Up, Restore, and Migrate a MongoDB Database

In style amongst each enterprises and startups, MongoDB is a database that’s completely suited to web-apps that must scale as soon as the consumer base will increase. MongoDB is totally different from conventional relational databases as a result of it makes use of json like objects to retailer knowledge, as an alternative of tables in relational databases. 

On this submit, we’ll study to backup and restore a MongoDB database. In all software program merchandise there may be an import and export function, which in database time periods, offers with human-readable format. On the opposite hand, the backup and restore operations use MongoDB particular knowledge, which protect the MongoDB attributes.  

So, when migrating the database, we should always choose backup and restore over import and export. However we must also needless to say our supply and goal methods are suitable, which implies that each must be Windows or each must be a Linux primarily based system like Ubuntu/Mac. 


We’re utilizing Home windows 10 on this tutorial. Please be sure to have downloaded the MongoDB Neighborhood Server and put in in it. It’s a very simple setup and you will see that lot of excellent articles on the web detailing this outPlease ensure that you’ve added it within the Setting variable in your PC. 

Backup Issues 

In a manufacturing surroundings, backups act as a snapshot of the database at a sure level. Massive and complicated databases do fail or will be hacked. If that occurs, we will use the final backup file to revive the database to the level, earlier than it failed. These are a few of the components which must be considered when doing a restoration. 

 1. Restoration Level Goal 

We should always know the target of the restoration level, which implies how a lot knowledge we’re prepared to lose throughout a backup and restoration. A steady backup is most popular for crucial knowledge like financial institution info and backups must be taken a number of instances in the course of the day. Alternatively, if the information doesn’t change ceaselessly, then the backup will be taken each 6 months. 

 2. Restoration Time Goal

This tells how rapidly the restoration will be achieved. Throughout restoration the appliance might be down for a while; and this downtime must be minimized, or else buyer relationships might be misplaced. 

 three. Database and Snapshot Isolation

This refers to the space between the first database server and the backup server. If they’re shut sufficient i.e., in the identical constructing, then the restoration time reduces. Nonetheless, within the occasion of a bodily occasion similar to a fireplace, there’s a chance of it been destroyed with the first database.  

 four. Restoration Course of 

We ought to at all times take a look at our backups in take a look at servers to see if they are going to work, in case a restoration is required. 

 5. Out there Storage 

Backup of database usually takes plenty of house and in most instances, it’s going to by no means be required. So, we should always attempt to decrease the house taken on the disk, by archiving the database into a zipper file. 

 6. Complexity of Deployment

The backup technique must be simple to set and must be automated, in order that we don’t have to recollect to take the backup after common intervals. 

Understanding the Fundamentals 

The very first thing that we should always know is that MongoDB makes use of json and bson(binary json) codecs for storing knowledge. So, individuals coming from a JavaScript background can relate to things for json, which have a key-value pair. Additionally, json is the popular format wherein we obtain or ship knowledge to an API endpoint. 

You’ll be able to verify the json knowledge of a MongoDB database in any device or on-line editors. Even the well-known Home windows utility Notepad++ has a json viewer. 

An instance of a json doc seems like under. Back Up, Restore, and Migrate a MongoDB Database

As we will see from the above instance, json could be very handy to work with, particularly for builders.  However it doesn’t help all the information sorts out there in bson. So, for backup and restoring, we should always use binary bson. 

The second factor to remember is that MongoDB robotically creates databases and assortment titles in the event that they don’t exist throughout restore operations. 

Third, since MongoDB is a document-based database, in lots of consumer instances we retailer great amounts of information in a single assortment, similar to the entire submit of an article. MongoDB can be used extensively in massive databases and massive knowledge. So, studying and inserting the information can devour plenty of CPU, reminiscence and disk house. We should always at all times run the backups in the course of the non-peak hours like evening. 

As already talked about earlier, we will use import and export capabilities for backup and restoration of MongoDB databases, however we should always use instructions like mongodump and mongorestore to backup and restore respectively. 

MongoDB backup 

We’ll first cowl backing up the MongoDB database. For this we use the mongodump command.  

First open the Home windows command immediate and go to the situation wherein MongoDB is put in. You probably have selectedn the default setting, whereas putting in MongoDB although the pop-up will probably be put in in a location like 

C:Program FilesMongoDBServer4.4bin 

The model quantity solely will change if you’re studying this weblog sooner or later. Additionally, word that it’s higher to run the command immediate in the Admin mode. So, as soon as we open the command immediate, we have to change the listing to MongoDB bin folder by giving the under command. 

cd C:Program InformationMongoDBServer4.4bin

Back Up, Restore, and Migrate a MongoDB Database

Now, enter mongod and press enter. It would present some json textual content.

Back Up, Restore, and Migrate a MongoDB Database

Now, we will backup to any location. For this submit I’m backing up on my Desktop in a Backup folder, which I’ve created by the command line.Back Up, Restore, and Migrate a MongoDB Database

Now, now we have to run mongodump command, but it surely must be additionally current in our MongoDB bin folder. If it isn’t current, we have to download it from and set up it. Back Up, Restore, and Migrate a MongoDB DataBack Up, Restore, and Migrate a MongoDB Databasebase

After this, copy the whole exe information from the obtain to the MongoDB bin folder. Back Up, Restore, and Migrate a MongoDB Database

MongoDB Backup with no possibility 

Now, run the mongodump command from the bin listing. Right here, we aren’t giving any argument so the backup of the entire database might be taken in the identical bin listing.Back Up, Restore, and Migrate a MongoDB Database

MongoDB Backup to an output listing 

Now, run the mongodump command from the bin listing. Right here, the argument –out specifies the listing wherein the information backup might be maintained. In our case we’re giving the Backup folder in the  Desktop, which now we have created earlier. 

mongodump --out C:UserspcDesktopBackup 

Back Up, Restore, and Migrate a MongoDB Database

Now, go to the desktop and you will discover the backup that has been created in our Backup folder.  Back Up, Restore, and Migrate a MongoDB Database

MongoDB Backup a particular database 

Now, we will additionally backup solely a database in mongodump utilizing the –db possibility. I’ve an instance database, so to backup solely that I’ll use the under command. 

mongodump --db instance --out C:UserspcDesktopBackup 

As, you may see within the under output solely the instance database was agained up. Back Up, Restore, and Migrate a MongoDB Database

MongoDB Backup a particular assortment 

Now, if we need to solely backup a particular assortment, we have to use the –assortment possibility and provides the gathering title. Additionally, word that the database title is necessary on this case, as mongodb must know in regards to the database to seek for the gathering. I’ve a merchandise assortment throughout the instance database, so to backup solely that I’ll use the under command. 

mongodump --db instance --out C:UserspcDesktopBackup –assortment merchandise 

As, you may see within the under output solely the merchandise assortment from instance database was agained up. Back Up, Restore, and Migrate a MongoDB Database

MongoDB Backup from distant MongoDB situations 

We will get the backup from distant mongodb situations additionally. I’ve so much of MongoDB databases for my private tasks on MongoDB atlas, which is the free to make use of Cloud database for MongoDB. To get a backup of distant databases, now we have to make use of the connection string with –uri parameter. I used the under command. 

mongodump --uri "mongodb+srv://xxxx:xxxxxxxxxxx@cluster0.suvl2.mongodb.internet/xxxxxDB?retryWrites=true&w=majority" --out C:UserspcDesktopBackup 

You can see within the under output the backup of the distant occasion. Back Up, Restore, and Migrate a MongoDB Database

MongoDB Backup procedures 

We should always attempt to make the backup process as automated as attainable. Top-of-the-line methods is to make use of a cron job, in order that it could possibly run every single day. As, mentioned earlier it’s best to run the backup in the evening when the database has the least load.  

Establishing a cron job is less complicated on a Linux or a Mac as a result of the Windows equal of it isn’t good. Alternatively, you are able to do set up mongodb in WSL2 for Windows which helps Ubuntu 

Suppose, on a Linux host which has a mongoDB occasion operating, you need to run the backup at 04:04 am every day. For this within the terminal, open the cron editor by operating the under command in the terminal. 

sudo crontab –e 

Now, within the cron editor, it’s essential to add command like under for our case. 

four four * * * mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`

Restoring and migrating a MongoDB database 

After we restore the MongoDB database from a backup, we will take the precise copy of the MongoDB info, together with the indexes. We restore MongoDB through the use of the command mongorestore, which works solely with the binary backup produced by mongodump. 

Now, now we have taken the backup of instance database earlier and it’s in our Backup folder. We’ll use the under command to revive it. Within the arguments we’ll specify the title of the database first with –db possibility. After that with –drop, we make it possible for the instance database is first dropped. And within the remaining argument, we specify the trail of our backup. 

mongorestore --db instance --drop C:UserspcDesktopBackupexample 

Now, if we verify in terminal, now we have our instance database restored correctly. Back Up, Restore, and Migrate a MongoDB Database


On this article, now we have discovered about MongoDB backup and restore. We’ve discovered the totally different choices for the backups, and why and when backups are required. Continue to learn!