Skip to content

Instantly share code, notes, and snippets.

@Mohamed-Code-309
Last active May 13, 2023 11:46
Show Gist options
  • Save Mohamed-Code-309/5ec2a5030c26fa386988c6ec1391cd1a to your computer and use it in GitHub Desktop.
Save Mohamed-Code-309/5ec2a5030c26fa386988c6ec1391cd1a to your computer and use it in GitHub Desktop.

How to Backup and Restore Mongodb Databases 💾

  1. Introduction
  2. Mongodb Database Tools
  3. mongodump Command
  4. mongorestore Command
  5. bsondump Command

In the world of MongoDB, ensuring the safety and availability of your data is crucial. MongoDB provides two powerful command-line tools, mongodump and mongorestore that simplify the process of creating backups and restoring data.

In this article, we will explore how these tools work, their key features, and how they can be leveraged to backup and restore your MongoDB databases.

Whether you are a developer, system administrator, or database manager, understanding the capabilities of mongodump and mongorestore is essential for maintaining data integrity and accuracy .

Table of contents ☝️


The MongoDB Database Tools is a collection of command-line utilities provided by MongoDB that assist in various database operations such as backups, restores, data migrations, performance analysis, and more.

These tools include mongodump, mongorestore, mongoexport, mongoimport, mongostat, bsondump, and others, each serving a specific purpose to help developers, administrators, and database managers efficiently manage and interact with MongoDB databases.

In this article, our focus will be on two key components mongodump and mongorestore.

mongodump is responsible for creating backups, while mongorestore is used to restore data from those backups into MongoDB. Together, these tools offer a comprehensive backup and restore solution for MongoDB databases.

To add mongodump and mongorestore to your system, you need to follow these steps:

  1. Download MongoDB: Visit the official MongoDB website (https://www.mongodb.com/) and download the appropriate MongoDB Community Edition for your operating system.

  2. Download MongoDB Database Tools: From the following link : (https://www.mongodb.com/try/download/database-tools)

  3. According to your system, you can download a zip file or a msi file, the following steps describe how to add the tools in a windows environment :

    • Extract the zip file and after extracting it, copy both mongodump.exe and mongorestore.exe to the location where you your mongodb server is installed (e.g., C:\Program Files\MongoDB\Server{{version}}\bin)

    • Or install the msi file. then setup all the tools. This will create a folder called tools in C:\Program Files\MongoDB that contain all the command-line utilities provided by MongoDB. Then you need to add the directory of those binaries files to your system's PATH environment variable.

Table of contents ☝️


The mongodump is a utility for creating database backups for Mongodb. To start using mongodump, you need to open a command prompt or terminal window and run the mongodump command followed by the desired options and arguments. Here's the basic syntax:

mongodump <options>

Running mongodump command without any specific options or arguments will dump all databases from the MongoDB server.

mongodump 

By default, mongodump creates a folder called dump in the current working directory (the same directory you run the mongodump command), and within that directory, it creates a subdirectory for each database being dumped.

Inside each database directory, it creates:

  1. BSON files (binary representation of MongoDB documents) for each collection in that database. This file contains the actual data. The bsondump utility tool is useful for reading the output BSON files generated by mongodump.

  2. JSON file contains metadata information about the database and its collections. It includes details such as collection names, indexes, options, and other metadata related to the structure and organization of the database.

Table of contents ☝️

Here are some commonly used options for mongodump:

To display the version information for mongodump installed on your system, you can use the --version option:

mongodump --version

mongodump options ☝️

We use the flag -o, --out <directory>.

To dump the entire databases to a folder called backup:

mongodump --out backup

To dump the entire databases to a folder called mongodata in drive D:

mongodump --out D:\mongodata

mongodump options ☝️

We use the flag -d, --db <database>.

mongodump --db blog --out backup

The command creates a backup of a specific database named blog inside a folder called backup:

Unfortunately, Cannot use --db multiple time in the same command, in other words, we can backup only a single database per command.

mongodump options ☝️

We use the flag -c, --collection <collection>.

mongodump --db blog --collection comments --out D:\

The commnad will dump a collection called comments exist in a database called blog. You will find the dumped data in a folder has the same name as the database called blog in drive D, so it is better to specify name of the folder you want to backup your data to.

Unfortunately, Cannot use --collection multiple time in the same command, we can backup only a single collection per command.

mongodump options ☝️

We use the flag --excludeCollection <collection>.

mongodump --db blog --excludeCollection comments

This command will create a backup of the blog database, excluding the comments collection. The resulting backup will include all other collections from the blog database, except for comments.

unlike --collection option, you can specify multiple --excludeCollection options to exclude multiple collections in a single backup command.

mongodump --db blog --excludeCollection comments --excludeCollection posts

This command creatse a backup for all the collections the blog database except for comments and posts collections.

mongodump options ☝️

The --query option in mongodump allows you to specify a query expression to filter the documents that will be included in the backup based on certain criteria. Only the documents matching the query will be backed up.

For example, suppose you have a database named blog with a collection named posts. If you want to back up only the documents from the posts collection that have a field called category equal to "technology", you would run the following command:

mongodump -d blog -c posts --query '{\"category\":\"technology\"}'

To avoid any issues with the shell interpreting the json string, we use \ before any double quotation " and enclose the query brackets {} between a single quotation '

mongodump options ☝️

Alternatively, you can also store the query filter in a separate file and pass it to the mongodump command using the --queryFile option. Here's an example:

  1. Create a file named query.json with the following contents:

    { "category": "technology" }
  2. Use the mongodump command with the --queryFile option:

    mongodump -d blog -c posts --queryFile query.json -o backup

This should avoid any issues with the shell interpreting the quotes in the query filter.

mongodump options ☝️

The --gzip option enables compression of the output files during the backup process. This can significantly reduce the size of the backup files and save storage space.

mongodump --gzip
mongodump --gzip -d blog -o backup

Using the --gzip option is especially beneficial when dealing with large databases, as it helps reduce the backup file size, making it more efficient for storage and transfer.

By default, it creates a .gz file for each BSON and JSON file in the backup.

mongorestore automatically detects and decompresses the gzip-compressed BSON files during the restore process.

mongodump options ☝️

The --archive option allows you to create a single archive file that contains the backup data instead of generating individual files for each collection. You can specify the path and filename for the archive file you want to create. This can be useful for situations where you want to store the backup in a compressed format or transfer it as a single file.

mongodump --archive=backup.archive 

This command will create a single archive file named backup.archive that contains the backup data for the all databases.

You can specify other options suh as : --db, --collection, --excludeCollectio, ...etc but the out option are not allowed to be used with the --archive option.

mongodump --db blog --collection posts --archive=D:\backup\blog.archive 

The command creates a single archive file called blog.archive that contains the backup data for the posts collection in the blog database, saved at the location "D:\backup". Just make sure the backup folder in drive D is existed.

mongodump options ☝️

The --authenticationDatabase option is used to specify the <authentication database> when connecting to a MongoDB server that requires authentication. This option is necessary when you are connecting with a username and password to a MongoDB deployment that uses authentication.

The <authentication database> is the database where you created the user or where the user has the necessary privileges to perform operations. It is often admin database.

mongodump --authenticationDatabase admin --username myuser --password mypassword  --db mydatabase --out /backup

The command authenticate against the admin database, and then perform the backup of the specified mydatabase. The resulting backup files will be stored in the backup directory.

mongodump options ☝️

We use the both --host and --port options to connect to a remote server in order to perform the backup:

mongodump --host <remote_host> --port <remote_port>
  • The --host option specifies the hostname or IP address of the MongoDB server.
  • The --port option specifies the port number on which the MongoDB server is running (default is 27017).
mongodump --host 123.45.67.89 --port 27017 --username myuser --password mypassword --authenticationDatabase admin --db mydatabase --out /path/to/backup

This command will connect to the remote server at IP address 123.45.67.89 on port 27017, authenticate with the provided username and password, backup the mydatabase database, and store the backup files in the /path/to/backup/mydatabase directory on your local machine.

mongodump options ☝️

Table of contents ☝️


The mongorestore is a utility to restore backups created by mongodump. To start using mongorestore, you need to open a command prompt or terminal window and run the mongorestore command followed by the desired options and arguments. Here's the basic syntax:

mongorestore <options>

Running mongorestore command without any specific options or arguments will will search for folder called dump in the current working directory (the terminal directory) to restore data from it.

mongorestore

The command reads the data from the backup file and inserting it back into a MongoDB server. It recreates the database, collections, indexes, and other database structures present in the backup.

Before running this command without any options, make sure you have a folder called dump resulted from executing the mongodump command or other compatible backup tools.

Here are some commonly used options for mongorestore:

To display the version information for mongorestore installed on your system, you can use the --version option:

mongorestore --version

mongorestore options ☝️

To start restoring data from a specific folder, just enter the path to the backup data or folder name in the current directory without any option after the mongostore command.

mongorestore D:\backup

This command restore databases from a folder named backup located in drive D.

mongorestore options ☝️

We use the --nsInclude option to specify the namespace filter. The namespace represents a combination of a database name and a collection name and follows the format <database_name>.<collection_name_pattern>.

mongorestore backup --nsInclude blog.posts

This command restores only a collection called posts from a database called blog from a folder named backup exist in the current directory. However, it will not restore any collections from the same database.

We can specify multiple --nsInclude options to restore multiple collection from the same database.

mongorestore --nsInclude blog.posts --nsInclude blog.comments

The command will restore posts and comments collections from the blog database and ignore restoring other collections from the same databas.

mongorestore options ☝️

In the --nsInclude option, We can use the * symbol after the <database_name> to include all collections exist in the database.

mongorestore --nsInclude blog.*

The command will restore all collections exist in the blog database from the backup. However, it will not restore any collections from other databases.

We can specify multiple --nsInclude options to restore multiple databases with different collections.

mongorestore --nsInclude blog.* --nsInclude test.customers --nsInclude test.orders

This command restores :

  1. blog.*: includes all collections within the blog database.
  2. test.customers: includes only the customers collection within the test database.
  3. test.orders: includes only the orders collection within the test database.

mongorestore options ☝️

The --drop option is used to drop (delete) the target database before restoring the backup.

By default, mongorestore adds or updates data from the backup without removing any existing data. However, when you include the --drop option, it first drops the target database and then performs the restore operation. This ensures a clean slate before restoring the backup.

mongorestore --drop --nsInclude blog.posts

This command will drop the posts collection from the blog database then restore it from the default dump folder that exist in the current directory.

This command will not drop any collections if they are not there in the backup.

mongorestore options ☝️

To restore a backup created with the --archive option using the mongodump command , you can use the mongorestore command with the --archive option as well:

mongorestore --archive=backup.archive

mongorestore options ☝️

Table of contents ☝️


The bsondump command is used to convert BSON files to a human-readable format JSON format.

It takes the following options:

  • --bsonFile: specify the input BSON file to be processed.
  • --outFile : specify the output file where the converted data will be saved.
bsondump --bsonFile posts.bson --outFile posts.json

The command takes the input BSON file posts.bson and converts it to a human-readable JSON format, saving the converted data in the output file posts.json.

We can ignore the option --outFile, allowing you to view the contents of the BSON document inside the command prompt or terminal window.

Table of contents ☝️


Related Content:

✔️ User Authentication and Authorization in MongoDB (coming soon)


Links:

🕴️ Linkedin: Dragon Slayer 🐲
📝 Articles: All Articles written by D.S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment