Skip to content

Streaming mongodump for large datasets: archive mode and parallelism

Use mongodump --archive and --numParallelCollections to stream large MongoDB backups to S3 or a remote disk without filling local storage.

mongodump is fine for small databases and a foot-gun on large ones if you treat it like a desktop backup tool. Dumping a 2 TiB collection to a directory of BSON files fills the local disk before the dump finishes; running it serially against a sharded cluster takes a weekend. The fix is --archive (single-stream output you can pipe directly to S3 or gpg) combined with --numParallelCollections and --gzip. This article shows the streaming patterns that scale to terabyte-class datasets.

How to verify

Stream a test dump through pv to see the bytes flow and confirm the archive is well-formed:

mongodump --uri="mongodb://backup:secret@mongo1:27017/?authSource=admin" \
  --archive --gzip | pv -ptebar > /tmp/test.archive.gz
gunzip -c /tmp/test.archive.gz | head -c 32 | xxd
mongorestore --archive=/tmp/test.archive.gz --gzip --dryRun
df -h /tmp

mongorestore --dryRun reads the archive without writing anything; if it parses, the archive is valid. The archive format starts with a known header (6d 6f 6e 67 6f — “mongo” in ASCII).

What’s happening

mongodump reads documents from a mongod or mongos, serializes them to BSON, and writes them somewhere. The default output is a directory tree (dumps/dbname/collname.bson + collname.metadata.json); --archive collapses everything into a single binary stream that can be piped or written to a file. Combined with --gzip, the stream is compressed before it leaves the dumping host — typically a 3-5× reduction for document data, less for already-compressed binary fields.

--numParallelCollections controls how many collections mongodump reads concurrently from a single source. The default is 4. On a fast network and a large dataset, raising it to 8 or 16 reduces wall-clock time substantially, but pushes more load onto the source — read concern of the source replica is now amplified by parallelism. For consistency, point mongodump at a secondary (--readPreference secondary) or, better, at a hidden member dedicated to backups.

For sharded clusters, dumping through mongos is supported but slow. The recommended pattern is to dump each shard’s replica-set separately (against secondaries), then dump the config-server replica-set, then restore in the same shape. The trade-off: shard-direct dumps are not point-in-time consistent across shards. If you need cluster-wide consistency, use the Atlas snapshot feature or Ops Manager continuous backup.

The procedure

  1. Run against a dedicated backup secondary. A hidden: true, priority: 0 member exists to do dumps and not affect election or load distribution.

    const cfg = rs.conf()
    cfg.members.push({ _id: 4, host: "mongo-backup:27017", hidden: true, priority: 0, votes: 0 })
    rs.reconfig(cfg)
  2. Pipe the archive directly to S3 without intermediate disk. The AWS CLI’s s3 cp - reads stdin.

    mongodump \
      --uri="mongodb://backup:secret@mongo-backup:27017/?readPreference=secondary&authSource=admin" \
      --archive --gzip --oplog --numParallelCollections 8 \
    | aws s3 cp - "s3://backups/mongo/$(date -u +%Y/%m/%d/%H%M)-prod.archive.gz" \
      --storage-class STANDARD_IA \
      --expected-size 2199023255552
  3. Encrypt before the bytes leave the dumping host with gpg in the pipeline.

    mongodump --uri=... --archive --gzip --oplog \
    | gpg --batch --yes --encrypt --recipient backup@example.internal \
    | aws s3 cp - "s3://backups/mongo/$(date -u +%Y/%m/%d/%H%M)-prod.archive.gz.gpg"
  4. Use --oplog for point-in-time consistency of the dump itself. Without it, the dump is consistent per-collection but not across collections.

  5. For sharded clusters, dump each shard separately against its hidden secondary. Run in parallel from a coordinator host.

    for shard in shard0 shard1 shard2; do
      mongodump --uri="mongodb://backup:secret@$shard-backup:27017/?authSource=admin" \
        --archive --gzip --oplog \
      | aws s3 cp - "s3://backups/mongo/$(date -u +%Y/%m/%d)/$shard.archive.gz" &
    done
    wait
  6. Verify restorability monthly. A backup you have never restored is a hope, not a backup.

    aws s3 cp "s3://backups/mongo/$LATEST" - \
    | gpg --decrypt \
    | mongorestore --archive --gzip --nsInclude="shop.users" \
        --uri="mongodb://restore:secret@restore-target:27017"

Operational notes

  • mongodump is logical backup. Restore time is hours, not minutes — every document is re-inserted. For aggressive RTO targets, prefer filesystem snapshots of a backup secondary or Ops Manager block-level continuous backup.

  • --oplog requires a replica-set. Standalone mongod cannot produce a point-in-time consistent dump.

  • The hidden secondary’s storage must hold the dump’s working set. Snapshot reads consume cache; an undersized backup secondary stalls.

  • Compression is CPU-bound. Gzip in mongodump is single-threaded per pipeline; for very large dumps, pipe to pigz (parallel gzip) instead.

    mongodump --uri=... --archive | pigz -p 8 | aws s3 cp - "s3://..."
  • S3 multipart upload boundaries matter. A 5 GiB single-part upload is the maximum without multipart; the AWS CLI handles this automatically when given --expected-size. For terabyte dumps, set --cli-write-timeout 0 to avoid client-side timeouts.

Stack Harbor runs mongodump-based backups for clients whose datasets fit logical backup windows and need encrypted, region-replicated archives. We pair them with snapshot-based backups for fast restore and PIT recovery, and verify restorability on a cadence rather than on demand. See /en/services/managed-operations/ for the engagement model around MongoDB backup and recovery.