skip to content

Backup & Admin Tooling

The tools a team runs against a self-managed MongoDB: dumps and restores, filesystem snapshots, the validate command, the profiler and live counters. Asked in platform and DBA loops.

part ofMongoDBread it →
on this pageshow

explore

questions

12

In MongoDB, what does mongodump write out, and why is mongoexport not a substitute for it as a backup?

level: juniorimportance: must knowfreq 50%

answer

  1. binary copy against text copy
  2. BSON files beside metadata files
  3. definitions, not index data
  4. one database a full run skips
  5. one collection at a time

basics

~20 s

mongodump writes a binary copy: BSON documents plus metadata holding collection options and index definitions. mongoexport writes one collection as JSON or CSV with no indexes or options, and its docs say plainly that it is not a backup tool.

solid answer

~40 s

`mongodump` connects to a running `mongod` or `mongos` and writes each collection as a `.bson` file beside a `.metadata.json` file that holds its options, UUID and index definitions. A full run also carries users and roles, and it skips the `local` database. The indexes themselves aren't copied, only their definitions, so `mongorestore` rebuilds them after loading the data. `mongoexport` takes one collection per run and writes JSON or CSV. Its default relaxed Extended JSON can lose type detail, it records no indexes or collection options, and the tools docs say it isn't meant for backing up, or for converting data to JSON and back. So I use `mongodump` to back up a small deployment and `mongoexport` when a person or another system needs to read the data.

code

bash · 3 lines
bash
mongodump --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=rs0" --out=/opt/backup/nightly

mongoexport --uri="mongodb://mongodb0.example.com:27017/reporting" --collection=events --type=csv --fields=_id,status --out=events.csv

go deeper

for a junior

Recall the two outputs: BSON plus metadata for mongodump, JSON or CSV for mongoexport. Say out loud that index definitions travel in a dump and index data does not.

for a middle

Explain the restore consequence: documents are inserted and indexes rebuilt. Name what a full run skips, starting with the local database, and what view metadata means for a view's documents.

for a senior

Show you check a dump against what the deployment really holds. Encrypted collections, views and system collections all behave differently, and a dump that looks complete can restore into something that isn't.

for a principal

Frame the two tools as different contracts: one promises a way back into MongoDB, the other promises readable output. Keep exports out of recovery plans so nobody mistakes a data feed for a backup.

## What `mongodump` writes `mongodump` is a command-line tool from the MongoDB Database Tools. It connects to a running `mongod` or `mongos`, reads the data through the server and writes a **binary export**. It doesn't copy the storage engine's files. A directory dump contains: - A root directory, `dump/` unless `--out` names another. - One subdirectory per database. - One `.bson` file per collection, holding its documents. - One `.metadata.json` file per collection, holding its options, UUID and index definitions. - A metadata file for each view, with no `.bson` file beside it. - An `oplog.bson` file at the top, only when the run used `--oplog`. `--archive` puts all of that into one file or stream, and `--gzip` compresses the output. When the source uses WiredTiger, `mongodump` writes the data uncompressed, so a dump isn't the size of the compressed files on disk. Running it again over the same folder overwrites what is there. A full run also includes user and role definitions. `mongorestore` loads the result by inserting the documents and then **rebuilds the indexes** from the recorded definitions. That is what makes a dump **logical**: it holds the data and the instructions for rebuilding everything else, not the physical structures. ## What a dump leaves behind The list is worth knowing, because each item is something a restored deployment won't have: - **Index data.** Only definitions travel, so every index is built again on restore. - **The `local` database.** A full run skips it, and you get it only by naming it with `--db=local`. - **Most of `config`.** A full run keeps only `chunks`, `collections`, `databases`, `settings`, `shards`, `tags` and `version`. - **`admin.system.keys`,** which the tool always skips. - **`system.*` collections in ordinary databases,** except `system.js`. - **View contents.** A view keeps only its metadata, unless `--viewsAsCollections` dumps its documents as a collection instead. - **Encrypted collections.** The docs say you can't use `mongodump` on a collection that uses Queryable Encryption or client-side field level encryption. - **Anything written later.** A dump describes the past, and without `--oplog` it may not describe one single moment. ## What `mongoexport` writes `mongoexport` produces text for people and other systems: - It needs a collection, and it fails with `must specify a collection` otherwise. - `--type` defaults to `json`. `csv` also needs `--fields` or `--fieldFile`. - `--jsonFormat` defaults to `relaxed`. `canonical` keeps BSON types that relaxed mode can blur. - `mongoimport` is the matching way back in. The tools docs are direct about it: `mongoimport` and `mongoexport` aren't intended for backing up and restoring data, or for converting data to another format and back to a cluster. The reason follows from the output. Each file holds one collection's documents, with no index definitions, collection options, users or roles, so rebuilding a deployment from a set of exports means rebuilding everything else by hand. ## Side by side | | `mongodump` | `mongoexport` | |---|---|---| | Output | BSON files, or one archive | JSON or CSV | | Scope | Server, database, collection or query | One collection per run | | Index definitions and options | Recorded in metadata | Not recorded | | Users and roles | Included in a full run | Not included | | Way back in | `mongorestore` | `mongoimport` | ## Which one to reach for 1. To be able to bring a small deployment back, use `mongodump`. The manual describes the pair as useful for backing up small deployments, and points larger ones at snapshots. 2. To give an analyst a CSV, or feed one collection to another system, use `mongoexport`. 3. To move data between MongoDB deployments, use `mongodump` and `mongorestore`, since a trip through JSON is the case the docs warn against. ## Before you rely on a dump - **Version match.** Restore into the same major version or feature compatibility version as the source, and use the same tools version for `mongodump` and `mongorestore`. The Database Tools version separately from the server. - **File names.** On a case-insensitive filesystem, collections whose names differ only by case overwrite each other in a directory dump, so the docs say to use `--archive` there. - **A restore test.** The guidance is to verify a backup by restoring it to a test deployment, because a dump that was written isn't yet a dump that was read back.

  • Why can a mongodump be smaller than a copy of the data files and still be slower to restore?
    The manual notes that copied files are larger because they include indexes plus storage padding and fragmentation. WiredTiger's block compression can narrow that gap, because `mongodump` writes uncompressed data unless you add `--gzip`. Restore is slower because the server has to insert every document and then build every index, where restored files only need to be put in place and started.
  • If mongoexport's --jsonFormat=canonical keeps BSON types, does that make it a backup?
    No. Canonical mode fixes one problem, the type detail that default relaxed mode can blur. The output still covers one collection per run and carries no index definitions, collection options, users or roles, and the tools docs still say `mongoexport` isn't intended for backing up and restoring data.

  • A mongodump file contains the index data, so a restore needs no index builds.
  • mongoexport to JSON makes a fine nightly backup of a whole deployment.
  • A full mongodump includes the local database, so it carries the whole oplog.
  • mongodump copies the WiredTiger data files as they sit on disk.
  • Users and roles never travel in a mongodump, so they must be recreated by hand.
open as a page

In MongoDB, what do profiling levels 0, 1 and 2 record, and how do `slowms`, `sampleRate` and the slow-query log relate to them?

level: middleimportance: must knowfreq 52%

basics

~20 s

Level 0 records nothing in system.profile, level 1 records operations at or above slowms or matching a filter, and level 2 records everything. Separately, slowms and sampleRate also drive the slow-query log, which keeps working at level 0.

open as a page

A nightly mongodump of a busy MongoDB replica set restores with documents that disagree with each other; what went wrong, and what do --oplog and --oplogReplay guarantee?

level: seniorimportance: must knowfreq 52%

basics

~20 s

Without --oplog a dump reads collections at different moments, so writes during the run land in some and not others. --oplog captures those writes and --oplogReplay applies them, giving the state at the dump's end, not a time you choose.

open as a page

What do MongoDB's `mongostat` and `mongotop` each show, and where do their numbers come from?

level: juniorimportance: should knowfreq 27%

basics

~20 s

mongostat prints server-wide rates such as inserts, queries and connections per interval, built from serverStatus. mongotop prints milliseconds of read and write time per collection, built from the top command. Together they show how busy and where.

open as a page

Using mongorestore, how do you bring back one MongoDB collection from a nightly gzipped mongodump archive, and what happens to documents already there?

level: middleimportance: should knowfreq 42%

basics

~10 s

Run mongorestore with --gzip, --archive and --nsInclude=db.collection. It only inserts, so a document whose _id already exists stays as it is and counts as a failure, unless --drop removes that collection first.

open as a page

What does MongoDB's `validate` command check on a collection, what does it lock, and what do `full` and `background` change?

level: middleimportance: should knowfreq 24%

basics

~10 s

MongoDB's validate scans one collection's documents and indexes for damage on one member. By default it holds an exclusive collection lock; full adds storage-engine verification, while background reads the last stable checkpoint without blocking.

open as a page

How do you back up a self-managed sharded MongoDB cluster, and why can't you simply run mongodump --oplog through mongos?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Stop the balancer, DDL and writes, take db.fsyncLock() through mongos, then dump through mongos or snapshot the config server and each shard, then unlock and restart the balancer. mongodump refuses --oplog on mongos, so only stopped writes hold the copy together.

open as a page

When does a filesystem snapshot of a running MongoDB mongod need db.fsyncLock(), and what must you do to restore it into a new replica set?

level: seniorimportance: should knowfreq 40%

basics

~20 s

One snapshot holding data files and journal together needs no lock outside a sharded cluster. A separate journal volume, or disks that can't be captured together, need db.fsyncLock(). On restore, drop local and initiate a new set.

open as a page

MongoDB API calls turn slow right now: how do you use `serverStatus`, `$currentOp` and `killOp` to find and stop the cause, and what can go wrong?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Sample serverStatus twice to see the load, list live work with $currentOp and allUsers, then kill a confirmed client operation with killOp. The kill is only a mark that takes effect at a safe point, so verify it.

open as a page

Your self-managed three-member MongoDB replica set holds 2 TB and has a one-hour recovery objective, yet backups are a nightly mongodump; how do you decide what replaces it and prove it restores?

level: principalimportance: should knowfreq 35%

basics

~20 s

A nightly mongodump at 2 TB is unlikely to restore within an hour, because every document is inserted and every index rebuilt. I'd move whole-set recovery to snapshots or a coordinated service, and judge it by timed restores to a test deployment.

open as a page

What should a team running self-managed MongoDB leave on in production: profiler level, slow-query thresholds, and who may kill an operation?

level: principalimportance: should knowfreq 36%

basics

~20 s

A sensible default leaves the profiler off, ships the slow-query log with a threshold tuned to its volume, polls serverStatus, and time-boxes level 1 for investigations. Kills go to a narrow inprog and killop role, not to wide administrative roles.

open as a page

A disk fault is reported on one member of a MongoDB replica set: how do `validate`, `dbCheck` and `repair` each fit into deciding what to do?

level: seniorimportance: nice to knowfreq 15%

basics

~20 s

Run validate on the suspect member to check its own structures, and dbCheck from the primary to compare members through hashed batches. Then resync the damaged member from an intact one; repair is standalone only and removes corrupt documents.

open as a page