When working with MongoDB in distributed environments, you often need to perform database operations from machines where installing the full MongoDB package isn't desirable. A common case is backing up production databases from application servers or executing migrations between environments.
MongoDB provides standalone packages of just the database tools (including mongodump and mongorestore) without requiring a full MongoDB server installation. Here's how to implement this:
# For Ubuntu/Debian systems
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.5.1.deb
sudo apt install ./mongodb-database-tools-ubuntu2004-x86_64-100.5.1.deb
# For RHEL/CentOS systems
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.5.1.rpm
sudo yum install mongodb-database-tools-rhel70-x86_64-100.5.1.rpm
With the tools installed, you can now perform operations against your MongoDB server (A) from your HTTP server (B):
# Basic remote dump
mongodump --host=mongodb-serverA.example.com --port=27017 --db=production_db --out=/backups/daily
# With authentication
mongodump --host=mongodb-serverA.example.com --username=admin --password=secure123 --authenticationDatabase=admin --db=production_db
# Compressed dump for large databases
mongodump --host=mongodb-serverA.example.com --gzip --archive=/backups/prod_dump_$(date +%Y%m%d).gz
The same approach works for mongorestore operations:
# Restore from local dump directory
mongorestore --host=mongodb-serverA.example.com --db=new_prod_db /backups/daily/production_db
# Restore from compressed archive
mongorestore --host=mongodb-serverA.example.com --gzip --archive=/backups/prod_dump_20230101.gz
If package installation isn't an option, Docker provides a clean solution:
# One-time mongodump using Docker
docker run --rm mongo:4.4 mongodump --host=mongodb-serverA.example.com --out=/data/dump
# Persistent volume for storing dumps
docker run --rm -v /host/backups:/data/dump mongo:4.4 \
mongodump --host=mongodb-serverA.example.com --out=/data/dump
When working remotely with MongoDB:
- Always use TLS/SSL connections (--ssl option)
- Encrypt sensitive backup files
- Use MongoDB roles with minimal required privileges
- Consider network-level restrictions (firewalls, VPCs)
Here's a sample cron job for automated daily backups:
0 2 * * * /usr/bin/mongodump --host=mongodb-prod.example.com --oplog --gzip --archive=/backups/mongodump_$(date +\%Y\%m\%d).gz
When you need to perform MongoDB backup/restore operations from a machine that doesn't have (and shouldn't have) a full MongoDB installation, you're facing a common infrastructure dilemma. The standard approach of installing MongoDB just to get access to mongodump
and mongorestore
introduces unnecessary bloat and potential security concerns on your HTTP server.
Here are three professional approaches to solve this:
1. Minimal MongoDB Tools Package
# For Debian/Ubuntu:
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian11-x86_64-100.6.1.deb
sudo apt install ./mongodb-database-tools-*.deb
# For RHEL/CentOS:
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.6.1.rpm
sudo yum install mongodb-database-tools-*.rpm
2. Docker Container Approach
# One-time execution:
docker run --rm mongo mongodump --uri="mongodb://user:pass@serverA:27017" --out=/data/dump
# Persistent volume for dumps:
docker run --rm -v $(pwd):/backup mongo \
mongodump --host serverA --port 27017 -u admin -p password --authenticationDatabase admin --out=/backup/dump
3. SSH Tunnel Method
# First establish tunnel:
ssh -N -L 27018:localhost:27017 user@serverA &
# Then run tools locally pointing to tunnel:
mongodump --port 27018 --out ./mongo_backup
# Don't forget to kill the tunnel after:
kill %1
When connecting remotely, ensure proper authentication is configured. For MongoDB 4.0+:
# Create backup user (run on serverA):
use admin
db.createUser({
user: "backupagent",
pwd: "securepassword",
roles: ["backup"]
})
Here's a complete backup script using the Docker method:
#!/bin/bash
DATE=$(date +%Y-%m-%d)
BACKUP_DIR="/backups/mongo/$DATE"
docker run --rm -v $BACKUP_DIR:/backup mongo \
mongodump --host mongo.prod.example.com \
--username backupagent \
--password "$MONGO_BACKUP_PWD" \
--authenticationDatabase admin \
--gzip \
--out /backup
# Optional compression
tar -czf "$BACKUP_DIR.tar.gz" "$BACKUP_DIR"
- Always use TLS/SSL for remote connections
- Consider IP whitelisting for the backup server
- Use temporary credentials with limited privileges
- Encrypt backup files at rest
For large databases:
mongodump \
--numParallelCollections=4 \
--gzip \
--archive=backup.archive