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

# Back Up and Restore with MongoDB Tools

This tutorial covers creating backups and restoring data using [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) and [`mongorestore`.](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore)

To restore a backup of your self-hosted deployment to a managed [MongoDB Atlas deployment](https://www.mongodb.com/docs/atlas), see [Seed with mongorestore.](https://www.mongodb.com/docs/atlas/import/mongorestore/)

## Considerations

### Deployments

The [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore) and [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) utilities work with [BSON](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) data dumps, and are useful for creating backups of small deployments. For resilient and non-disruptive backups, use [file system snapshots](https://www.mongodb.com/docs/manual/tutorial/backup-with-filesystem-snapshots.md#std-label-backup-restore-filesystem-snapshots) or block-level disk snapshots with [Cloud Backups](https://www.mongodb.com/docs/atlas/backup/cloud-backup/overview/) from MongoDB Atlas.

**Note: Back Up Sharded Clusters with MongoDB Atlas**

To use [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-program-mongodump) and [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-program-mongorestore) as a backup strategy for sharded clusters, see [Back Up a Self-Managed Sharded Cluster with a Database Dump.](https://www.mongodb.com/docs/manual/tutorial/backup-sharded-cluster-with-database-dumps.md#std-label-backup-sharded-dumps)

Sharded clusters can also use one of the following coordinated backup and restore processes, which maintain the atomicity guarantees of transactions across shards:

- [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)

### Performance Impacts

Because [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) and [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore) interact with a running [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance, they can impact database performance. The tools create traffic and force the database to read all data through memory. When MongoDB reads infrequently accessed data, it evicts frequently accessed data, degrading performance for the database's regular workload.

When backing up data with MongoDB tools, follow these guidelines:

- Label files to identify backup contents and the backup time.

- If the performance impact of [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) and [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore) is unacceptable, use an alternative such as [Filesystem Snapshots](https://www.mongodb.com/docs/manual/tutorial/backup-with-filesystem-snapshots.md#std-label-backup-restore-filesystem-snapshots) or [Cloud Backups in MongoDB Atlas.](https://www.mongodb.com/docs/atlas/backup/cloud-backup/overview/)

- To ensure [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) can take a consistent backup of a replica set, you must either use the [`--oplog`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--oplog) option to capture writes received during backup operations or stop all writes to the replica set for the duration of the backup.

  For sharded cluster replica sets, see [Back Up a Self-Managed Sharded Cluster with a Database Dump.](https://www.mongodb.com/docs/manual/tutorial/backup-sharded-cluster-with-database-dumps.md#std-label-backup-sharded-dumps)

- Verify your backups by restoring them to a test deployment.

- To reduce backup inconsistencies in a sharded cluster, stop the balancer, all write operations, and any schema transformations during the backup.

**See also:**

For more information, see [Backup Methods for a Self-Managed Deployment](https://www.mongodb.com/docs/manual/core/backups.md#std-label-backup-methods). For MongoDB Database Tools reference, see:

- [`mongoexport`](https://www.mongodb.com/docs/database-tools/mongoexport.md#mongodb-binary-bin.mongoexport)

- [`mongoimport`](https://www.mongodb.com/docs/database-tools/mongoimport.md#mongodb-binary-bin.mongoimport)

- [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump)

- [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore)

### Output Format

[`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore) and [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) can output data to an archive file, which is a single-file alternative to multiple BSON files. Archive files are special-purpose formats that support non-contiguous file writes. They enable concurrent backups from MongoDB and restores to MongoDB. Archive files also optimize disk I/O during backup and restore.

You can also write archive files to standard output (`stdout`). Writing to standard output enables data migration over networks, reduced disk I/O, and concurrency gains in both MongoDB tools and your storage engine.

For more information on archive files, see the [`--archive`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--archive) option.

### 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.

## Procedures

### Back Up a Database with `mongodump`

The [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) utility backs up data by connecting to a running [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod). It can back up an entire server, database, or collection, or use a query to back up part of a collection. `mongodump` excludes the content of the `local` database from its output.

Without any arguments, `mongodump` connects to the MongoDB instance on the local system on port `27017` and creates a database backup named `dump/` in the current directory:

```bash
mongodump
```

To specify the host and port, use one of the following:

- Specify the `--uri` string using an [SRV](https://www.mongodb.com/docs/manual/reference/connection-string-formats.md#std-label-connections-dns-seedlist) or [standard](https://www.mongodb.com/docs/manual/reference/connection-string-formats.md#std-label-connections-standard-connection-string-format) connection string:

  ```bash
  mongodump --uri="mongodb+srv://username:password@cluster0.example.mongodb.net" <additional_options>
  ```

- Specify the hostname and port in `--host`:

  ```bash
  mongodump --host="mongodb0.example.com:27017" <additional_options>
  ```

- Specify `--host` and `--port` separately:

  ```bash
  mongodump --host="mongodb0.example.com" --port=27017 <additional_options>
  ```

To specify a different output directory, use [`--out`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--out) (or `-o`):

```bash
mongodump --out=/opt/backup/mongodump-1
```

To limit the dump to a specific database or collection, use [`--db`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--db) and [`--collection`:](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--collection)

```none
mongodump --collection=myCollection --db=test
```

This creates a dump of `myCollection` from the `test` database in a `dump/` subdirectory of the current directory.

`mongodump` overwrites existing files in the output folder (default: `dump/`). Before running it multiple times, back up or rename the output folder.

#### Required Access

To run [`mongodump`](https://www.mongodb.com/docs/database-tools/mongodump.md#mongodb-binary-bin.mongodump) against a MongoDB deployment that has [access control](https://www.mongodb.com/docs/manual/core/authorization.md#std-label-authorization) enabled, you must have privileges that grant [`find`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-find) action for each database to back up. The built-in [`backup`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-backup) role provides the required privileges to perform backup of any and all databases.

The [`backup`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-backup) role provides additional privileges to back up the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection that exists when running with [database profiling.](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling)

#### Specify Host and Port

Use [`--host`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--host) and [`--port`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--port) to connect to a remote instance:

```bash
mongodump \
   --host=mongodb1.example.net \
   --port=3017 \
   --username=user \
   --password="pass" \
   --out=/opt/backup/mongodump-1
```

Specify username and password on any `mongodump` command to authenticate.

#### Create Backups Using Oplogs

The [`--oplog`](https://www.mongodb.com/docs/database-tools/mongodump.md#std-option-mongodump.--oplog) option collects [oplog](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-oplog) entries during the backup so you can restore the database to its state at the moment the backup completed.

With `--oplog`, `mongodump` copies all data from the source database and all oplog entries from the beginning to the end of the backup. Use this in conjunction with [`mongorestore --oplogReplay`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--oplogReplay) to restore a backup that reflects the exact moment `mongodump` completed.

### Restore a Database with `mongorestore`

The [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore) utility restores a binary backup created by `mongodump` by connecting to a running `mongod` directly. By default, `mongorestore` looks for a database backup in the `dump/` directory and can restore an entire backup or a subset.

**Note:**

All MongoDB collections have UUIDs (Universally unique identifiers) by default. When MongoDB restores collections, the restored collections retain their original UUIDs. When restoring a collection where no UUID was present, MongoDB generates a UUID for the restored collection.

For more information on collection UUIDs, see [Collections.](https://www.mongodb.com/docs/manual/core/databases-and-collections.md#std-label-collections)

To connect `mongorestore` to an active `mongod`:

```bash
mongorestore --uri <connection string> <path to the backup>
```

For example, to restore from a directory:

```bash
mongorestore /opt/backup/mongodump-1
```

This restores the backup to the `mongod` instance on `localhost:27017`.

#### Access Control

To restore data to a deployment that has [access control](https://www.mongodb.com/docs/manual/core/authorization.md) enabled, the [`restore`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-restore) role provides the necessary privileges *if* the data does not include [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection data and you run `mongorestore` without the `--oplogReplay` option.

You need additional privileges if the backup data includes [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection data or you run with [`--oplogReplay`:](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--oplogReplay)

| `system.profile` | If the backup data includes [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection data and the target database does not contain the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection, [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore) attempts to create the collection even though the program does not actually restore `system.profile` documents. The user requires additional privileges to perform [`createCollection`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-createCollection) and [`convertToCapped`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-convertToCapped) actions on the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection for a database. Both the built-in roles [`dbAdmin`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-dbAdmin) and [`dbAdminAnyDatabase`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-dbAdminAnyDatabase) provide the additional privileges. |
| --- | --- |
| `--oplogReplay` | To run with [`--oplogReplay`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--oplogReplay), create a [user-defined role](https://www.mongodb.com/docs/manual/tutorial/manage-users-and-roles.md#std-label-create-user-defined-role) that has [`anyAction`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-anyAction) on [`anyResource`.](https://www.mongodb.com/docs/manual/reference/resource-document.md#std-label-resource-anyresource) Grant only to users who must run [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore.md#mongodb-binary-bin.mongorestore) with [`--oplogReplay`.](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--oplogReplay) |

#### Specify Host and Port

By default, `mongorestore` connects to `localhost:27017`. To restore to a different host or port, use `--host` and `--port`:

```bash
mongorestore --host=mongodb1.example.net --port=3017
```

To authenticate, include [`--username`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--username) and [`--authenticationDatabase`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--authenticationDatabase). Omit [`--password`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--password) to have `mongorestore` prompt for the password:

```bash
mongorestore \
   --host=mongodb1.example.net \
   --port=3017 \
   --username=user \
   --authenticationDatabase=admin \
   /opt/backup/mongodump-1
```

#### Use an Oplog File to Restore Data

To capture writes that occur while `mongodump` is running, use `--oplog`. `mongodump` creates an `oplog.bson` file with oplog entries for each write during the run. Apply those operations with `--oplogReplay` when restoring.

For examples, see [mongodump Examples](https://www.mongodb.com/docs/database-tools/mongodump/mongodump-examples.md#std-label-mongodump-examples) and [mongorestore Examples.](https://www.mongodb.com/docs/database-tools/mongorestore/mongorestore-examples.md#std-label-mongorestore-examples)

`mongorestore --oplogReplay` restores all data from `oplog.bson` but does not support restoring to an arbitrary point in time. Use it to ensure the restored data reflects any writes that occurred during the `mongodump --oplog` run.

**Note:**

`--oplog` is intended for use with replica sets. For sharded clusters, including replica sets that are part of a sharded environment, see [Back Up a Self-Managed Sharded Cluster with a Database Dump.](https://www.mongodb.com/docs/manual/tutorial/backup-sharded-cluster-with-database-dumps.md#std-label-backup-sharded-dumps)

Use [`--drop`](https://www.mongodb.com/docs/database-tools/mongorestore.md#std-option-mongorestore.--drop) to drop each collection before restoring.
