MongoDB

MongoDB Backup and Restore Example

Hello readers, these days it is very important to protect the data against any damage if a disaster or the unforeseen outages. Thus, irrespective of the database type, it is important to keep up the regular data backups to make data available during the server crash or data corruption. In this tutorial, we will see how to use the Backup and Restore features of the MongoDB database.

1. Introduction

If you have installed the MongoDB application on Windows or Ubuntu operating system and you wish to learn about backing up and restore features of MongoDB then follow the below steps to prevent loss of data. It’s very simple to use these utilities, but before moving further let’s take a look at MongoDB and its features.

1.1 What is MongoDB?

  • MongoDB is a high-performance NoSQL database where each database has collections which in turn contains documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (BSN)
  • The documents in MongoDB doesn’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
  • Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily
  • This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability

1.2 Why MongoDB?

  • As a NoSQL type database, MongoDB stores the data in the form of a document. Thus, MongoDB offers more flexibility
  • This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents
  • MongoDB offers indexes in order to improve the search performance within the NoSQL database
  • To offer horizontal scalability, MongoDB uses sharding by splitting the data across the numerous MongoDb occurrences
  • Replication: MongoDB can give high availability with the replica sets

2. MongoDB Backup and Restore Example

In this MongoDB tutorial, we will learn how to handle the Backup and Restore utilities provided by MongoDB.

2.1 Backing up the Data

In MongoDB, to create an entire database backup, developers should use the mongodump command. This command nicely takes a snapshot of the entire database and place it in the dump directory of the MongoDB. When the mongodump command runs without any input arguments, it connects to the local-host MongoDB instance on port 27017 and creates a database backup in the dump directory. Here is what the syntax will look like.

Syntax

> mongodump

Let’s understand this with the help of an example.

2.1.1 Start MongoDB

Start a standalone mongod instance as shown below.

Fig. 1: Start MongoDB instance
Fig. 1: Start MongoDB instance

2.1.2 Connect to the Mongo Instance

Connect with the mongo shell to make a connection with the MongoDB instance on port 27017 as shown below.

Fig. 2: Connect to MongoDB
Fig. 2: Connect to MongoDB

2.1.3 Check Databases

Check how many databases exist on the server by executing the show dbs command as shown below.

Fig. 3: MongoDB databases
Fig. 3: MongoDB databases

Here udemy and employee are the two sample databases that we have created and each of them has collections.

2.1.4 Data backup

Now, go back to the administration window and type the mongodump command to activate the data backup as shown below.

Fig. 4: MongoDB backup utility
Fig. 4: MongoDB backup utility

The MongoDB backup utility will create the database backup named dump in the current directory. In this folder, we will have one folder each for the databases that we have.

Fig. 5: Backup directory
Fig. 5: Backup directory

Do remember, this command overwrites the existing output files in the backup data folder. The below list shows the options that can be used with the mongodump command.

SyntaxDescriptionExample
mongodump --host HOST_NAME --port PORT_NUMBERThis command takes the databases backup of the specified MongoDB instance.mongodump --host hp-PC --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORYThis command will take the databases backup at the specified path.mongodump --dbpath /data/db/ --out /mongodb_backup/data/
mongodump --collection COLLECTION --db DB_NAMEThis command will take the backup of a specified collection of a particular database.mongodump --collection umongo --db udemy
mongodump --db DB_NAMEThis command will take the backup of a particular database only.mongodump --db udemy

2.2 Restoring the Data

Let’s say something bad happened at night and the business data was deleted. In MongoDB, the restore utility restores the data created by the mongodump. The mongorestore command restores an entire database or a subset of the backup and this command imports the database backup from the dump directory to the mongod instance. Here is what the syntax will look like.

Syntax

> mongorestore

Let’s understand this with the help of an example.

2.2.1 Drop Databases

Go to the mongo shell and drop the udemy and employee databases. We will drop the two databases by executing the db.dropDatabase() command as shown below.

Fig. 6: Dropping the databases
Fig. 6: Dropping the databases

2.2.2 Restoring Databases

In this step, we will go ahead and restore the two databases. Go back to the administration window and type the mongorestore command to restore the databases from the backup directory.

Fig. 7: Restoring the databases
Fig. 7: Restoring the databases

This command goes inside the dump directory, checks for the databases and restores them to the mongod instance.

2.2.3 Check Restored Databases

Now, go back to the mongo shell and we will check the restored databases by executing the show dbs command as shown below.

Fig. 8: Check databases
Fig. 8: Check databases

The below list shows the options that can be used with the mongorestore command.

SyntaxDescriptionExample
mongorestore --db DB_NAME COLLECTION_PATHThis command will restore a particular database from the backup location.mongorestore --db udemy dump/udemy
mongorestore --db DB_NAME --collection COLLECTION_NAME COLLECTION_PATHThis command will restore a particular collection of a specified database from the backup location.mongorestore --db udemy --collection umongo dump/udemp/umongo.bson

That’s all for this post. Happy Learning!!

3. Conclusion

In this tutorial, we learned about the data backup and restore features of the MongoDB. Developers can download the sample commands in the Downloads section.

4. Download the Eclipse Project

This was an example of Backup and Restore utilities of the MongoDB.

Download
You can download the full source code of this example here: MongoDbBackupAndRestoreEx

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button