English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Create Backup in MongoDB

In this chapter, we will see how to create backups in MongoDB.

MongoDB Data Dump

To create a database backup in MongoDB, you should use the mongodump command. This command will dump the entire data of the server into the dump directory. There are many options available to limit the amount of data or create a backup of a remote server.

Syntax

mongodump The basic syntax of the command is as follows-

>mongodump

Example

Start your mongod server. Assume that your mongod server is running on the local host and port27017Please open the command prompt and then navigate to the bin directory of the mongodb instance, and then enter the commandmongodump

The 'mycol' collection has the following data.

>mongodump

This command will connect to the server running at127.0.0.1and the server running on the port, and the server's27017All data is returned to the directory/bin/dump/. The following is the command output-

The following is the command output that can be used with themongodumpList of available options used with the command.

SyntaxDescriptionExample
mongodump --host HOST_NAME --port PORT_NUMBER

This command will backup all databases of the specified mongod instance.

mongodump --host oldtoolbag.com --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORYThis command will only backup the specified database on the specified path.mongodump --dbpath /data/db/ --out /data/backup/
mongodump --collection COLLECTION --db DB_NAMEThis command will only backup the specified collection of the specified database.mongodump --collection mycol --db test

Restore Data

Use the MongoDB mongorestore command to restore backup data, which restores all data in the backup directory.

Syntax

mongorestoreThe basic syntax is-

>mongorestore

The following is the output of the command-