MongoDB Dump & Restore
MongoDB Database Tools Installation and Database Backup (mongodump) Guide
Overview
MongoDB Database Tools are a set of command-line utilities used to perform database operations such as backup, restore, import, and export. The mongodump utility is used to create a backup of a MongoDB database by exporting the database into BSON files, which can later be restored using the mongorestore utility.
Prerequisites
Before creating a backup, ensure the following:
- MongoDB Server is installed and running.
- MongoDB Database Tools are installed.
- You have access to the MongoDB server.
- You know the database name (if backing up a specific database).
- You have permission to save files in the backup location.
Step 1: Download MongoDB Database Tools
-
Open your web browser.
-
Navigate to the MongoDB Database Tools download page:
-
Select the following options:
- Platform: Windows
- Package: ZIP
- Version: Latest Stable Version
-
Click Download.
Step 2: Extract the Downloaded File
After the download is complete:
- Locate the downloaded ZIP file.
- Right-click the file and select Extract All.
- Open the extracted folder.
Example:
mongodb-database-tools-windows-x86_64-100.13.0Inside the extracted folder, open the bin folder.
mongodb-database-tools-windows-x86_64-100.13.0
│
└── bin
├── mongodump.exe
├── mongorestore.exe
├── mongoexport.exe
├── mongoimport.exe
├── mongostat.exe
├── mongotop.exe
└── bsondump.exeThe bin folder contains all the MongoDB Database Tools.
Step 3: Add the Database Tools to the System PATH
Adding the bin folder to the Windows PATH allows you to run MongoDB commands from any Command Prompt or PowerShell window.
Copy the bin folder path
Example:
C:\Program Files\MongoDB\DatabaseTools\binAdd the path to Environment Variables
- Open the Start Menu.
- Search for Environment Variables.
- Click Edit the system environment variables.
- Click Environment Variables.
- Under System Variables, select Path.
- Click Edit.
- Click New.
- Paste the copied bin folder path.
- Click OK to save the changes.
- Close and reopen Command Prompt or PowerShell.
Step 4: Verify the Installation
Open Command Prompt or PowerShell.
Run the following command:
mongodump --versionIf the installation is successful, you will see output similar to:
mongodump version: 100.xx.x
git version: ...
Go version: ...This confirms that the MongoDB Database Tools have been installed successfully.
Step 5: Verify That MongoDB Is Running
Open Command Prompt and start the MongoDB shell.
mongoshList all available databases:
show dbsExample:
admin
config
local
bacsys
productionExit the MongoDB shell:
exitStep 6: Navigate to the Location Where You Want to Store the Backup
Open Command Prompt or PowerShell.
Navigate to the folder where you want the backup to be created.
Example:
cd D:\or
cd C:\BackupYou can also create a new folder if required.
Example:
mkdir mongodb-backupStep 7: Create a Backup of All Databases
Run the following command:
mongodump --host localhost --port 27017 --out ./mongodb-backupCommand Explanation
| Option | Description |
|---|---|
mongodump | Starts the MongoDB backup process. |
--host localhost | Connects to the MongoDB server running on the local machine. |
--port 27017 | Uses the default MongoDB port. |
--out ./mongodb-backup | Saves the backup inside a folder named mongodb-backup in the current directory. |
After the command completes successfully, a backup folder will be created.
Example:
mongodb-backup
├── admin
├── config
├── local
└── bacsysStep 8: Create a Backup of a Specific Database
If you only want to back up one database, use the following command:
mongodump --host localhost --port 27017 --db bacsys --out ./mongodb-backupReplace bacsys with your database name.
Example output:
mongodb-backup
└── bacsys
├── users.bson
├── users.metadata.json
├── devices.bson
└── devices.metadata.jsonStep 9: Create a Backup Using Authentication
If authentication is enabled on your MongoDB server, use the following command:
mongodump --host localhost --port 27017 --username admin --password yourPassword --authenticationDatabase admin --out ./mongodb-backupReplace:
- admin with your MongoDB username.
- yourPassword with your MongoDB password.
- authenticationDatabase with the database used for authentication.
Step 10: Verify the Backup
Open the backup folder.
Example:
mongodb-backup
└── bacsysYou should see files similar to:
users.bson
users.metadata.json
devices.bson
devices.metadata.jsonFile Description
- .bson files contain the database data.
- .metadata.json files contain collection metadata such as indexes.
If these files are present, the backup was created successfully.
Common mongodump Commands
Backup all databases
mongodump --host localhost --port 27017 --out ./mongodb-backupBackup a specific database
mongodump --host localhost --port 27017 --db bacsys --out ./mongodb-backupBackup using authentication
mongodump --host localhost --port 27017 --username admin --password yourPassword --authenticationDatabase admin --out ./mongodb-backupBackup from a remote MongoDB server
mongodump --host 192.168.1.100 --port 27017 --out ./mongodb-backupBackup Folder Structure
mongodb-backup
├── admin
├── config
├── local
├── bacsys
│ ├── users.bson
│ ├── users.metadata.json
│ ├── devices.bson
│ └── devices.metadata.json
└── productionRestore Backup (mongorestore)
Use mongorestore to restore a backup created with mongodump.
Restore a specific database
mongorestore --host localhost --port 27017 --db bacsys ./mongodb-backup/bacsysRestore all databases from a backup folder
mongorestore --host localhost --port 27017 ./mongodb-backupRestore using authentication
mongorestore --host localhost --port 27017 --username admin --password yourPassword --authenticationDatabase admin --db bacsys ./mongodb-backup/bacsysRestore from a remote MongoDB server
mongorestore --host 192.168.1.100 --port 27017 --db bacsys ./mongodb-backup/bacsysNotes
- Always ensure the MongoDB server is running before taking or restoring a backup.
- Store backups in a secure location.
- Verify the backup after it completes.
- Compress the backup folder into a ZIP file if it needs to be transferred or archived.
- Use
mongorestoreto restore the backup when required.