Lesson 37 of 50 – MongoDB Database Backup
74%

MongoDB Database Backup

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.

1. What Is a Database Backup?

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:

  • Student details
  • Course information
  • Fee payment records
  • Attendance records
  • Examination results

If these records are lost, a valid backup can help restore the database to a previously captured state.

2. Common MongoDB Backup Methods

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.

3. Check MongoDB Database Tools

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.

4. Back Up All Databases

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.

5. Back Up a Specific Database

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.

6. Back Up a Specific Collection

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.

7. Back Up a MongoDB Database with Authentication

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.

8. Create a Compressed Backup

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.

9. Important Backup Considerations

  • Store backups separately from the production database.
  • Restrict access to backup files because they may contain sensitive information.
  • Use a schedule suitable for how frequently your data changes.
  • Keep more than one backup when appropriate.
  • Monitor backup jobs and check that they complete successfully.
  • Test restoration regularly in a safe environment.
  • For replica sets and sharded clusters, select a backup method that provides the required consistency.
  • Back up and protect encryption keys and other required recovery material separately.

A backup is only useful if it can be restored successfully. A successful command alone does not prove that a complete recovery is possible.

10. Practical Assignment

  1. Check the installed version of mongodump.
  2. Create a backup of all accessible databases.
  3. Create a separate backup of the school database.
  4. Back up only the students collection.
  5. Create a compressed archive of the school database.
  6. Inspect the output files and folders.
  7. Store a copy of the backup in a separate, secure location.
  8. Explain the difference between a database backup and a collection backup.
  9. Explain why restoration testing is important.
  10. Document the date, location, and result of your backup operation.

🧠 Quick Quiz

1. Which tool creates a MongoDB logical backup?




2. Which option specifies the output directory?




3. Why should backups be tested?