Open In App

How to Back Up and Restore a MongoDB Database?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
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. Its collections contain groups of documents and functions that are equivalent to tables in relational databases. MongoDB is a database that emerged in the mid-2000s. In this article, we will learn how to back up data and then restore it. The guide was developed on a computer with the Ubuntu 20.04.2 LTS operating system.

MongoDB data format

One of the popular standards for data exchange and storage is JSON(JavaScript Object Notation). JSON can effectively describe data with complex structures. In this regard, the way of storing data in MongoDB is similar to JSON, although JSON is not technically used. Instead of JSON MongoDB uses BSON(binary JSON) to store data in MongoDB. BSON makes working with data faster. It provides faster searching and processing. Although, it should be noted that BSON, in contrast to JSON, has a slight drawback. In general, JSON data takes up less space than that in BSON; on the other hand, high speed would fully compensate for this disadvantage.

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")

We’ve created our databases. Now let’s check if everything is fine:

use admin
show dbs

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

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

To make a backup copy, log in as root. Now create a directory for storing 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

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. In this article, we will discuss them one by one.

Back up all databases, without data compression.

Let’s create a directory for storing backup:

$ mkdir /tmp/backup_v2

Use the following command to backing up:

$ mongodump --out /tmp/backup_v2

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

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.

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

Now create a backup and compress the backup file:

$ mkdir /tmp/backup_v3
$ mongodump --gzip --out /tmp/backup_v3

Now view the backup file:

$ 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)

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

$ mkdir /tmp/backup_v4/
$ 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



Last Updated : 08 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads