MongoDB Backup and Restore

1. Install MongoDB CLI Database Tools

Download MongoDB Tools:

Visit the official MongoDB Download Center to download the MongoDB Command Line Database Tools package suitable for your operating system. After installation, verify the tools are installed by navigating to the tools directory:

C:\Program Files\MongoDB\Tools\100\bin

The directory should contain executable files such as mongodump and mongorestore.

2. Create a Folder to Store Backups

Navigate to your product directory (e.g., C:\Elixir\Ambience-2024.0.4). Create a new folder named ‘db_backup’ to store MongoDB backup files.

3. Configure the Command Prompt Environment

Launch a Command Prompt (CMD) window. Navigate to the backup folder you just created by entering this command:

cd C:\Elixir\Ambience-2024.0.4\db_backup

Set the path to MongoDB tools: The MongoDB tools (such as mongoexport and mongoimport) are not always included in your system’s default PATH, so you must set the path to the directory containing the MongoDB tools manually.

set path="C:\Program Files\MongoDB\Tools\100\bin"

Purpose of Setting Path:

  • Functionality: Ensures the command prompt can locate MongoDB tools like mongodump and mongorestore without needing to specify their full paths.

  • Temporary Nature: This path setting is only valid for the current command prompt session. Repeat this step if a new CMD window is opened.


4. Perform MongoDB Backup

i) Backup the Whole Database:
To create a backup of all databases on the MongoDB server, enter the following command:

mongodump --uri="mongodb://example-host:example-port" 

Replace example-host:example-port with your MongoDB server details.

ii) Backup a Specific Database:
To create a backup of an entire database, execute the following command:

mongodump --uri="mongodb://example-host:example-port" --db=exampleDB

Replace example-host:example-port with your MongoDB server details, and exampleDB with the name of the database you want to back up.

iii) Backup a Specific Collection:
For backing up a specific collection within a database, enter the following command:

mongodump --uri="mongodb://example-host:example-port" --db=exampleDB --collection=exampleCollection

Replace example-host:example-port with your MongoDB server details, exampleDB with the database name and exampleCollection with the collection name.

Note: By default, backup files will be stored in a folder named dump inside the current directory. Please ensure that the dump folder is created in the backup directory specified earlier before proceeding.


5. Perform MongoDB Restore

i) Restore the Whole Database:
To restore all databases from the backup, execute the following command:

mongorestore --uri="mongodb://example-host:example-port" 

Replace example-host:example-port with your MongoDB server details.

ii) Restore a Specific Database:
To restore a previously backed-up database, provide the path to the database as shown:

mongorestore --uri="mongodb://example-host:example-port" --db=exampleDB /path/to/db_backup/exampleDB

Replace example-host:example-port with your MongoDB server details, and exampleDB with the name of the database you want to restore.

iii) Restore a Specific Collection:
For restoring a specific collection, provide the path to the .bson file of the collection as shown:

mongorestore --uri="mongodb://example-host:example-port" --db=exampleDB --collection=exampleCollection /path/to/db_backup/exampleDB/exampleCollection.bson

Replace example-host:example-port with your MongoDB server details, exampleDB with the database name and exampleCollection with the collection name you with to restore.


6. Verify Successful Backup and Restore

Utilize the mongo shell or a MongoDB client to verify the restored database and collections.