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

  1. Open your web browser.

  2. Navigate to the MongoDB Database Tools download page:

    https://www.mongodb.com/try/download/database-tools

  3. Select the following options:

    • Platform: Windows
    • Package: ZIP
    • Version: Latest Stable Version
  4. Click Download.


Step 2: Extract the Downloaded File

After the download is complete:

  1. Locate the downloaded ZIP file.
  2. Right-click the file and select Extract All.
  3. Open the extracted folder.

Example:

mongodb-database-tools-windows-x86_64-100.13.0

Inside 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.exe

The 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\bin

Add the path to Environment Variables

  1. Open the Start Menu.
  2. Search for Environment Variables.
  3. Click Edit the system environment variables.
  4. Click Environment Variables.
  5. Under System Variables, select Path.
  6. Click Edit.
  7. Click New.
  8. Paste the copied bin folder path.
  9. Click OK to save the changes.
  10. Close and reopen Command Prompt or PowerShell.

Step 4: Verify the Installation

Open Command Prompt or PowerShell.

Run the following command:

mongodump --version

If 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.

mongosh

List all available databases:

show dbs

Example:

admin
config
local
bacsys
production

Exit the MongoDB shell:

exit

Step 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:\Backup

You can also create a new folder if required.

Example:

mkdir mongodb-backup

Step 7: Create a Backup of All Databases

Run the following command:

mongodump --host localhost --port 27017 --out ./mongodb-backup

Command Explanation

OptionDescription
mongodumpStarts the MongoDB backup process.
--host localhostConnects to the MongoDB server running on the local machine.
--port 27017Uses the default MongoDB port.
--out ./mongodb-backupSaves 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
└── bacsys

Step 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-backup

Replace bacsys with your database name.

Example output:

mongodb-backup
└── bacsys
    ├── users.bson
    ├── users.metadata.json
    ├── devices.bson
    └── devices.metadata.json

Step 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-backup

Replace:

  • 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
└── bacsys

You should see files similar to:

users.bson
users.metadata.json
devices.bson
devices.metadata.json

File 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-backup

Backup a specific database

mongodump --host localhost --port 27017 --db bacsys --out ./mongodb-backup

Backup using authentication

mongodump --host localhost --port 27017 --username admin --password yourPassword --authenticationDatabase admin --out ./mongodb-backup

Backup from a remote MongoDB server

mongodump --host 192.168.1.100 --port 27017 --out ./mongodb-backup

Backup Folder Structure

mongodb-backup
├── admin
├── config
├── local
├── bacsys
│   ├── users.bson
│   ├── users.metadata.json
│   ├── devices.bson
│   └── devices.metadata.json
└── production

Restore Backup (mongorestore)

Use mongorestore to restore a backup created with mongodump.

Restore a specific database

mongorestore --host localhost --port 27017 --db bacsys ./mongodb-backup/bacsys

Restore all databases from a backup folder

mongorestore --host localhost --port 27017 ./mongodb-backup

Restore using authentication

mongorestore --host localhost --port 27017 --username admin --password yourPassword --authenticationDatabase admin --db bacsys ./mongodb-backup/bacsys

Restore from a remote MongoDB server

mongorestore --host 192.168.1.100 --port 27017 --db bacsys ./mongodb-backup/bacsys

Notes

  • 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 mongorestore to restore the backup when required.