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?
answer
- data files and journal together
- a snapshot taken across several disks
- writes stop, and reads may follow
- lockCount must return to zero
- the local database on the way back
basics
~20 sOne 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.
solid answer
~50 sA snapshot of a running `mongod` is correct without a lock when WiredTiger's data files and journal sit on one volume and the snapshot captures them together: restarting from it looks like recovering from a crash, and any write not yet on disk isn't included. I need `db.fsyncLock()` when the journal is on a separate volume, when striped disks can't be snapshotted as one, or on a sharded cluster. The lock flushes writes and blocks new ones, and reads wait too once a write is attempted, so on a replica set I use a hidden secondary. `db.fsyncUnlock()` must bring `lockCount` back to zero. To restore into a new set, I start a standalone on the files, drop `local`, restart with `--replSet`, run `rs.initiate()` with one member, and add the rest by copying files or by initial sync.
code
javascript · 5 lines// mongosh, connected directly to the hidden secondary
db.fsyncLock()
// take the volume snapshot from the OS shell now, then:
db.fsyncUnlock()go deeper
Recall that a snapshot copies the files, indexes included, and that db.fsyncLock and db.fsyncUnlock go in a pair. Know that the journal's location is what the question turns on.
Explain why data files and journal must be captured together, and why a lock is needed when they can't be. Predict what the lock does to writes and then to reads.
Run it on a hidden secondary, watch lockCount and handle an election during the lock. Know the restore order, including dropping local, and why copying to three hosts first forces an initial sync.
Standardise the volume layout so the lockless path is available, since a lock is a recurring operational risk. Own off-site copies and restore rehearsal, because a snapshot on the source storage protects little.
## What a snapshot is relying on A **filesystem snapshot** is a volume manager or storage feature that captures a volume at one moment. It isn't a MongoDB feature, and the files it captures are the live `mongod`'s own, including indexes. For the result to be usable, the manual says the database must be valid at that moment: every accepted write must be on disk, in WiredTiger's data files or its journal. A write that hasn't reached disk simply isn't in the backup. The data files reflect the last checkpoint, and the journal holds what came after. That is why the two must be captured **at the same instant**. A `mongod` started on such a snapshot recovers the way it does after a crash. ## When you need the lock | Setup | `db.fsyncLock()` needed? | |---|---| | Data files and journal on one volume, one snapshot | No | | Journal on a separate volume | Yes | | Striped RAID where the platform snapshots each disk apart | Yes, or put a logical volume on top of the RAID and snapshot that | | Sharded cluster | Yes, per its own procedure | | Copying files with `cp` or `rsync` | Stop all writes, because a file copy is not atomic | The manual's overview says the journal must be on the same logical volume for a correct snapshot of a running `mongod`, and its tutorial adds that separate volumes are supported only if the database is locked and writes suspended. Read together, the lock is what buys you the separate volume. ## What the lock does `db.fsyncLock()` flushes pending writes to disk and blocks further writes until the lock is released: - **Reads aren't safe either.** Other connections may keep reading until the first write is attempted, and then they wait too. On a busy node, that means reads stall. - **Use a quiet member.** For a replica set the tutorial says to use a secondary that isn't receiving reads, meaning a hidden member. Connect to it directly, since the command is allowed on a secondary. - **It lags while locked.** The locked member can't apply replicated writes, so keep the window short. - **It is counted.** Each lock adds to `lockCount`. Call `db.fsyncUnlock()` until the count is zero, or writes stay blocked. - **Files may still change.** Journal syncs and checkpoints can alter data files under the lock. The logical data doesn't change, though some copy tools may warn. - **An election breaks it.** If a locked primary goes down, the new primary holds no lock and accepts writes, and the backup may be inconsistent or unusable. Unlock until the count reaches zero, lock again and restart the backup. The old primary needs its own unlock when it returns. ## Getting it off the box A snapshot usually lives on the same storage as the original, so it isn't protection against losing that storage. The manual says to archive it elsewhere. On LVM, a snapshot that runs out of its allotted space becomes unusable, so size it for changes during the copy-out. ## Restoring into a new replica set The manual warns that you can't copy one data set onto three new hosts and **then** create the set, because MongoDB will force the secondaries through an initial sync. The order is: 1. Restore the files to a volume and mount it. The restored files carry a stale `mongod.lock`, and with `db.fsyncLock()` backups the manual says to remove it. 2. Start a standalone `mongod` on the files with the same startup options as when the snapshot was taken. 3. Run `db.dropDatabase()` in `local`, then shut down. This discards the old set's identity and oplog. 4. Restart with `--replSet` and run `rs.initiate()` with one member. 5. Add the other members: shut down cleanly, copy the data directory to each, restart, and use `rs.add()`, or let initial sync distribute the data, which the manual notes can be slow on a large database. The Enterprise encrypted storage engine with `AES256-GCM` adds a rule: after restoring a filesystem copy taken while `mongod` was stopped, roll the database keys with `--eseDatabaseKeyRollover` to avoid reusing them.
- Why take the snapshot from a hidden secondary rather than the primary?If the lock is needed, it blocks writes on the node that holds it, and reads there wait too once a write arrives. On a hidden member that hurts nobody, since it gets no application reads. It only falls behind for a while. The manual's tutorial says to use a secondary that isn't receiving reads, and names a hidden member.
- A primary holding an fsync lock goes down during the backup. What now?The elected primary doesn't inherit the lock and accepts writes, so the backup may be inconsistent or unusable. The manual's recovery is to run `db.fsyncUnlock()` until the count is zero, take the lock again, and restart the backup. The old primary stays locked when it comes back and needs its own unlock.
- Every filesystem snapshot of a running mongod needs db.fsyncLock first.
- db.fsyncLock only blocks writes, so reads on that node are never affected.
- One db.fsyncUnlock always releases the lock, however many times it was taken.
- A snapshot sitting on the same storage as the data is already a safe backup.
- You can keep the old local database when seeding a new replica set from a snapshot.
- Copying live data files with rsync is as safe as an atomic volume snapshot.