8.3 Backup, Restore & Performance Telemetry
Key Takeaways
- mongodump and mongorestore are binary backup and restoration utilities that export and ingest raw BSON (.bson) and index metadata (.metadata.json), preserving 100% type fidelity and index definitions.
- Point-in-time consistent backups of live replica sets are achieved using 'mongodump --oplog' and restored via 'mongorestore --oplogReplay' to replay operations captured during the backup window.
- Key backup flags include --archive (single consolidated stream), --gzip (compression), --nsInclude/--nsExclude (namespace filtering), and --drop (drops target collection before restore).
- mongostat monitors real-time, second-by-second cluster operational telemetry, including CRUD rates, WiredTiger dirty/used cache percentages, active connections, and queued operations.
- mongotop profiles read and write time utilization per collection, instantly identifying database I/O hotspots.
8.3 Backup, Restore & Performance Telemetry
Maintaining data durability, executing rapid disaster recovery, and diagnosing real-time performance bottlenecks are fundamental responsibilities in MongoDB database administration and application development. MongoDB provides a comprehensive suite of binary backup tools and command-line telemetry utilities:
mongodump&mongorestore: High-performance binary BSON backup and restoration utilities that preserve exact data types, index definitions, and collection metadata.mongostat: Real-time server telemetry monitor that reports live CRUD operation throughput, active connections, lock queues, and WiredTiger cache pressure.mongotop: Real-time profiling monitor that tracks CPU and lock time utilization on a per-collection basis.
1. Binary Backups: mongodump & mongorestore Fundamentals
Unlike mongoexport (which generates lossy, human-readable JSON or CSV text files), mongodump creates a binary BSON dump of the database. It connects directly to a running mongod or mongos instance, reads the raw documents, and outputs binary .bson files alongside .metadata.json files.
Structure of a mongodump Output Directory
By default, mongodump creates a directory named dump/ containing subdirectories for each database:
dump/
├── ecommerce/
│ ├── customers.bson <- Raw binary BSON document records
│ ├── customers.metadata.json <- Index definitions, collection options, collations
│ ├── orders.bson
│ └── orders.metadata.json
└── admin/
├── system.users.bson
└── system.users.metadata.json
Advantages of Binary BSON Dumps
- 100% BSON Type Fidelity: Types like
Decimal128, 64-bitLong,ISODate,ObjectId, andBinaryare preserved bit-for-bit without string conversion or lossy float casting. - Index Definitions Restored Automatically: The
.metadata.jsonfiles contain the full index specifications (compound indexes, partial indexes, TTL settings, unique constraints, and custom collation rules).mongorestoreautomatically rebuilds these indexes. - Storage Efficiency: Binary BSON combined with in-line compression (
--gzip) is significantly smaller and faster to process than text-based JSON exports.
2. Advanced mongodump Capabilities & Point-in-Time Consistency
The Critical --oplog Option for Live Consistent Snapshots
In an active production database, client applications continuously execute write operations while mongodump is running. If a dump takes 30 minutes to complete, documents dumped at minute 1 may reference documents modified or deleted by minute 29, resulting in an inconsistent backup snapshot.
To achieve Point-in-Time Consistency, mongodump provides the --oplog flag:
# Capture a point-in-time consistent binary backup of an active replica set
mongodump --uri="mongodb://primary.example.com:27017" \
--oplog \
--archive=./prod_backup_$(date +%Y%m%d).archive.gz \
--gzip
How --oplog Works Internally:
- When
mongodumpstarts, it records the current timestamp on the primary's Oplog (local.oplog.rs). - It dumps the collection documents while simultaneously tailing and capturing all write operations recorded in the oplog during the dump window.
- It writes these operations into an
oplog.bsonfile in the root of the dump output. - When
mongorestoreis executed with--oplogReplay, it restores the collections and then applies the captured oplog entries up to the completion point, bringing the entire dataset into a mathematically consistent, point-in-time state.
[!CAUTION]
--oplogRequirements: The--oplogoption only works against replica set nodes (or master-slave instances) and only when dumping the entire cluster/server (you cannot use--oplogwhen specifying an individual collection via--collection).
Essential mongodump Flags
| Flag | Purpose & Description |
|---|---|
--archive=<filePath> | Consolidates the entire backup into a single archive file instead of a folder hierarchy. If specified without a path (--archive), streams binary data directly to stdout. |
--gzip | Compresses BSON files or the archive stream using GZIP on the fly, dramatically reducing disk usage. |
--nsInclude="<db>.<coll>" | Granular namespace inclusion filter supporting wildcards (e.g., --nsInclude="sales.*" or --nsInclude="*.orders"). |
--nsExclude="<db>.<coll>" | Granular namespace exclusion filter (e.g., --nsExclude="analytics.audit_logs"). |
--oplog | Captures the oplog during the dump window to produce a point-in-time consistent snapshot. |
3. Restoration Strategies with mongorestore
mongorestore reads binary BSON archives or directories produced by mongodump and writes the documents, collections, and index specifications back into a target MongoDB cluster.
Essential mongorestore Flags
--drop: Drops each collection from the target database before restoring it from the dump. This prevents duplicate key conflicts against preexisting documents.--oplogReplay: Replays theoplog.bsoncaptured during amongodump --oplogbackup to guarantee point-in-time consistency.--nsInclude/--nsExclude: Selects specific namespaces to restore from an archive.--nsFromand--nsTo: Remaps namespaces dynamically during restoration (e.g., restoring data from production to a staging database).--noIndexRestore: Restores collection documents without building secondary indexes. Useful for staging environments where developers want to inspect raw data immediately or build indexes in the background later.--numParallelCollections <N>/-j <N>: Sets the number of collections to restore concurrently (defaults to 4).
Practical mongorestore Commands
# 1. Restore from a compressed single archive file with drop and oplog replay
mongorestore --uri="mongodb://localhost:27017" \
--archive=./prod_backup_20260902.archive.gz \
--gzip \
--drop \
--oplogReplay
# 2. Namespace Remapping: Restore 'production' database dump into 'staging'
mongorestore --uri="mongodb://staging.example.com:27017" \
--archive=./prod_backup.archive.gz \
--gzip \
--nsFrom="production.*" \
--nsTo="staging.*"
# 3. Restore a specific collection from a directory dump
mongorestore --uri="mongodb://localhost:27017" \
--db=ecommerce \
--collection=customers \
--drop \
./dump/ecommerce/customers.bson
4. Real-Time Operational Telemetry: mongostat
mongostat is a live diagnostic tool that provides a high-level, second-by-second overview of the status of a running mongod or mongos instance. It is analogous to the Unix vmstat or iostat utility.
Running mongostat
# Poll the database every 1 second (default)
mongostat --uri="mongodb://localhost:27017"
# Poll every 5 seconds, displaying 10 total iterations
mongostat --uri="mongodb://localhost:27017" 5 10
Interpreting mongostat Output Columns
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 12500 *0 *0 0 150|0 2.1% 78.4% 0 16.4G 12.1G 0|15 1|4 4.2m 18.6m 450 2026-09-02T10:15:01Z
| Column Group | Metric Header | Meaning & Diagnostic Significance |
|---|---|---|
| CRUD Rates | insert, query, update, delete | Number of respective operations executed per second. Asterisks (e.g., *0) indicate secondary replica node operations. |
| Cursor Batches | getmore | Number of getmore requests per second (fetching subsequent batches from open query cursors). High getmore relative to query indicates queries returning massive result sets. |
| Command Rate | command | Number of database commands executed per second (e.g. aggregate, findAndModify, count). Format `local |
| WiredTiger Cache | dirty | Percentage of the WiredTiger cache containing modified (dirty) bytes waiting to be flushed to disk. Critical: If dirty exceeds 20%, WiredTiger throttles incoming writes. |
| WiredTiger Cache | used | Percentage of configured WiredTiger cache currently occupied by data. If used exceeds 80%, background eviction threads aggressively page out clean data. |
| Checkpoints | flushes | Number of WiredTiger checkpoint flushes to disk during the polling interval. Typically 0 between 60-second checkpoint intervals. |
| Process Memory | vsize / res | Virtual memory (vsize) and Resident physical memory (res) allocated to the mongod process. |
| Lock Queues | `qr | qw` |
| Active Clients | `ar | aw` |
| Network I/O | net_in / net_out | Network traffic entering and leaving the instance (in bytes, KB, or MB). |
| Connections | conn | Total number of open incoming client connection sockets. |
Diagnostic Scenarios with mongostat
- Unindexed Query Storm:
queryis high,resis saturated,usedis near 100%, andqrspikes as CPU and memory churn searching unindexed collections. - Storage I/O Bottleneck:
dirtyexceeds 20%,qw(queued writers) rises steadily, andnet_indrops because the disk cannot flush modified pages fast enough to accept new writes. - Connection Leak:
conncontinuously climbs until reachingmaxConns, rejecting new client connections.
5. Collection-Level Profiling: mongotop
While mongostat monitors server-level operation counts and memory pressure, mongotop tracks the amount of time a mongod instance spends performing read and write operations on a per-collection basis.
Running mongotop
# Poll collection time utilization every 3 seconds
mongotop --uri="mongodb://localhost:27017" 3
Interpreting mongotop Output
2026-09-02T10:15:30Z
ns total read write
ecommerce.orders 1482ms 210ms 1272ms
ecommerce.inventory 340ms 315ms 25ms
ecommerce.customers 85ms 85ms 0ms
admin.system.roles 0ms 0ms 0ms
ns: Target collection namespace (<database>.<collection>).total: Total active time spent bymongodservicing operations on this collection during the polling interval.read: Time spent servicing read operations (queries, aggregations, scans).write: Time spent servicing write operations (inserts, updates, deletes).
Diagnostic Value of mongotop
- Pinpointing Hotspots: Instantly answers: "Which specific collection is consuming 90% of our database I/O?"
- Read vs. Write Characterization: Reveals whether a busy collection is read-dominated (
ecommerce.inventoryspending 315ms reading) or write-dominated (ecommerce.ordersspending 1272ms writing).
6. Diagnostic Tool Comparison Matrix
| Diagnostic Tool | Scope of Inspection | Primary Metrics Tracked | When to Use |
|---|---|---|---|
mongostat | Entire mongod/mongos Server Instance | Global ops/sec, CRUD rates, WiredTiger dirty/used cache %, `qr | qwlock queues,conn` |
mongotop | Per-Collection Namespace | Elapsed CPU/lock time (ms) spent in read, write, and total per collection | Identifying which specific collection is causing database I/O hotspots |
db.currentOp() | Individual In-Flight Operations / Threads | opid, secs_running, planSummary (COLLSCAN), query filter, client IP | Locating and killing specific slow or blocked queries in real time |
db.stats() | Database Storage Extents | dataSize, storageSize, indexSize, freeStorageSize, document count | Evaluating database capacity, compression ratio, and storage growth |
An administrator must take a full binary backup of an active, high-traffic production replica set while client applications continue to insert and update records. Which mongodump and mongorestore workflow ensures the restored database reflects a point-in-time consistent state?
While monitoring a cluster during peak traffic, an administrator observes in mongostat that the 'dirty' column is at 24% and the 'qw' column is rapidly increasing. What does this indicate about cluster health?
A development team reports that database read latency has spiked significantly. The administrator needs to identify which specific collection is consuming the largest amount of time servicing read operations across the entire database. Which tool should be used?
An administrator needs to restore a production database dump from 'prod_backup.archive.gz' into a development database named 'dev_testing', ensuring that any preexisting collections in 'dev_testing' are dropped first. Which command executes this correctly?