MongoDB Backup and Restore

Last update on April 14 2018 06:14:57 (UTC/GMT +8 hours)

Introduction

The Backup and Restore in MongoDB is an important part to handle a database. This utility is important because your valuable data can be removed or crashed or corrupted from your server, your local machine by accidentally or by any mistake, whatever it may be. On that vital moment, you can keep safe by restoring your data if you have a backup of your valuable data. Backup can be done in various format. Here, in this article, we will describe the process for creating backups and restoring data using the utilities provided with MongoDB

MongoDB Backup

Backup a Database with mongodump

The backup utility of MongoDB can create a backup for an entire server, database or collection, or you can use a query to backup for a part of a collection.

When mongodump only runs without any arguments, the command connects to the MongoDB instance on the local system (e.g. localhost) on port 27017 and creates a database backup named dump/ in the current directory.

Here is the command to backup data from a mongod or mongos instance running on the same machine and on the default port of 27017.

To take the backup you have to enter the bin directory into the mongodb folder on your machine. Here our path is D:\mongodb\bin and you have to execute the command from here.

N.B. : The mongodump command overwrites output files if they already exist in the backup data folder. So, it is better to ensure that, you no longer need the files in the output folder or you have taken a copy of the previous backup folder, before to run the mongodump command multiple times.

Backup a Database with mongodump specify different output directory

You can take the backup in specific directory by using the --out or -o option. Here in the example below we mention the directory under D: is backup_data/backup/ folder.

N.B. : The mongodump command overwrites output files if they already exist in the backup data folder. So, it is better to ensure that, you no longer need the files in the output folder or you have taken a copy of the previous backup folder, before to run the mongodump command multiple times.

Backup of a specific collection and a specific database using mongodump

If you want to take the backup of a specific database and a specific collection using mongodump command you can specify --db and --collection as options to mongodump.

Here in the example below our database is myinfo and collection is userdetails .

D:\mongodb\bin>mongodump --collection userdetails --db myinfo

The operation above will create a dump of the collection named userdetails from the database myinfo in a dump/ subdirectory of the current working directory. Here is the output

N.B. : The mongodump command overwrites output files if they already exist in the backup data folder. So, it is better to ensure that, you no longer need the files in the output folder or you have taken a copy of the previous backup folder, before to run the mongodump command multiple times.

Create Backups from Non-Local mongod Instances

In this type of backup the --host and --port options for mongodump allow the user to connect to and backup from a remote host. Here is the command like:

You can open this file in Excel. Here is the file showing in the mention directory and the data exported in .csv format.

Export in CSV Format using file containing field list

You can also specify the fields in a file containing the line-separated list of fields to export and use this .txt file along with --fieldFile option in the mongoexport command.

Here is the myfields.txt file like below. Open a Notepad editor and write the following text, only one field in one line, then save the file in a name as you like in the \mondodb\bin\ folder and use this name in the command. Here our file name is myfields.txt and we have used this name.

Export in JSON Format

The example below exports the collection userdetails from the MongoDB instance running on the localhost port number 27017, with journaling explicitly enabled. This command writes the exported data to the file newuserdetails.json in JSON format.

Export from Remote Host Running with Authentication

The example below exports the collection userdetails from the database myinfo, which requires authentication. Assume that this data resides on the MongoDB instance located on the host [your_domain.example.com] running on port 37017, which requires the username [name of the user] and the password [your password].

Our collection name is userdetails and db is myinfo and the location and the backup file name is D:\backup_data\backup\newuserdetails.json

MongoDB Restore

The restore utility using mongorestore command restores a binary backup created by mongodump. The mongorestore can restore either an entire database backup or a subset of the backup.

The following command will import the database backup in the dump folder to the mongod instance running on the localhost interface. Here is the command. Here we did not mention the folder name, so by default, the database backup in the dump folder will be imported.

The following command will import the database backup in the myinfo folder under the dump folder to the mongod instance running on the localhost interface. Here is the command. Here we mention the folder name, so the database backup from the mention folder will be imported.

The mongorestore command by default connects to a MongoDB instance running on the localhost interface with default port (27017). We can also restore the backup to a different host or port by using the --host and --port options.

The command mongoimport restores a database from a backup taken with mongoexport.

Here in the example assume that our backup file name is newuserdetails.JSON, db name is myinfo and collection name is userdetails. In the following example, mongoimport imports the data in the JSON data from the newuserdetails.json file into the collection userdetails in the myinfo database.

The mongoimport command can also be used to import data on a remote MongoDB database with authentication enabled. Here in the example below the mongoimport imports the data from the file \backup_data\backup\newuserdetails.json into the collection userdetails within the database myinfo in a remote MongoDB database

The mongoimport can imports the csv formatted data into the collection. An option --headerline is used to instructs mongoimport to determine the name of the fields using the first line in the CSV file.

Here in the example assume that our backup file name is userdetails.csv located in the folder \backup_data\backup\, db name is myinfo and collection name is userdetails.