> This page location: /docs.md > /docs/management.md > /docs/manual/core/backups.md > /docs/manual/administration/backup-sharded-clusters.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Back Up a Self-Managed Sharded Cluster with a Database Dump

Starting in MongoDB 7.1 (also available starting in 7.0.2, 6.0.11, and 5.0.22), you can back up data on sharded clusters using [`mongodump`.](https://www.mongodb.com/docs/database-tools/mongodump.md#std-program-mongodump)

## About this Task

[`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-program-mongodump) is a utility that creates a binary export of database content. You can use the `mongodump` utility to take self-managed backups of a sharded cluster.

To back up a sharded cluster with `mongodump`, you must stop the balancer, stop writes, and stop any schema transformation operations on the cluster. This helps reduce the likelihood of inconsistencies in the backup.

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

- [MongoDB Atlas](https://www.mongodb.com/atlas/database)

- [MongoDB Cloud Manager](https://www.mongodb.com/cloud/cloud-manager)

- [MongoDB Ops Manager](https://www.mongodb.com/products/ops-manager)

### Stale Backups

Backups provide a snapshot of the current state of the database. When you restore from a backup, the restored database doesn't include any changes made after the backup was taken, which can result in data loss.

## Before you Begin

This task uses [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-program-mongodump) to back up a sharded cluster. Ensure that you have a cluster running that contains data in sharded collections.

### Version Compatibility

This procedure requires a version of MongoDB that supports fsync locking from [`mongos`.](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos)

Starting in MongoDB 7.1 (also available starting in 7.0.2, 6.0.11, and 5.0.22) the [`fsync`](https://www.mongodb.com/docs/manual/reference/command/fsync.md#mongodb-dbcommand-dbcmd.fsync) and
[`fsyncUnlock`](https://www.mongodb.com/docs/manual/reference/command/fsyncunlock.md#mongodb-dbcommand-dbcmd.fsyncUnlock) commands can run on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos) to lock and unlock a sharded cluster.

### Admin Privileges

To use this procedure, your MongoDB user must have the [`fsync`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-fsync) authorization, which can be available through a custom role or using the built-in [`hostManager`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-hostManager) role.

With this authorization you can run the [`fsync`](https://www.mongodb.com/docs/manual/reference/command/fsync.md#mongodb-dbcommand-dbcmd.fsync) and [`fsyncUnlock`](https://www.mongodb.com/docs/manual/reference/command/fsyncunlock.md#mongodb-dbcommand-dbcmd.fsyncUnlock) commands.

## Steps

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

1. Find a Backup Window

   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.

   **Note:**

   These steps can only produce a consistent backup if they are followed exactly and no operations are in progress when you begin.

   For more information, see [Schedule Backup Window for a Self-Managed Sharded Cluster.](https://www.mongodb.com/docs/manual/tutorial/schedule-backup-window-for-sharded-clusters.md#std-label-sharded-schedule-backup)

2. Stop the Balancer

   To prevent chunk migrations from disrupting the backup, connect to [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos) and use the [`sh.stopBalancer()`](https://www.mongodb.com/docs/manual/reference/method/sh.stopbalancer.md#mongodb-method-sh.stopBalancer) method to stop the balancer:

   ```javascript
   sh.stopBalancer()
   ```

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

   To verify that the balancer is stopped, use the [`sh.getBalancerState()`](https://www.mongodb.com/docs/manual/reference/method/sh.getbalancerstate.md#mongodb-method-sh.getBalancerState) method:

   ```javascript
   use config
   while( sh.isBalancerRunning().mode != "off" ) {
      print( "Waiting for Balancer to stop..." );
      sleep( 1000 );
   }
   ```

3. Lock the Cluster

   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`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos) and use the [`db.fsyncLock()`](https://www.mongodb.com/docs/manual/reference/method/db.fsynclock.md#mongodb-method-db.fsyncLock) method:

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

   To confirm the lock, on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos) and the primary [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-program-mongod) of the config servers, run the following aggregation pipeline and ensure that all the shards are locked:

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

   **Output:**

   ```json
   [ { fsyncLocked: true }, { fsyncUnlocked: false } ]
   ```

4. Take Backup

   To back up the sharded cluster, use `mongodump` to connect to [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos) and perform the backup:

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

5. Unlock the Cluster

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

   To unlock the cluster, use the [`db.fsyncUnlock()`](https://www.mongodb.com/docs/manual/reference/method/db.fsyncunlock.md#mongodb-method-db.fsyncUnlock) method:

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

   To confirm the unlock, on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos) and the primary [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-program-mongod) of the config servers, run the following aggregation pipeline and ensure that all shards are unlocked:

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

   **Output:**

   ```json
   [ { fsyncLocked: false }, { fsyncUnlocked: true } ]
   ```

6. Restart the Balancer

   To restart the balancer, use the [`sh.startBalancer()`](https://www.mongodb.com/docs/manual/reference/method/sh.startbalancer.md#mongodb-method-sh.startBalancer) method:

   ```javascript
   sh.startBalancer()
   ```

   To confirm that the balancer is running, use the [`sh.getBalancerState()`](https://www.mongodb.com/docs/manual/reference/method/sh.getbalancerstate.md#mongodb-method-sh.getBalancerState) method:

   ```javascript
   sh.getBalancerState()
   ```

   **Output:**

   ```javascript
   true
   ```

   The command returns `true` when the balancer is running.

## Next Steps

To restore the backup:

- To a sharded cluster, see [Restore a Self-Managed Sharded Cluster from Database Dumps.](https://www.mongodb.com/docs/manual/tutorial/restore-sharded-cluster-with-database-dumps.md#std-label-restore-sharded-dumps)

- To a replica set or standalone, run [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-program-mongorestore) directly against [`mongod`:](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-program-mongod)

  ```bash
  mongorestore --nsExclude='config.*' /path/to/backup
  ```

For more information, see [Back Up and Restore with MongoDB Tools.](https://www.mongodb.com/docs/manual/tutorial/backup-and-restore-tools.md#std-label-manual-tutorial-backup-and-restore)
