left-icon

MongoDB 3 Succinctly®
by Zoran Maksimovic

Previous
Chapter

of
A
A
A

CHAPTER 11

Back Up and Restore

Back Up and Restore


In this chapter, you will learn how to back up and restore a database in MongoDB.

Back up

MongoDB provides a tool called mongodump.exe to back up your MongoDB database. By calling the mongodump --help, you will get quite a big list of options that can be used.

The most basic example would be backing up a database into a specific folder, which can be achieved as follows:

Code Listing 114: mongodump usage

mongodump -h localhost --db mydb -o c:\backup

Where:

  • -h represents the host where the MongoDB runs.
  • --db represents the database to be backed up.
  • -o contains the output folder.

Backup command.

Figure 47: Backup command.

After running the command on the filesystem, we can see that the data has been exported correctly, and that MongoDB has created a folder called mydb.

 Backup folder.

Figure 48: Backup folder.

The backup folder contains two types of files: .bson, which contains a bson copy of the data (bson being in binary format is not human-readable), and *metadata.json, which contains the configuration information of the collection itself, such as indexes.

Restore

After backing up the data, you can also restore it to MongoDB. To restore the data, you use mongorestore.exe. If you type it in the Command Prompt window and press Enter, you will get a response as shown in Figure 49.

The basic operation to perform is to restore the database from the previously taken backup. This is obtained by running the following command:

Code Listing 115: mongorestore usage

mongorestore -h localhost --db mydb --drop c:\backup

  • -h represents the host where the MongoDB runs.
  • --db represents the database to be restored.
  • --drop contains the information of dropping the collection before recreating it.

Restoring the database.

Figure 49: Restoring the database.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.