MongoDB Tutorial - MongoDB Backup








To backup MongoDB, use mongodump command, which dumps all data into dump directory.

Syntax

Basic syntax of mongodump command is as follows

>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/.

Example

The following syntax shows how to indicate the host and port.

mongodump --host HOST_NAME --port PORT_NUMBER

The following commmand will backup all databases of specified mongod instance.

mongodump --host yourserver.com --port 27017

The following syntax shows how to set the data folder and backup folder.

mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY

The following command will backup only specified database at specified path.

mongodump --dbpath /data/db/ --out /data/backup/

The following syntax shows how to backup collections from a database.

mongodump --collection COLLECTION --db DB_NAME

The following command will backup only specified collection of specified database.

mongodump --collection mycol --db test




Restore data

To restore backup datam, use MongoDB's mongorestore command.

The basic syntax of mongorestore command is

>mongorestore