MongoDB backup and restore

In this MongoDB tutorial, we will learn how to create backup and restore databases in MongoDB on different operating systems. these are the topics we are going to cover

  • How does we create the backup of database in Windows
  • How does we restore the backup of database in Windows
  • How does we create backup and restore of database in Ubuntu
  • How does MongoDB Atlas backup and restore
  • How does MongoDB backup and restore by sharded cluster

How does we create a backup of database in Windows

Why do we backup

The main reason for the backup is to make a copy of the data so that we can use it in the future if our primary data fails. There are various reasons in which our primary data may get fails:

  • Data corruption
  • Accidental deletion of data
  • Hardware and software fails
  • Malicious attack (virus or malware)

For doing the backup of a database you need to follow these steps:

  • Open command prompt and start the shell of mongoDB
how we can start mongodb server in windows
MongoDB shell
  • Check all the database that is in MongoDB by using command show dbs.
>show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mydatabase  0.171GB
test        0.201GB

As you can see here, these are the two databases that we created mydatabase and test and the rest are the default database that was created automatically at the time of installation.

  • MongoDB provides a tool called as mongodump for creating data backup.
  • It dumps all the data into dump directory of MongoDB.
  • It backup the data in BSON format is known as BSON dump data.
  • It is stored in mongodb’s bin\dump folder.

We can backup two ways using Mongodump and both the ways are given below.

Read MongoDB group by count

Without arguments:

You simply need to write mongodump on MongoDB shell, it will create the backup for every database and every collection.

C:\Program Files\MongoDB\Server\5.0\bin>mongodump
Backup by using mongodump
Backup by using mongodump
backup database using mongodump
after backup all the databases using mongodump

With arguments:

Using argument can restrict the amount of data like which database and collection you want to backup.

mongodump --db database_name --collection collection_name --out path
backup a specific database using mongodump
backup a specific database using mongodump

You can also specify the path like where we want to store the backup of the database. As you can see in the above example –out after that we specify the path.

Here you can see backup is created on the specified location.

backup of specific database
specific database backup by mongodump

Read: How to install MongoDB

How does we restore backup of the database in Windows

In MongoDB, the purpose of restoring the data because sometimes our primary data may get deleted due to

  • Data corruption
  • Accidental deletion of data
  • Hardware and software fails
  • Malicious attack (virus or malware)

For data restoration, we use the mongorestore tool. With the help of this, we can easily restore all the backup databases.

You can restore either all the databases backup or a specific database that you want to restore from the backups.

To restore all the databases you can use this command

mongorestore dump
how to restore all the database in  mongodb
restores all the databases using mongorestore
>show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mydatabase  0.171GB
test        0.201GB

To restore a specific database or collection you can use this command

mongorestore  –db database_name –collection collection_name directory\collection_name.bson
restore the specific database using mongorestore
restore the specific database using mongorestore
> show databases
admin       0.000GB
config      0.000GB
local       0.000GB

test        0.201GB

This way you can easily restore the database.

Read: How to create new a database in MongoDB

How does we create backup and restore of the database in Ubuntu

Backup MongoDB database in Ubuntu

You will learn how you can Backup a database in Ubuntu. As you already know, we use the mongodump command in windows for backup so here we also use the same command to take backups of all databases or a single database backup, single collection backup.

Backup of a single database

For backup of a single database, we use this command

$mongodump --db world --out /backup/mongo/

Here, the world is the database name and a backup of this database will be created in the /backup/mongo directory.

Note

  • -db:- database name for the backup.
  • -out:- Location of the backup database, this will create a folder with database name.

Backup of a single collection

With the help of the following command, you can take backup of a single collection

$mongodump --collection mydb --db world --out /backup/mongo/

Here, backup files will be created in the /backup/mongo/world directory.

Backup of all databases

For backup of all the databases, you need to use this command

$mongodump --out /backup/mongo

With the help of this command, you can easily backup all the databases. Here, /backup/mongo is the location of the backup directory.

Restore MongoDB database in Ubuntu

mongorestore tool is used to restore the MongoDB database backup. You can restore with the help of the below command

$mongorestore --db world --drop /backup/mongo/world

Here, –drop will remove the database if already exist.

Read MongoDB aggregate $count

How does MongoDB Atlas backup and restore

In MongoDB Atlas, we can do backup and restore by using a cloud service provider. These are some of the hosting platforms that are supported by MongoDB Atlas:

  1. Google Cloud Playeform (GCP)
  2. Amazon Web services (AWS)
  3. Microsoft Azure
  • The restore function in MongoDB Atlas will help the user to restore a duplicate set. The target user uses the same version of MongoDB or a newer version than the duplicate set is supported.

The MongoDB Atlas backup feature fastly backup the data into a given cluster. You can easily restore it at any time with the help of cloud backup and cloud restore function.

Some of the points you need to remember while Backup and Restore

  • MongoDB supports both JSON and BSON(Binary JSON) file formats.
  • For backup and restore always use BSON because it support all the datatypes.
  • You don’t need to create database manully as it create automatically we you define it in the import from and on the same way for the collection when you insert doument into the database it will create automatically.
  • Sometimes Backup takes a long time, when data set are quite large.

Read: How does MongoDB create a collection

How does MongoDB backup and restore by sharded cluster

What is MongoDB sharding?

When the size of data increases, a single machine is not enough to store the data and also does not provide an acceptable read and write throughput.

We can solve the problem of sharding with the help of horizontal scaling. With sharding, if we want to add more machines to support data growth and the request of reading and write operations.

Sharded cluster provides the atomicity guarantees of transaction shards given below processes

  • Backup a sharded cluster
  • Restore a sharded cluster

This can be done with the help of

  • MongoDB Atlas
  • MongoDB cloud Manager
  • MongoDB ops Manager

You may also like reading the following articles.

In this article, we learned how to backup and restore databases in MongoDB using different operating systems. These are some more topics that we learned in this article:

  • How does we create the backup of database in Windows
  • How does we restore the backup of database in Windows
  • How does we create backup and restore of database in Ubuntu
  • How does MongoDB Atlas backup and restore
  • How does MongoDB backup and restore by sharded cluster