How to Create Backup in MongoDB?

If you are interested to learn about the MongoDB ObjectId

Dump MongoDB Data

Use the mongodump command to create a backup of your MongoDB database. The whole contents of your server will be dumped into the dump directory with this command. There are numerous ways to control the amount of data or make backups of your remote server.

Syntax

The basic syntax of mongodump command is as follows −

>mongodump

Example

Use the mongodump command to create a backup of your MongoDB database.

The whole contents of your server will be dumped into the dump directory with this command. There are numerous ways to control the amount of data or make backups of your remote server.

>mongodump

The command will connect to the server running at 127.0.0.1 and port 27017 and back all data of the server to directory /bin/dump/. Following is the output of the command −

DB Stats

Following is a list of available options that can be used with the mongodump command.

SyntaxDescriptionExample
mongodump –host HOST_NAME –port PORT_NUMBERThis commmand will backup all databases of specified mongod instance.mongodump –host tutorialspoint.com –port 27017
mongodump –dbpath DB_PATH –out BACKUP_DIRECTORYThis command will backup only specified database at specified path.mongodump –dbpath /data/db/ –out /data/backup/
mongodump –collection COLLECTION –db DB_NAMEThis command will backup only specified collection of specified database.mongodump –collection mycol –db test

Restore data

To restore backup data MongoDB’s mongorestore command is used. This command restores all of the data from the backup directory.

Syntax

The basic syntax of mongorestore command is −

>mongorestore

Following is the output of the command −

DB Stats

How to create a backup of all databases

The command used to create a backup in MongoDB is “mongodump“. The following syntax will help you in this regard:

mongodump <options>

To create a backup of all the databases and associated collections, you must execute the following command in your Ubuntu terminal:

You may have noticed that the command is executed in the Ubuntu terminal. It is a universal command that can be executed on several operating systems and is used to create backups in MongoDB.

$ sudo mongodump
Graphical user interface, text, application, chat or text message Description automatically generated

The collections and associated documents present on your MongoDB server are backed up.

How to backup a database in MongoDB

The syntax to create a backup of a database is provided below:

mongodump --db <database-name>

For instance, we want to create a backup for “linuxhint” database. To do so, we have executed the below-stated command in Ubuntu’s terminal:

$ sudo mongodump --db linuxhint
Graphical user interface, text Description automatically generated

It can be observed from the output that, all the collections and documents associated with linuxhint database are successfully backed up after this execution.

How to backup a collection of a database

Sometimes the size of the database is very large and creating a backup for these databases consumes time as well as storage. In such conditions, the administrator will prefer to create a backup of important collections. The mongodump command also allows you to create a backup of a single collection only. To create a backup of a single collection; one must follow the syntax provided below:

mongodump –db <database-name> –collection <collection-name>

Furthermore, the command written below creates the backup of a collection “staff” and this collection belongs to “linuxhint” database:

> mongodump --db linuxhint --collection staff
Text Description automatically generated

As discussed earlier, the default dump address of the MongoDB database and collections is your home directory. However, you can change the backup address by using the “–out” option in “mongodump” command.

mongodump –db linuxhint –out </path/of/location>

For example, we have used the following command to get the backup of “linuxhint” database at our desired address:

> sudo mongodump –db linuxhint –out /adnan/linuxhintdb_backup/

Text Description automatically generated

How to restore backed up databases/collections in MongoDB

Once the backup is created, you must know the way to restore it. In this section, we will demonstrate the way to restore data that includes databases and collections.

The command used to restore the backup has the following syntax:

mongorestore <options>

A single backup command creates backups for all databases. Similarly, a single restore command retrieves all the databases from the backup directory to your MongoDB server. The command stated below restores all the databases at once:

> mongorestore

Text Description automatically generated

Conclusion

Database Management Systems are used to manage the data of an organization and MongoDB is one of the well-known database management systems. The backup phenomenon has a key role in DBMS’s and almost all database administrators update the backup directory regularly. In this descriptive post, we have provided the possible ways to create a backup in MongoDB. Additionally, we have also provided ways to restore the backed-up data. Wrapping up, this guide is equally important for all users of MongoDB. Lastly, it is recommended to create backups of your data, either you are working on databases or any operations related to data management.

How to Create Backup in MongoDB?
Show Buttons
Hide Buttons