Open In App

How to Back Up and Restore a MongoDB Database?

Last Updated : 20 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MongoDB is considered one of the classic examples of NoSQL systems. Its documents are made up of key-value pairs, which are the basic unit of data in MongoDB. Whether we're dealing with accidental data loss, hardware failures, or other unforeseen issues, having a solid backup and restoration plan can save our significant time and resources.

In this article, we will learn the process of backing up and restoring MongoDB databases, using practical examples of how to back up entire databases or specific collections, both with and without compression. We’ll cover the process in the context of a MongoDB setup on Ubuntu 20.04.2 LTS.

Understanding MongoDB’s Data Format

MongoDB stores its data using a format called BSON (Binary JSON), which is similar to the widely-used JSON (JavaScript Object Notation) format. BSON offers enhanced performance by enabling faster data processing and searching.

While JSON is human-readable, BSON is more efficient for storing large amounts of data, making it a suitable format for MongoDB's backend storage. Although BSON takes up slightly more space than JSON, the improved speed compensates for this disadvantage.

Setting Up MongoDB Databases and Collections

Before we get into backing up, let's first create some databases and collections to work with. Here’s a simple example. So, let’s connect to our MongoDB and create three databases for backups. They will be DB1, DB2, and DB3. To fill these databases, let’s add some collections.

use DB1
db.createCollection("posts1")
db.createCollection("address1")
db.createCollection("phone1")

use DB2
db.createCollection("posts2")
db.createCollection("address2")
db.createCollection("phone2")

use DB3
db.createCollection("posts3")
db.createCollection("address3")
db.createCollection("phone3")

Output:

After creating the databases and collections, you can check them with the following commands:

use admin
show dbs

Output:

As you can see, our databases are okay. Now let’s check if they have the collections that we have created before.

use DB1
show collections

use DB2
show collections

use DB3
show collections

Output:

All the collections are there. Now we can proceed to the first backup option.

1. Backing Up a Database with Compression

Let’s start by creating a compressed backup of a specific database (DB1) using the mongodump utility. First, create a directory to store your backup:

$ mkdir /tmp/backup_v1

Now use the following command to create a backup:

$ mongodump --host=localhost --gzip --db DB1 --archive=/tmp/backup_v1/backup-db-1.gz

Now, after creating a backup, let’s move to the database directory and see what it has using the following command:

$ ls -la /tmp/backup_v1

Output:

As you can see, our Backup Copy has just been created.

Now, restore data from this kind of backup we use the following command:

$ mongorestore --gzip --archive=/tmp/backup_v1/backup-db-1.gz

In this example, we use only one command to make a backup copy, which is then archived. There are several options for how to make a backup copy.

2. Back up all databases, without data compression

If you prefer to back up all databases without compression, here’s how you can do it.

Create a Directory for the Backup:

$ mkdir /tmp/backup_v2

Run the Backup Command:

$ mongodump --out /tmp/backup_v2

Output:

After successful backup, let’s move to the database directory and see what it has. It has BSON and JSON collections without compression.

$ ls -la /tmp/backup_v2/db1

Output:

Now restore data from this backup using the following command:

$ mongorestore --drop --dir /tmp/backup_v2

Here, the --drop parameter is used to drop a collection before importing (if it exists) to avoid duplicate key errors. This --drop parameter should be used with caution.

Restoring Specific Collections:

Now restore a specific collection (for example, posts1 from DB1) from a backup of all databases:

$ mongorestore --drop --dir /tmp/backup_v2 --nsInclude 'DB1.posts1'

Restore all databases and all collections except for a specific collection (for example, posts1 from DB1):

$ mongorestore --drop --dir /tmp/backup_v2 --nsExclude 'DB1.posts1'

Back up all databases with compression

To back up all databases with gzip compression, use the following steps:

Create a Backup Directory:

$ mkdir /tmp/backup_v3

Run the Command with Compression:

mongodump --gzip --out /tmp/backup_v3

Restore from the Compressed Backup:

$ ls -la /tmp/backup_v3/db1

Here the backup file contains compress files.

Now restore data from this kind of backup:

$ mongorestore --gzip --drop --dir /tmp/backup_v3

Back up all databases with compression into a single archive (.gz)

To create a backup of all the databases with compression into a single archive (.gz):

Create a Backup Directory:

$ mkdir /tmp/backup_v4

Run the Backup Command with Archive:

$ mongodump --gzip --archive=/tmp/backup_v4/my_backup.gz

Now restore data from the backup using the following command:

$ mongorestore --gzip --drop --archive=/tmp/backup_v4/my_backup.gz

Back up a specific database

Create a backup of a specific database

$ mkdir /tmp/backup_v5
$ mongodump --gzip --out /tmp/backup_v5 --db DB2

Restore data from this backup:

$ mongorestore --gzip --drop --dir /tmp/backup_v5

Back up a single address2 collection from DB2

Create a backup a single address2 collection from DB2

$ mkdir /tmp/backup_v6
$ mongodump --gzip --out /tmp/backup_v6 --db DB2 -c address2

Back up the entire DB2 except for a single posts2 collection

Create a back up the entire DB2 except for a single posts2 collection

$ mkdir /tmp/backup_v7
$ mongodump --gzip --out /tmp/backup_v7 --db DB2 --excludeCollection posts2

Conclusion

Backing up and restoring MongoDB data is an essential part of any database management strategy. MongoDB’s mongodump and mongorestore utilities make this process efficient and flexible, whether we are backing up a single collection, an entire database, or even applying compression for more efficient storage. By following the steps outlined in this guide, we can ensure that our MongoDB data is properly safeguarded, enabling fast recovery in the event of data loss or corruption.


Next Article
Article Tags :

Similar Reads