English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this chapter, we will see how to create backups in MongoDB.
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.
mongodump
The basic syntax of the command is as follows-
>mongodump
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.1
and the server running on the port, and the server's27017
All 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 themongodump
List of available options used with the command.
Syntax | Description | Example |
---|---|---|
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_DIRECTORY | This command will only backup the specified database on the specified path. | mongodump --dbpath /data/db/ --out /data/backup/ |
mongodump --collection COLLECTION --db DB_NAME | This command will only backup the specified collection of the specified database. | mongodump --collection mycol --db test |
Use the MongoDB mongorestore command to restore backup data, which restores all data in the backup directory.
mongorestore
The basic syntax is-
>mongorestore
The following is the output of the command-