阿里云主机折上折
  • 微信号
Current Site:Index > mongodump and mongorestore

mongodump and mongorestore

Author:Chuan Chen 阅读数:36061人阅读 分类: MongoDB

mongodump and mongorestore are two utility tools officially provided by MongoDB for database backup and restoration operations. They support full or partial data export/import and are suitable for scenarios such as data migration and disaster recovery.

Basic Usage of mongodump

mongodump is used to export data from a MongoDB instance to BSON files. The basic command format is as follows:

mongodump --uri="mongodb://username:password@host:port/database" --out=/path/to/output

Common parameters:

  • --uri: Connection string
  • --out/-o: Output directory
  • --db: Specify a database
  • --collection: Specify a collection
  • --query: Filter data using a JSON query
  • --gzip: Compress the output

Example 1: Backup an entire instance

mongodump --host=127.0.0.1 --port=27017 --out=/backups/mongodump-2023-11-20

Example 2: Backup a specific collection

mongodump --db=test --collection=users --out=/backups/partial

Basic Usage of mongorestore

mongorestore is used to import BSON files generated by mongodump into MongoDB:

mongorestore --uri="mongodb://username:password@host:port" /path/to/dump

Key parameters:

  • --drop: Drop existing collections before restoring
  • --nsInclude: Specify namespace patterns
  • --noIndexRestore: Skip restoring indexes
  • --gzip: Decompress during restoration

Example 3: Full restoration

mongorestore --host=cluster.example.com --port=27017 --username=admin --password=secret /backups/mongodump-2023-11-20

Advanced Usage Tips

Incremental Backup Strategy

Combine query conditions for incremental backups:

# Backup data from the last 24 hours
mongodump --db=logs --collection=events \
  --query='{ "timestamp": { "$gt": { "$date": "2023-11-19T00:00:00Z" } } }' \
  --out=/backups/incremental

Restoring Specific Collections Using Wildcards

mongorestore --nsInclude='test.users' /backups/full-dump

Cross-Version Compatibility Handling

When source and target MongoDB versions differ:

mongodump --host=old-server:27017 --db=production \
  --readPreference=secondaryPreferred \
  --out=/backups/cross-version

Practical Application Scenarios

Data Migration Case

Migrate data from production to a test environment:

# Export from production
mongodump --uri="mongodb://prod-user:password@prod-cluster:27017/prod-db" \
  --gzip --out=/backups/prod-export

# Import to test environment
mongorestore --uri="mongodb://test-user:password@test-cluster:27017/test-db" \
  --drop --gzip /backups/prod-export

Scheduled Backup Script Example

A Node.js script for automated backups:

const { exec } = require('child_process');
const cron = require('node-cron');
const path = require('path');
const fs = require('fs');

const BACKUP_DIR = '/mnt/backups/mongo';
const CONN_STR = 'mongodb://admin:password@localhost:27017';

function runBackup() {
  const date = new Date().toISOString().split('T')[0];
  const backupPath = path.join(BACKUP_DIR, date);
  
  if (!fs.existsSync(backupPath)) {
    fs.mkdirSync(backupPath, { recursive: true });
  }

  exec(`mongodump --uri="${CONN_STR}" --gzip --out="${backupPath}"`, 
    (error, stdout, stderr) => {
      if (error) {
        console.error(`Backup failed: ${error.message}`);
        return;
      }
      console.log(`Backup completed to ${backupPath}`);
    });
}

// Run daily at 2 AM
cron.schedule('0 2 * * *', runBackup);

Performance Optimization Recommendations

  1. Parallel Restoration: Split collections for parallel restoration on large databases
# Restore multiple collections in parallel
mongorestore --numParallelCollections=4 /backups/large-dump
  1. Network Optimization: Use --compressors=snappy on local networks

  2. Memory Adjustment: Increase batch size for large-scale restorations

mongorestore --batchSize=1000 /backups/huge-dump

Troubleshooting Common Issues

Authentication Failure Handling

When encountering authentication errors, check the connection string format:

# Correct format
mongodump --uri="mongodb://user:pass@host:port/db?authSource=admin"

# Incorrect example (missing authSource)
mongodump --uri="mongodb://user:pass@host:port/db"

Version Compatibility Issues

Use --forceTableScan to resolve index issues:

mongodump --db=legacy --forceTableScan --out=/backups/legacy-data

Handling Large Collections

For collections exceeding 100GB:

# Split the backup
mongodump --db=bigdata --collection=huge_collection \
  --query='{ "_id": { "$lt": ObjectId("...") } }' \
  --out=/backups/part1

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.