On this page本页内容
This tutorial describes the process for creating backups and restoring data using the utilities provided with MongoDB.本教程介绍了使用MongoDB提供的实用程序创建备份和恢复数据的过程。
MongoDB Atlas, the official MongoDB cloud service, provides 2 fully-managed methods for backups:MongoDB Atlas是官方的MongoDB云服务,它提供了两种完全受管理的备份方法:
The mongodump and mongorestore utilities work with BSON data dumps, and are useful for creating backups of small deployments. mongodump和mongorestore实用程序与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备份方法文档中描述的方法。
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.mongodump和mongorestore不能作为备份策略的一部分,因为使用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+分片群集,请使用以下协调的备份和恢复过程之一,这些过程确实维护了跨分片事务的原子性保证:
Because 由于mongodump and mongorestore operate by interacting with a running mongod instance, they can impact the performance of your running database. mongodump和mongorestore通过与运行中的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工具备份数据时,请考虑以下准则:
mongodump and mongorestore is unacceptable for your use case.mongodump和mongorestore对性能的影响对于您的用例来说是不可接受的,请使用其他备份策略,如文件系统快照或MongoDB Cloud Manager。--oplog to capture incoming write operations during the {{out_tool}} operation to ensure that the backups reflect a consistent data state.--oplog在{{out_tool}}操作期间捕获传入的写入操作,以确保备份反映一致的数据状态。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数据库工具的参考文档:
The mongorestore and mongodump utilities work with BSON data dumps, and are useful for creating backups of small deployments. mongorestore和mongodump实用程序与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或文件系统快照)不可用,请使用这些工具进行备份。
mongodumpmongodump备份数据库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.mongodump和mongorestore不能作为备份策略的一部分,因为使用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+分片群集,请使用以下协调的备份和恢复过程之一,这些过程确实维护了跨分片事务的原子性保证:
local Databaselocal数据库mongodump excludes the content of the 在输出中排除local database in its output.local数据库的内容。
To run 要对启用了访问控制的MongoDB部署运行mongodump against a MongoDB deployment that has access control enabled, you must have privileges that grant find action for each database to back up. 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中更改。
backup role provides additional privileges to back up the system.profile collection that exists when running with database profiling. backup角色提供了额外的权限,用于备份使用数据库分析运行时存在的system.profile集合。read access on this collection.read权限。
mongodump Operationsmongodump操作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和--port。For 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/文件夹),或者重命名文件夹或文件。
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. --oplog,mongodump从备份过程的开始到结束复制源数据库中的所有数据以及所有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完成创建转储文件时的特定时刻。
mongod Instancesmongod实例创建备份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命令上,您可以如上所述,指定用户名和密码凭据以指定数据库身份验证。
mongorestoremongorestore恢复数据库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.mongodump和mongorestore不能作为备份策略的一部分,因为使用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+分片群集,请使用以下协调的备份和恢复过程之一,这些过程确实维护了跨分片事务的原子性保证:
To restore data to a MongoDB deployment that has access control enabled, the 要将数据还原到启用了访问控制的MongoDB部署,如果数据不包括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.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 | 
 
 | 
| --oplogReplay | To run with  
 | 
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.可以恢复整个数据库备份或备份的子集。
New in version 3.6:3.6版中新增:
All MongoDB collections have UUIDs by default. 默认情况下,所有MongoDB集合都有UUID。When 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.mongorestore将dump-2013-10-25目录中的数据库备份导入默认端口27017上本地主机接口上运行的mongod实例。
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选项在从备份恢复之前从数据库中删除每个集合。
mongod Instancesmongod实例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和--authenticationDatabase。Omit 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