MongoDB Create Backup

Advertisements


Dump MongoDB Data

To create backup of database in mongodb you should use mongodump command. This command will dump all data of your server into dump directory. There are many options available by which you can limit the amount of data or create backup of your remote server.

Syntax:

Basic syntax of mongodump command is as follows

>mongodump

Example

Start your mongod server. Assuming that your mongod server is running on localhost and port 27017. Now open a command prompt and go to bin directory of your mongodb instance and type the command mongodump

Consider the mycol collectioin has following data.

>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/. Output of the command is shown below:

DB Stats

There are a list of available options that can be used with the mongodump command.

This command will backup only specified database at specified path
SyntaxDescriptionExample
mongodump --host HOST_NAME --port PORT_NUMBERThis commmand will backup all databases of specified mongod instance.mongodump --host tutorialspoint.com --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORYmongodump --dbpath /data/db/ --out /data/backup/
mongodump --collection COLLECTION --db DB_NAMEThis command will backup only specified collection of specified database.mongodump --collection mycol --db test

Restore data

To restore backup data mongodb's mongorestore command is used. This command restore all of the data from the back up directory.

Syntax

Basic syntax of mongorestore command is

>mongorestore

Output of the command is shown below:

DB Stats

Advertisements
Advertisements