Docs HomeMongoDB Manual

Back Up and Restore with MongoDB Tools使用MongoDB工具备份和恢复

This tutorial describes the process for creating backups and restoring data using the utilities provided with MongoDB.本教程介绍了使用MongoDB提供的实用程序创建备份和恢复数据的过程。

Note

MongoDB Atlas

MongoDB Atlas, the official MongoDB cloud service, provides 2 fully-managed methods for backups:MongoDB Atlas是MongoDB的官方云服务,提供了两种完全管理的备份方法:

  1. Continuous Backups, which take incremental backups of data in your cluster, ensuring your backups are typically just a few seconds behind the operational system. 连续备份,对群集中的数据进行增量备份,确保您的备份通常只落后于操作系统几秒钟。Atlas continuous backups allow you to restore from stored snapshots or from a selected point in time within the last 24 hours. You can also query a continuous backup snapshot.Atlas连续备份允许您在过去24小时内从存储的快照或选定的时间点进行恢复。您还可以查询连续备份快照。
  2. Cloud Provider Snapshots, which provide localized backup storage using the native snapshot functionality of the cluster's cloud service provider.云提供程序快照,使用群集云服务提供商的本机快照功能提供本地化备份存储。

Considerations注意事项

Deployments部署

The mongodump and mongorestore utilities work with BSON data dumps, and are useful for creating backups of small deployments. mongodumpmongorestore实用程序可用于BSON数据转储,对于创建小型部署的备份非常有用。For resilient and non-disruptive backups, use a file system or block-level disk snapshot function, such as the methods described in the MongoDB Backup Methods document.对于弹性和无中断备份,请使用文件系统或块级磁盘快照功能,例如MongoDB备份方法文档中描述的方法。

Note

mongodump and mongorestore cannot be part of a backup strategy for 4.2+ sharded clusters that have sharded transactions in progress, as backups created with mongodump do not maintain the atomicity guarantees of transactions across shards.mongodumpmongorestore不能作为4.2+正在进行分片事务的分片集群的备份策略的一部分,因为使用mongodump创建的备份不能维护跨分片事务原子性的保证。

For 4.2+ sharded clusters with in-progress sharded transactions, use one of the following coordinated backup and restore processes which do maintain the atomicity guarantees of transactions across shards:对于具有正在进行的分片事务的4.2多个分片集群,请使用以下协调的备份和恢复过程之一,这些过程确实维护了跨分片的事务的原子性保证:

Performance Considerations性能注意事项

Because mongodump and mongorestore operate by interacting with a running mongod instance, they can impact the performance of your running database. 因为mongodumpmongorestore是通过与正在运行的mongod实例交互来操作的,所以它们可能会影响正在运行的数据库的性能。Not only do the tools create traffic for a running database instance, they also force the database to read all data through memory. When MongoDB reads infrequently used data, it can evict more frequently accessed data, causing a deterioration in performance for the database's regular workload.这些工具不仅为正在运行的数据库实例创建流量,还强制数据库通过内存读取所有数据。当MongoDB读取不常用的数据时,它可能会驱逐更频繁访问的数据,从而导致数据库常规工作负载的性能下降。

When backing up your data with MongoDB's tools, consider the following guidelines:使用MongoDB的工具备份数据时,请考虑以下准则:

  • Label files so that you can identify the contents of the backup as well as the point in time that the backup reflects.标记文件,以便您可以识别备份的内容以及备份反映的时间点。
  • Use an alternative backup strategy such as Filesystem Snapshots or MongoDB Cloud Manager if the performance impact of mongodump and mongorestore is unacceptable for your use case.如果mongodumpmongorestore的性能影响对于您的用例来说是不可接受的,请使用其他备份策略,如文件系统快照MongoDB Cloud Manager
  • Use --oplog to capture incoming write operations during the {{out_tool}} operation to ensure that the backups reflect a consistent data state.在{{out_tool}}操作期间,使用--oplog捕获传入的写入操作,以确保备份反映一致的数据状态。
  • Ensure that your backups are usable by restoring them to a test MongoDB deployment.通过将备份恢复到测试MongoDB部署来确保备份可用。
Tip

See also: 另请参阅:

MongoDB Backup Methods and MongoDB Cloud Manager Backup documentation for more information on backing up MongoDB instances. Additionally, consider the following reference documentation for the MongoDB Database Tools:有关备份MongoDB实例的更多信息,请参阅MongoDB备份方法MongoDB Cloud Manager备份文档。此外,请考虑以下MongoDB数据库工具的参考文档:

Binary BSON Dumps二进制BSON转储

The mongorestore and mongodump utilities work with BSON data dumps, and are useful for creating backups of small deployments. mongorestoremongodump实用程序可用于BSON数据转储,对于创建小型部署的备份非常有用。For resilient and non-disruptive backups, use a file system or block-level disk snapshot function, such as the methods described in the MongoDB Backup Methods document.对于弹性和无中断备份,请使用文件系统或块级磁盘快照功能,例如MongoDB备份方法文档中描述的方法。

Use these tools for backups if other backup methods, such as MongoDB Cloud Manager or file system snapshots are unavailable.如果其他备份方法(如MongoDB Cloud Manager文件系统快照)不可用,请使用这些工具进行备份。

Procedures过程

Back Up a Database with mongodump使用mongodump备份数据库

Note

mongodump and mongorestore cannot be part of a backup strategy for 4.2+ sharded clusters that have sharded transactions in progress, as backups created with mongodump do not maintain the atomicity guarantees of transactions across shards.mongodumpmongorestore不能作为4.2+正在进行分片事务的分片集群的备份策略的一部分,因为使用mongodump创建的备份不能维护跨分片事务原子性的保证。

For 4.2+ sharded clusters with in-progress sharded transactions, use one of the following coordinated backup and restore processes which do maintain the atomicity guarantees of transactions across shards:对于具有正在进行的分片事务的4.2多个分片集群,请使用以下协调的备份和恢复过程之一,这些过程确实维护了跨分片的事务的原子性保证:

Exclude local Database排除local数据库

mongodump excludes the content of the local database in its output.在其输出中排除本地数据库的内容。

Required Access所需访问权限

To run mongodump against a MongoDB deployment that has access control enabled, you must have privileges that grant find action for each database to back up. 要在启用了访问控制的MongoDB部署中运行mongodump,您必须拥有为每个要备份的数据库授予查找操作的权限。The built-in backup role provides the required privileges to perform backup of any and all databases.内置backup角色提供执行任何和所有数据库备份所需的权限。

The backup role provides additional privileges to back up the system.profile collection that exists when running with database profiling.backup角色提供了备份system.profile集合的额外权限,该集合在与数据库分析一起运行时存在。

Basic mongodump Operations基本mongodump操作

The mongodump utility backs up data by connecting to a running mongod.mongodump实用程序通过连接到正在运行的mongod来备份数据。

The utility can create a backup for an entire server, database or collection, or can use a query to backup just part of a collection.该实用工具可以为整个服务器、数据库或集合创建备份,也可以使用查询仅备份集合的一部分。

When you run mongodump without any arguments, the command connects to the MongoDB instance on the local system (e.g. localhost) on port 27017 and creates a database backup named dump/ in the current directory.当您在没有任何参数的情况下运行mongodump时,该命令会连接到本地系统(例如localhost)上端口27017上的MongoDB实例,并在当前目录中创建一个名为dump/的数据库备份。

To backup data from a mongod instance running on the same machine and on the default port of 27017, use the following command:要从运行在同一台机器上的默认端口27017上的mongod实例备份数据,请使用以下命令:

mongodump

You can also specify the --host and --port of the MongoDB instance that the mongodump should connect to. 您还可以指定mongodump应该连接到的MongoDB实例的--host--portFor example:例如:

mongodump --host=mongodb.example.net --port=27017

mongodump will write BSON files that hold a copy of data accessible via the mongod listening on port 27017 of the mongodb.example.net host. mongodump将编写BSON文件,其中包含可通过mongodb.example.net主机的27017端口上的mongod侦听访问的数据副本。See Create Backups from Non-Local mongod Instances for more information.有关详细信息,请参阅从非本地mongod实例创建备份

To specify a different output directory, you can use the --out or -o option:要指定不同的输出目录,可以使用--out-o选项

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

To limit the amount of data included in the database dump, you can specify --db and --collection as options to mongodump. 要限制数据库转储中包含的数据量,可以指定--db--collection作为mongodump的选项。For example:例如:

mongodump --collection=myCollection --db=test

This operation creates a dump of the collection named myCollection from the database test in a dump/ subdirectory of the current working directory.此操作从当前工作目录的dump/子目录中的数据库test中创建名为myCollection的集合的转储。

mongodump overwrites output files if they exist in the backup data folder. mongodump会覆盖备份数据文件夹中存在的输出文件。Before running the mongodump command multiple times, either ensure that you no longer need the files in the output folder (the default is the dump/ folder) or rename the folders or files.在多次运行mongodump命令之前,请确保不再需要输出文件夹(默认为dump/文件夹)中的文件,或者重命名文件夹或文件。

Create Backups Using Oplogs使用操作日志创建备份

The --oplog option with mongodump collects the oplog entries and allows you to perform a backup on a live database. If you later restore the database from the backup, the database will be the same as it was when the backup process completed.mongodump--oplog选项集合oplog条目,并允许您对活动数据库执行备份。如果以后从备份中恢复数据库,则数据库将与备份过程完成时相同。

With --oplog, mongodump copies all the data from the source database as well as all of the oplog entries from the beginning to the end of the backup procedure. 使用--oplogmongodump可以复制源数据库中的所有数据,以及备份过程开始到结束时的所有oplog条目。This operation, in conjunction with mongorestore --oplogReplay, allows you to restore a backup that reflects the specific moment in time that corresponds to when mongodump completed creating the dump file.此操作与mongorestore --oplogReplay结合使用,可以还原反映mongodump完成创建转储文件时对应的特定时刻的备份。

Create Backups from Non-Local mongod Instances从非本地mongod实例创建备份

The --host and --port options for mongodump allow you to connect to and backup from a remote host. mongodump--host--port选项允许您连接到远程主机并从远程主机进行备份。Consider the following example:考虑以下示例:

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

On any mongodump command you may, as above, specify username and password credentials to specify database authentication.如上所述,在任何mongodump命令上,您都可以指定用户名和密码凭据来指定数据库身份验证。

Restore a Database with mongorestore使用mongorestore恢复数据库

Note

mongodump and mongorestore cannot be part of a backup strategy for 4.2+ sharded clusters that have sharded transactions in progress, as backups created with mongodump do not maintain the atomicity guarantees of transactions across shards.mongodumpmongorestore不能作为4.2+正在进行分片事务的分片集群的备份策略的一部分,因为使用mongodump创建的备份不能维护跨分片事务原子性的保证。

For 4.2+ sharded clusters with in-progress sharded transactions, use one of the following coordinated backup and restore processes which do maintain the atomicity guarantees of transactions across shards:对于具有正在进行的分片事务的4.2多个分片集群,请使用以下协调的备份和恢复过程之一,这些过程确实维护了跨分片的事务的原子性保证:

Access Control访问控制

To restore data to a MongoDB deployment that has access control enabled, the restore role provides the necessary privileges to restore data from backups if the data does not include system.profile collection data and you run mongorestore without the --oplogReplay option.要将数据恢复到启用了访问控制的MongoDB部署,如果数据不包括system.profile集合数据,并且您在不使用--oplogReplay选项的情况下运行restore,则恢复角色提供从备份中恢复数据的必要权限。

If the backup data includes system.profile collection data or you run with --oplogReplay, you need additional privileges:如果备份数据包括system.profile集合数据,或者使用--oplogReplay运行,则需要其他权限:

system.profileIf the backup data includes system.profile collection data and the target database does not contain the system.profile collection, mongorestore attempts to create the collection even though the program does not actually restore system.profile documents. 如果备份数据包括system.profile集合数据,而目标数据库不包含system.profile集合,则即使程序实际上没有还原system.profile文档,mongorestore也会尝试创建该集合。As such, the user requires additional privileges to perform createCollection and convertToCapped actions on the system.profile collection for a database.因此,用户需要额外的权限才能对数据库的系统配置文件集合执行createCollectionconvertToCapped操作。
Both the built-in roles dbAdmin and dbAdminAnyDatabase provide the additional privileges. 内置角色dbAdmindbAdminAnyDatabase都提供了额外的权限。
--oplogReplayTo run with --oplogReplay, create a user-defined role that has anyAction on anyResource.要使用--oplogReplay运行,请在anyResource上创建一个具有anyAction用户定义角色
Grant only to users who must run mongorestore with --oplogReplay. 仅授予必须使用--oplogReplay运行mongorestore的用户。

Basic mongorestore Operations基本的mongorestore操作

The mongorestore utility restores a binary backup created by mongodump. mongorestore实用程序恢复mongodump创建的二进制备份。By default, mongorestore looks for a database backup in the dump/ directory.默认情况下,mongorestoredump/目录中查找数据库备份。

The mongorestore utility restores data by connecting to a running mongod directly.mongorestore实用程序通过直接连接到正在运行的mongod来恢复数据。

mongorestore can restore either an entire database backup or a subset of the backup.可以恢复整个数据库备份或备份的一个子集。

Note

New in version 3.6:3.6版中的新增功能:

All MongoDB collections have UUIDs by default. 默认情况下,所有MongoDB集合都有UUIDWhen 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.当MongoDB恢复集合时,恢复的集合将保留其原始UUID。当恢复一个没有UUID的集合时,MongoDB会为恢复的集合生成一个UUID。

For more information on collection UUIDs, see Collections.有关集合UUID的详细信息,请参阅集合

To use mongorestore to connect to an active mongod, use a command with the following prototype form:要使用mongorestore连接到活动的mongod,请使用具有以下原型形式的命令:

mongorestore --port=<port number> <path to the backup>

Consider the following example:考虑以下示例:

mongorestore /opt/backup/mongodump-1

Here, mongorestore imports the database backup in the /opt/backup/mongodump-1 directory to the mongod instance running on the localhost interface on the default port 27017.这里,mongorestore/opt/backup/mongodump-1目录中的数据库备份导入到默认端口27017上本地主机接口上运行的mongod实例。

Restore Point in Time Oplog Backup恢复时间点操作日志备份

If you created your database dump using the --oplog option to ensure a point-in-time snapshot, call mongorestore with the --oplogReplay option, as in the following example:如果使用--oplog选项创建数据库转储以确保时间点快照,请使用--oplogReplay选项调用mongorestore,如下例所示:

mongorestore --oplogReplay

You may also consider using the mongorestore --objcheck option to check the integrity of objects while inserting them into the database, or you may consider the mongorestore --drop option to drop each collection from the database before restoring from backups.您也可以考虑在将对象插入数据库时使用mongorestore --objcheck选项来检查对象的完整性,也可以考虑使用mongorestore --drop选项在从备份恢复之前从数据库中删除每个集合。

Restore Backups to Non-Local mongod Instances将备份恢复到非本地mongod实例

By default, mongorestore connects to a MongoDB instance running on the localhost interface and on the default port (27017). 默认情况下,mongorestore连接到在localhost接口和默认端口(27017)上运行的MongoDB实例。If you want to restore to a different host or port, use the --host and --port options.如果要恢复到其他主机或端口,请使用--host--port选项。

The following example that specifies the --host and --port options:以下示例指定--host--port选项:

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

If restoring to an instance that enforces access control, include the --username and the --authenticationDatabase as well. 如果还原到强制访问控制的实例,请同时包含--username--authenticationDatabaseOmit the --password option to have mongorestore prompt for the password:省略--password选项,让mongorestore提示输入密码:

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