A database backup is a copy of database data that can be used
to recover information if the original data is lost, damaged,
or accidentally deleted. MongoDB provides tools such as
mongodump for creating backups.
A backup is a separate copy of your database. It helps protect important information against accidental deletion, hardware failures, and other incidents.
For example, a school database might contain:
If these records are lost, a valid backup can help restore the database to a previously captured state.
| Method | Purpose |
|---|---|
mongodump |
Creates a logical backup of MongoDB data. |
mongorestore |
Restores data from a compatible dump. |
| MongoDB Atlas backups | Managed backup features for supported Atlas plans and configurations. |
| Filesystem or volume snapshots | Copies database storage using a suitable consistency procedure. |
In this lesson, we will focus on
mongodump, which is commonly used for
logical backups.
MongoDB Database Tools provide commands such as
mongodump and mongorestore.
These tools may need to be installed separately from
the MongoDB server.
Check whether mongodump is available:
mongodump --version
If the command is not recognized, install MongoDB Database Tools for your operating system and ensure their executable directory is available in your PATH.
You can download the tools from the official MongoDB Database Tools documentation and download pages.
Run the following command in your system terminal to create a logical dump of all databases accessible to the connected account.
mongodump --uri="mongodb://127.0.0.1:27017" --out="./mongodb_backup"
The --uri option specifies the MongoDB
server, while --out specifies the
directory where the dump files are written.
Example output structure:
mongodb_backup/
admin/
config/
school/
students.bson
students.metadata.json
courses.bson
courses.metadata.json
The exact files and databases depend on your MongoDB deployment and the data being backed up.
If you only need to back up the
school database, use the
--db option.
mongodump \
--uri="mongodb://127.0.0.1:27017" \
--db=school \
--out="./school_backup"
This creates a dump for the specified database instead of requesting a dump of all databases.
Make sure the connected user has the required permissions to read the data being backed up.
You can also back up one collection, such as
students, from the
school database.
mongodump \
--uri="mongodb://127.0.0.1:27017" \
--db=school \
--collection=students \
--out="./students_backup"
This is useful when you need a logical dump of a particular collection rather than an entire database.
Remember that a single-collection dump is not a complete backup of every collection in the database.
When authentication is enabled, provide credentials and the correct authentication database.
mongodump \
--uri="mongodb://127.0.0.1:27017/school?authSource=admin" \
--username=backupUser \
--out="./school_backup"
The command prompts for the password. Here,
backupUser is assumed to exist and have
the required permissions. The example assumes the
user is defined in the admin database.
If the user is defined in a different database,
change authSource accordingly.
Avoid putting real passwords directly into shell commands, scripts, or shared files. Use a secure credential-handling method appropriate to your environment.
MongoDB Database Tools can write a compressed archive
using the --archive and
--gzip options.
mongodump \
--uri="mongodb://127.0.0.1:27017" \
--db=school \
--archive="./school_backup.gz" \
--gzip
This creates a single compressed archive containing the logical dump of the specified database.
Compressed archives can be easier to store or transfer than a directory containing separate dump files.
A backup is only useful if it can be restored successfully. A successful command alone does not prove that a complete recovery is possible.
mongodump.school database.students collection.1. Which tool creates a MongoDB logical backup?
2. Which option specifies the output directory?
3. Why should backups be tested?