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

On this page本页内容

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. Atlas连续备份允许您在过去24小时内从存储的快照或选定的时间点进行恢复。You can also query a continuous backup snapshot.您还可以查询连续备份快照。
  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 mongorestorecannot 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.对于正在进行分片事务的4.2+分片集群,mongodumpmongorestore不能作为备份策略的一部分,因为使用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.使用--oplog{{out_tool}}操作期间捕获传入的写入操作,以确保备份反映一致的数据状态。
  • 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. 有关备份MongoDB实例的更多信息,请参阅MongoDB备份方法MongoDB Cloud Manager备份文档Additionally, consider the following reference documentation for the MongoDB Database Tools:此外,考虑以下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 Backup methods文档中描述的方法。

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 mongorestorecannot 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.对于正在进行分片事务的4.2+分片集群,mongodumpmongorestore不能作为备份策略的一部分,因为使用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.在输出中排除local数据库的内容。

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角色提供执行任何和所有数据库备份所需的权限。

Changed in version 3.2.1.在版本3.2.1中更改

The backup role provides additional privileges to back up the system.profile collection that exists when running with database profiling. backup角色提供了额外的权限,用于备份使用数据库分析运行时存在的system.profile集合。Previously, users required read access on this collection.以前,用户需要对此集合具有read权限。

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时,该命令将连接到端口27017上本地系统(例如localhost)上的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=/data/backup/

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. 覆盖备份数据文件夹中存在的输出文件。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/文件夹),或者重命名文件夹或文件。

Point in Time Operation Using Oplogs使用oplog的时间点操作

Use the --oplog option with mongodump to collect the oplog entries to build a point-in-time snapshot of a database within a replica set. 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-2013-10-24

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 mongorestorecannot 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.对于正在进行分片事务的4.2+分片集群,mongodumpmongorestore不能作为备份策略的一部分,因为使用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集合数据,并且您运行mongorestore时没有--oplogReplay选项,则restore角色将提供从备份还原数据所需的权限。

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

system.profile

If 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集合,则mongorestore会尝试创建集合,即使程序实际上并不还原system.profile文档。As such, the user requires additional privileges to perform createCollection and convertToCapped actions on the system.profile collection for a database.因此,用户需要额外的权限才能对数据库的system.profile集合执行createCollectionconvertToCapped操作。

Both the built-in roles dbAdmin and dbAdminAnyDatabase provide the additional privileges.内置角色dbAdmindbAdminAnyDatabase都提供了额外的权限。

--oplogReplay

To run with --oplogReplay, create a user-defined role that has anyAction on anyResource.

Grant only to users who must run mongorestore with --oplogReplay.仅授予必须使用--oplogReplay运行mongorestore的用户。

Basic mongorestore Operationsmongorestore的基本操作

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

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. MongoDB恢复集合时,恢复的集合将保留其原始UUID。When restoring a collection where no UUID was present, MongoDB generates a UUID for the restored collection.还原不存在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 dump-2013-10-25/

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

Restore Point in Time Oplog Backup恢复时间点Oplog备份

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-2013-10-24
←  Back Up and Restore with Filesystem SnapshotsRestore a Replica Set from MongoDB Backups →