Docs Menu
Docs Home
/
MongoDB Manual
/ / /

Back Up a Sharded Cluster with Database Dumps

To take a consistent backup of a sharded cluster with mongodump, you must first stop the balancer, stop writes, and stop any schema transformation operations on the cluster. This ensures that the cluster remains in a consistent state for the duration of the backup.

MongoDB provides backup and restore operations that can run with the balancer and running transactions through the following services:

This task uses mongodump to back up a sharded cluster. Ensure that you have a cluster running that contains data in sharded collections.

This procedure requires a version of MongoDB that supports fsync locking from mongos.

Starting in MongoDB 4.4.25, the fsync and fsyncUnlock commands can run on mongos to lock and unlock a sharded cluster.

To perform these tasks, your user must have the fsync authorization, which allows the user to run the fsync and fsyncUnlock commands.

To take a self-managed backup of a sharded cluster, complete the following steps:

1

To find a good time to perform a backup, monitor your application and database usage to find a time when chunk migrations, resharding, and schema transformation operations are unlikely to occur, as these can cause an inconsistent backup.

For more information, see Schedule Backup Window for Sharded Clusters.

2

To prevent chunk migrations from distruping the backup, connect to mongos and use the sh.stopBalancer() method to stop the balancer:

sh.stopBalancer()

If a balancing round is in progress, the operation waits for balancing to complete.

To confirm that the balancer is stopped, use the sh.getBalancerState() method:

sh.getBalancerState()
false

The command returns false when the balancer is stopped.

3

The sharded cluster must remain locked during the backup process to protect the database from writes, which may cause inconsistencies in the backup.

To lock a sharded cluster, connect to mongos and use the db.fsyncLock() method:

db.getSiblingDB("admin").fsyncLock()

To confirm the lock, on mongos and the primary mongod of the config servers, run the following aggregation pipeline and ensure that all the shards are locked:

db.getSiblingDB("admin").aggregate( [
{ $currentOp: { } },
{ $facet: {
"locked": [
{ $match: { $and: [
{ fsyncLock: { $exists: true } },
{ fsyncLock: true }
] } }],
"unlocked": [
{ $match: { fsyncLock: { $exists: false } } }
]
} },
{ $project: {
"fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
"fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
} }
] )
[ { fsyncLocked: true }, { fsyncUnlocked: false } ]
4

To back up the sharded cluster, use mongodump to connect to mongos and perform the backup:

mongodump \
--host mongos.example.net \
--port 27017 \
--username user \
--password "passwd" \
--out /opt/backups/example-cluster-1
5

After the backup completes, you can unlock the cluster to allow writes to resume.

To unlock the cluster, use the db.fsyncUnlock() method:

db.getSibling("admin").fsyncUnlock()

To confirm the unlock, on mongos and the primary mongod of the config servers, run the following aggregation pipeline and ensure that all shards are unlocked:

db.getSiblingDB("admin").aggregate( [
{ $currentOp: { } },
{ $facet: {
"locked": [
{ $match: { $and: [
{ fsyncLock: { $exists: true } },
{ fsyncLock: true }
] } }],
"unlocked": [
{ $match: { fsyncLock: { $exists: false } } }
]
} },
{ $project: {
"fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
"fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
} }
] )
[ { fsyncLocked: false }, { fsyncUnlocked: true } ]
6

To restart the balancer, use the sh.startBalancer() method:

sh.startBalancer()

To confirm that the balancer is running, use the sh.getBalancerState() method:

sh.getBalancerState()
true

The command returns true when the balancer is running.

You can restore a database from a mongodump backup using mongorestore.

  • To restore a sharded cluster, execute mongorestore against mongos.

  • To migrate to a replica set or standalone server, execute mongorestore against mongod.

For more information, see Back Up and Restore with MongoDB Tools.

Back

Back Up a Sharded Cluster with File System Snapshots