Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB Backup (mongodump) and Restore (mongorestore)

MongoDB Backup (mongodump) and Restore (mongorestore)


MongoDB data backup

In MongoDB we use the mongodump command to back up MongoDB data. This command can export all data to a specified directory.

The mongodump command can specify the level of data to export and the server to dump to through parameters.

Syntax

The mongodump command script syntax is as follows:



>mongodump -h dbhost -d dbname -o dbdirectory

Example

Start your mongod service locally using 27017. Open a command prompt window, enter the bin directory of the MongoDB installation directory, and type the command mongodump:



>mongodump

After running the above command, the client connects to the MongoDB service with IP 127.0.0.1 and port 27017, and backs up all data to the bin/dump/ directory. The command output is as follows:

MongoDB data backup

The list of optional parameters for the mongodump command is as follows:

Syntax Description Example
mongodump --host HOST_NAME --port PORT_NUMBER This command backs up all MongoDB data mongodump --host runoob.com --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY mongodump --dbpath /data/db/ --out /data/backup/
mongodump --collection COLLECTION --db DB_NAME This command backs up the collections of the specified database. mongodump --collection mycol --db test

MongoDB data restoration

MongoDB uses the mongorestore command to restore backed-up data.

Syntax

The mongorestore command script syntax is as follows:



>mongorestore -h <:port> -d dbname 

Next, we execute the following command:



>mongorestore

The output of the above command is as follows:

MongoDB Data Recovery