MongoDB mongodump
Introduction
Database backups are a critical part of any application's operational strategy. MongoDB provides a powerful utility called mongodump
that allows you to create binary exports (backups) of your MongoDB databases, which can later be restored using the mongorestore
tool.
In this guide, we'll explore how to use mongodump
effectively for various backup scenarios, understand its options, and learn best practices for implementing a solid backup strategy for your MongoDB deployments.
What is mongodump?
mongodump
is a command-line utility that creates a binary export of the contents of a MongoDB database. It's part of the MongoDB Database Tools and provides a way to create backups of your collections, which can be used for:
- Creating full or partial database backups
- Migrating data between MongoDB instances
- Setting up development/test environments with production-like data
- Creating archive points for disaster recovery
Basic Usage
The simplest way to use mongodump
is to run it without any parameters, which will connect to the local MongoDB instance and back up all databases:
mongodump
By default, this command:
- Connects to
localhost
on port27017
- Dumps all databases (except local)
- Outputs the data to the
./dump
directory
The output will look similar to:
2023-10-15T12:34:56.789+0000 writing admin.system.version to dump/admin/system.version.bson
2023-10-15T12:34:56.791+0000 done dumping admin.system.version (1 document)
2023-10-15T12:34:56.791+0000 writing myDatabase.users to dump/myDatabase/users.bson
2023-10-15T12:34:56.834+0000 done dumping myDatabase.users (242 documents)
...
Common Options
Let's explore some of the most commonly used options with mongodump
:
Specifying a Database and Collection
To back up a specific database:
mongodump --db myDatabase
To back up a specific collection within a database:
mongodump --db myDatabase --collection users
Setting Output Directory
Change the default output directory:
mongodump --out /path/to/backup/directory
Connection Options
When connecting to a remote MongoDB server or one with authentication:
mongodump --host mongodb.example.com --port 27017 --username myUser --password myPassword --authenticationDatabase admin
For MongoDB Atlas or other services using connection strings:
mongodump --uri "mongodb+srv://username:[email protected]/myDatabase"
Creating Compressed Backups
To save disk space, you can create gzipped output files:
mongodump --archive=backup.gz --gzip
Advanced Usage Scenarios
Partial Backups with Queries
You can use queries to back up only specific documents:
mongodump --db myDatabase --collection users --query '{"active": true}'
This backs up only users where the active
field equals true
.
Point-in-Time Backups
For consistent backups of replica sets, you can use the --oplog
option:
mongodump --oplog
This captures all operations during the dump, allowing for point-in-time recovery.
Excluding Collections
Exclude specific collections from the backup:
mongodump --db myDatabase --excludeCollection=logs --excludeCollection=temp
Real-World Examples
Example 1: Daily Backup Script
Here's a simple bash script you might use for daily backups:
#!/bin/bash
# daily-backup.sh
# Variables
BACKUP_DIR="/var/backups/mongodb/$(date +%Y%m%d)"
LOG_FILE="/var/log/mongodb/backup-$(date +%Y%m%d).log"
# Create backup directory
mkdir -p $BACKUP_DIR
# Run mongodump
mongodump --out $BACKUP_DIR --gzip > $LOG_FILE 2>&1
# Delete backups older than 14 days
find /var/backups/mongodb -type d -mtime +14 -exec rm -rf {} \; 2>/dev/null
Example 2: Migrating a Production Database to Development
Sanitizing sensitive data while migrating to a dev environment:
# Step 1: Dump only the essential collections
mongodump --uri "mongodb://prod-server:27017/myApp" \
--collection users --collection products --collection orders
# Step 2: Restore to development with the mongorestore tool (not covered in this guide)
# Step 3: Sanitize sensitive data directly in development (example MongoDB command)
# db.users.updateMany({}, {$set: {email: "[email protected]", phone: "555-0000"}})
Example 3: Selective Backup Based on Date Range
Backing up transactions from the last month:
mongodump --db finance --collection transactions \
--query '{"date": {"$gte": {"$date": "2023-09-01T00:00:00Z"}, "$lt": {"$date": "2023-10-01T00:00:00Z"}}}' \
--out monthly-backup
Common Issues and Solutions
Error: "Failed to connect to..."
If you encounter connection issues:
- Check that MongoDB is running
- Verify network connectivity
- Ensure authentication credentials are correct
- Check firewall settings
Error: "not authorized on admin to execute command..."
This indicates a permissions issue:
- Ensure your user has the correct roles (at least
backup
role) - Specify the correct
--authenticationDatabase
Large Databases
For very large databases:
- Consider using
--collection
to break down backups - Schedule backups during low-traffic periods
- Use compression with
--gzip
- Consider using
mongodump
with shards individually if applicable
mongodump vs. Other Backup Methods
Let's compare different MongoDB backup approaches:
Best Practices
-
Schedule Regular Backups: Automate your backup process using cron jobs or task schedulers.
-
Test Your Backups: Regularly verify that your backups can be restored properly.
-
Use Compression: Especially for large databases, use the
--gzip
option to save space. -
Set Retention Policies: Automatically remove older backups to manage storage.
-
Secure Your Backups: Store backups in secure locations and consider encryption.
-
Document Your Backup Strategy: Keep clear documentation on how to restore in case of emergency.
-
Monitor Backup Jobs: Set up alerts for failed backup jobs.
Summary
mongodump
is a powerful and flexible utility for backing up MongoDB databases. It allows you to create full or partial backups, with options for compression, filtering, and point-in-time recovery. When combined with proper scheduling and retention policies, it forms the foundation of a robust MongoDB backup strategy.
By understanding how to use mongodump
effectively, you can protect your data against loss and facilitate data migration tasks with confidence.
Additional Resources
- MongoDB Database Tools Documentation: MongoDB Documentation
- Learn more about
mongorestore
for restoring your backups - For automated backups of MongoDB Atlas clusters, explore Atlas Backup solutions
Exercises
-
Create a backup of a single collection and restore it with a different name.
-
Write a script that does a weekly full backup and daily incremental backups using
--oplog
. -
Set up a backup strategy for a database with one large collection (>10GB) and several small ones.
-
Create a backup of documents that match a specific query, then restore only those documents to a test database.
-
Practice creating compressed archives of your backups and extracting specific collections from them.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)