MongoDB - Backup and Recovery
Definition
Backup and Recovery in MongoDB refers to the process of creating copies of database data (backup) and restoring the data (recovery) when data loss, corruption, hardware failure, or accidental deletion occurs. It helps ensure data availability and business continuity.
Steps to Take a Backup in MongoDB
1). Open the Command Prompt or Terminal.
2). Ensure the MongoDB server is running.
3). Navigate to the MongoDB bin directory (if required).
4). Use the mongodump utility to create a backup.
5). Specify the database name to back up.
6). Choose the backup destination folder.
7). Execute the backup command.
8). Verify that the backup files are created successfully.
Backup Example
mongodump --db studentdb --out C:\mongodb_backup
Explanation:
This command creates a backup of the studentdb database and stores it in the C:\mongodb_backup folder.
Steps to Restore a Backup in MongoDB
1). Open the Command Prompt or Terminal.
2). Ensure the MongoDB server is running.
3). Locate the backup folder created by mongodump.
4). Use the mongorestore utility.
5). Specify the database name and backup location.
6). Execute the restore command.
7). MongoDB restores the backed-up data.
8). Verify the restored documents using find().
Recovery Example
mongorestore --db studentdb C:\mongodb_backup\studentdb
Explanation:
This command restores the backup of the studentdb database from the specified backup folder.