How to Backup and Restore MongoDB Database

How to Backup and Restore MongoDB Database

How to Backup and Restore MongoDB Database

Backing up and restoring MongoDB databases is essential for ensuring data safety and continuity. MongoDB provides several tools to back up and restore data, with the most common tools being mongodump for backup and mongorestore for restore.

1. MongoDB Backup with mongodump

A. Overview of mongodump

mongodump is a command-line utility that creates a backup of your MongoDB database by dumping the contents into BSON files, which can later be restored.

B. Basic mongodump Command

  1. Open your terminal (or shell) and run the following command to back up a specific database:

    mongodump --db <database_name> --out <backup_directory>

    Example:
    Backup blogDB to a folder called backup:

    mongodump --db blogDB --out /path/to/backup/

    Expected Output:

    connecting to: mongodb://127.0.0.1:27017/ 2025-02-02T00:00:00.000+0000 writing blogDB.posts to /path/to/backup/blogDB/posts.bson 2025-02-02T00:00:00.000+0000 done dumping blogDB

    The backup will be stored in a BSON format, which can be restored later.

C. Backup a Specific Collection

If you only want to back up a specific collection, use:

mongodump --db <database_name> --collection <collection_name> --out <backup_directory>

Example:
Backup posts collection from blogDB:

mongodump --db blogDB --collection posts --out /path/to/backup/

D. Backup All Databases

To back up all databases, simply omit the --db option:

mongodump --out /path/to/backup/

This will create a backup of all databases on the MongoDB server.

2. MongoDB Restore with mongorestore

A. Overview of mongorestore

mongorestore is a utility to restore data from a BSON backup created by mongodump. You can restore a single database, collection, or all databases from a backup directory.

B. Basic mongorestore Command

To restore a database, use the following command:

mongorestore --db <database_name> <backup_directory>/<database_name>

Example:
Restore blogDB from the backup directory:

mongorestore --db blogDB /path/to/backup/blogDB/

Expected Output:

connecting to: mongodb://127.0.0.1:27017/ 2025-02-02T00:00:00.000+0000 restoring blogDB.posts from /path/to/backup/blogDB/posts.bson 2025-02-02T00:00:00.000+0000 done restoring blogDB

C. Restore a Specific Collection

To restore only a specific collection from a backup:

mongorestore --db <database_name> --collection <collection_name> <backup_directory>/<database_name>/<collection_name>.bson

Example:
Restore posts collection from blogDB:

mongorestore --db blogDB --collection posts /path/to/backup/blogDB/posts.bson

D. Restore All Databases

To restore all databases from a backup, use:

mongorestore /path/to/backup/

This restores everything in the backup directory.

E. Handling Existing Data During Restore

  • Overwrite Existing Data:
    If you want to overwrite an existing database during restoration, use the --drop flag:

    mongorestore --drop --db blogDB /path/to/backup/blogDB/
  • Restore from a Remote MongoDB Instance:
    To restore a backup to a remote MongoDB server, specify the --host option:

    mongorestore --host <host>:<port> --db blogDB /path/to/backup/blogDB/

3. MongoDB Backup and Restore using MongoDB Atlas (Cloud)

If you are using MongoDB Atlas, the cloud version of MongoDB, you can back up and restore data through the Atlas UI or CLI.

  • Atlas UI:

    • Go to the Backups tab in the MongoDB Atlas dashboard.
    • Create backups using the Scheduled Backup option.
    • Restore a backup using the Restore button to any point in time.
  • Atlas CLI:
    The MongoDB Atlas CLI allows you to automate backup and restore tasks via the command line. You can use commands like atlas backups create and atlas backups restore for managing backups.

4. Summary

Used mongodump to create database backups.
Restored data with mongorestore.
Optionally overwrote existing data using the --drop flag.
Used MongoDB Atlas for cloud-based backup and restore (optional).

5. Next Steps

🔹 Set up automated backups for continuous protection of your MongoDB data.
🔹 Monitor backup sizes and times to ensure performance.
🔹 Test your restore process regularly to verify backup integrity.

Would you like help setting up automated backups or more advanced restore options? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close