paint-brush
How to Back Up and Restore a MongoDB Databaseby@shepelev
5,074 reads
5,074 reads

How to Back Up and Restore a MongoDB Database

by Alexey ShepelevNovember 14th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The guide was developed on a computer with the Ubuntu 20.04.2 LTS operating system. MongoDB is a database that emerged in the mid-2000s. 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. The MongoDB data format is called BSON (binary JSON) and is used to store data. To make a backup copy, log in as root and create a directory for storing backup:
featured image - How to Back Up and Restore a MongoDB Database
Alexey Shepelev HackerNoon profile picture

Today I would like to tell my readers about a database management system that requires no table schema description. That’s right – I’m talking about MongoDB. A DBMS 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. The installation instructions can be found in the official guide below.

https://docs.mongodb.com/v5.0/administration/install-community/

In this article, I would like to show you 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. To store data in MongoDB, a format called BSON (binary JSON) is used.

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, the high speed of BSON fully compensates 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, to 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 back 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 compressed files.

Now, to 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

I hope this article will help you understand how to work with this DBMS, and you can successfully deal with the task if necessary. Thank you all for reading this article!