Backing Up MongoDB using mongodump
Backing Up MongoDB using mongodump
Backing up your MongoDB data is critical to prevent data loss and ensure you can restore it in case of corruption, deletion, or system failure. MongoDB provides a built-in tool called mongodump for this purpose.
What is mongodump?
mongodump is a command-line tool that creates a binary export (BSON files) of your MongoDB databases. This backup can later be restored using mongorestore.
Basic Syntax
    mongodump --uri="mongodb://localhost:27017" --out=/path/to/backupThis command connects to your MongoDB instance running on localhost and dumps all databases to the specified output directory.
Example 1: Backup All Databases
    mongodump --out=./mongo-backupThis creates a folder named mongo-backup in your current directory containing backups of all databases.
    2025-05-01T09:00:00.123+0530    writing admin.system.version to 
    2025-05-01T09:00:00.456+0530    done dumping admin.system.version (1 document)
    2025-05-01T09:00:00.789+0530    writing test.users to 
    2025-05-01T09:00:01.001+0530    done dumping test.users (3 documents)
    Example 2: Backup a Specific Database
    mongodump --db=test --out=./backup-testdbThis command only backs up the test database into the backup-testdb directory.
Question: Why might you back up only one database instead of all?
Answer: In large-scale applications, backing up just the relevant database (e.g., user data, logs) saves time, space, and processing power.
Example 3: Backup a Specific Collection
    mongodump --db=test --collection=users --out=./backup-usersThis command backs up only the users collection from the test database.
    2025-05-01T09:10:22.001+0530    writing test.users to 
    2025-05-01T09:10:22.456+0530    done dumping test.users (3 documents)
    Example 4: Backup with Authentication
    mongodump --uri="mongodb://username:password@localhost:27017/test" --out=./auth-backupIf your MongoDB instance requires authentication, provide the URI with the correct username and password.
Understanding the Output Files
After running mongodump, you’ll see the following structure:
    ./mongo-backup/
    ├── admin
    │   └── system.version.bson
    ├── test
    │   └── users.bson
    └── metadata.json
    Each .bson file represents a dumped collection in binary format. These files are not human-readable but can be restored using mongorestore.
Where is mongodump Installed?
mongodump is part of the MongoDB Database Tools. If you installed MongoDB using official installers, it is likely already available in your terminal. If not, install it from: MongoDB Tools Download.
Common Use Cases
- Nightly or weekly automated backups
- Taking a snapshot before schema changes
- Exporting a database for staging or development
Summary
mongodump is a simple yet powerful utility to export MongoDB databases, collections, or entire clusters to BSON files. It is the first step in any reliable MongoDB backup and disaster recovery plan.
In the next section, we’ll learn how to restore these backups using mongorestore.
Next Topic ⮕Restoring Data using mongorestore












Comments
Loading comments...