MongoDB - Create a backup

MongoDB - Create a backup

MongoDB Data Dump

To back up a database in MongoDB, you must use the mongodump command . This command will dump all your server data into the dump directory. There are many options available through which you can limit the amount of data or back up your remote server.

Syntax

The basic syntax of the mongodump command is as follows:

>mongodump

example

Start your Mongod server. Assuming your mongod server is running on localhost and port 27017, open a command prompt and navigate to the bin directory of your mongodb instance and issue the mongodump command

Consider the collection mycol to have the following data.

> mongodump

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

D.B. Stats

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

Syntax Description example
mongodump --host HOST_NAME --port PORT_NUMBER This command will back up all databases of the specified mongod instance. mongodump --host tutorialspoint.com --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY This command will only backup the specified database to the specified path. mongodump --dbpath/data/db/ --out/data/backup/
mongodump - COLLECTION - DB_NAME This command will only back up the specified collection of the specified database. mongodump --collection mycol --db test

Restore data

The MongoDB mongorestore command is used to restore backup data . This command restores all data from the backup directory.

Syntax

The basic syntax of mongorestore command is −

>mongorestore

Following is the output of the command −