This tutorial describes the process for creating backups and restoring data using the command-line utilities 本教程描述了使用MongoDB提供的命令行实用程序mongorestore and mongodump provided with MongoDB.mongorestore和mongodump创建备份和还原数据的过程。
To restore a backup of your self-hosted deployment to a managed MongoDB Atlas deployment, see Seed with mongorestore.要将自托管部署的备份还原到托管的MongoDB Atlas部署,请参阅Seed with mongorestore。
For a fully-managed backup method, use Cloud Backups in MongoDB Atlas, which provide localized backup storage using the native snapshot functionality of the cluster's cloud service provider.对于完全托管的备份方法,请使用MongoDB Atlas中的云备份,它使用集群云服务提供商的本地快照功能提供本地化的备份存储。
Considerations注意事项
Deployments部署
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 file system snapshots or block-level disk snapshots with Cloud Backups from MongoDB Atlas.对于弹性和无中断的备份,请使用文件系统快照或块级磁盘快照以及MongoDB Atlas的云备份。
Note
Back Up Sharded Clusters with MongoDB Atlas使用MongoDB Atlas备份分片集群
To use 要使用mongodump and mongorestore as a backup strategy for sharded clusters, see Back Up a Self-Managed Sharded Cluster with a Database Dump.mongodump和mongorestore作为分片集群的备份策略,请参阅使用数据库转储备份自管理分片集群。
Sharded clusters can also use one of the following coordinated backup and restore processes, which maintain the atomicity guarantees of transactions across shards:分片集群还可以使用以下协调的备份和恢复过程之一,以维护跨分片事务的原子性保证:
Performance Impacts性能影响
Because 因为mongodump and mongorestore operate by interacting with a running mongod instance, they can impact the performance of your running database. Not only do the tools create traffic for a running database instance, they also force the database to read all data through memory. mongodump和mongorestore是通过与正在运行的mongod实例交互来运行的,所以它们会影响正在运行的数据库的性能。这些工具不仅为正在运行的数据库实例创建流量,还强制数据库通过内存读取所有数据。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 Cloud Backups in MongoDB Atlas if the performance impact of如果mongodumpandmongorestoreis unacceptable for your use case.mongodump和mongorestore的性能影响对用例不可接受,请使用替代备份策略,如MongoDB Atlas中的文件系统快照或云备份。To ensure为了确保mongodumpcan take a consistent backup of a replica set, you must either use the--oplogoption to capture writes received during backup operations or stop all writes to the replica set for the duration of the backup.mongodump可以对副本集进行一致的备份,您必须使用--oplog选项来捕获备份操作期间收到的写入,或者在备份期间停止对副本集的所有写入。For sharded cluster replica sets, see Back Up a Self-Managed Sharded Cluster with a Database Dump.有关分片群集副本集,请参阅使用数据库转储备份自管理分片群集。Ensure that your backups are usable by restoring them to a test MongoDB deployment.通过将备份还原到测试MongoDB部署,确保备份可用。To help reduce the likelihood of inconsistencies in a sharded cluster backup, you must stop the balancer, stop all write operations, and stop any schema transformations for the duration of the backup.为了帮助降低分片群集备份中不一致的可能性,您必须停止平衡器,停止所有写入操作,并在备份期间停止任何架构转换。
Tip
Backup Methods for a Self-Managed Deployment and MongoDB Atlas Cloud Backups for more information on backing up MongoDB instances. Additionally, consider the following reference documentation for the MongoDB Database Tools:有关备份MongoDB实例的更多信息,请参阅自我管理部署的备份方法和MongoDB Atlas云备份。此外,请考虑以下MongoDB数据库工具的参考文档:
Output Format输出格式
mongorestore and mongodump can output data to an archive file, which is a single-file alternative to multiple BSON files. mongorestore和mongodump可以将数据输出到归档文件中,归档文件是多个BSON文件的单个文件替代方案。Archive files are special-purpose formats that support non-contiguous file writes. They enable concurrent backups from MongoDB, as well as restores to MongoDB. Using archive files optimizes disk I/O while backup and restore operations execute.存档文件是支持非连续文件写入的专用格式。它们支持从MongoDB进行并发备份,以及恢复到MongoDB。在执行备份和还原操作时,使用归档文件可以优化磁盘I/O。
You can also output archive files to the standard output (您还可以将存档文件输出到标准输出(stdout). Writing to the standard output allows for data migration over networks, reduced disk I/O footprint, and concurrency gains in both the MongoDB tools and your storage engine.stdout)。写入标准输出允许通过网络进行数据迁移,减少磁盘I/O占用,并在MongoDB工具和存储引擎中提高并发性。
For more information on archive files, see the 有关存档文件的更多信息,请参阅--archive option.--archive选项。
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使用mongodump备份数据库
mongodumpNote
Back Up Sharded Clusters with MongoDB Atlas使用MongoDB Atlas备份分片集群
To use 要使用mongodump and mongorestore as a backup strategy for sharded clusters, see Back Up a Self-Managed Sharded Cluster with a Database Dump.mongodump和mongorestore作为分片集群的备份策略,请参阅使用数据库转储备份自管理分片集群。
Sharded clusters can also use one of the following coordinated backup and restore processes, which maintain the atomicity guarantees of transactions across shards:分片集群还可以使用以下协调的备份和恢复过程之一,以维护跨分片事务的原子性保证:
Exclude local Database排除local数据库
local Databasemongodump excludes the content of the 在其输出中排除local database in its output.local数据库的内容。
Required Access所需访问权限
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,您必须拥有为每个要备份的数据库授予find操作的权限。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操作
mongodump OperationsThe 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
To specify the host and port of the MongoDB instance, you can either:要指定MongoDB实例的主机和端口,您可以:
Specify the hostname and port in the使用SRV或标准连接字符串在--uristring, using either an SRV or standard connection string:--uri字符串中指定主机名和端口:mongodump --uri="mongodb+srv://username:password@cluster0.example.mongodb.net" <additional_options>Specify the hostname and port in the在--hoststring:--host字符串中指定主机名和端口:mongodump --host="mongodb0.example.com:27017" <additional_options>Specify the hostname and port in the在--hostand--port:--host和--port中指定主机名和端口:mongodump --host="mongodb0.example.com" --port=27017 <additional_options>
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. For example:--db和--collection指定为mongodump的选项。例如:
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/文件夹),或者重命名文件夹或文件。
Create Backups Using Oplogs使用Oplogs创建备份
The --oplog option with mongodump collects the oplog entries and allows you to perform a backup on a live database. mongodump的--oplog选项集合oplog条目,并允许您在实时数据库上执行备份。If you later restore the database from the backup, the database will be the same as it was when the backup process completed.如果稍后从备份还原数据库,则数据库将与备份过程完成时相同。
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完成创建转储文件时对应的特定时刻的备份。
Create Backups from Non-Local mongod Instances从非本地mongod实例创建备份
mongod InstancesThe --host and --port options for mongodump allow you to connect to and backup from a remote host. Consider the following example:mongodump的--host和--port选项允许您连接到远程主机并从远程主机进行备份。考虑以下示例:
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还原数据库
mongorestoreNote
Back Up Sharded Clusters with MongoDB Atlas使用MongoDB Atlas备份分片集群
To use 要使用mongodump and mongorestore as a backup strategy for sharded clusters, see Back Up a Self-Managed Sharded Cluster with a Database Dump.mongodump和mongorestore作为分片集群的备份策略,请参阅使用数据库转储备份自管理分片集群。
Sharded clusters can also use one of the following coordinated backup and restore processes, which maintain the atomicity guarantees of transactions across shards:分片集群还可以使用以下协调的备份和恢复过程之一,以维护跨分片事务的原子性保证:
Access Control访问控制
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集合数据,并且您在没有--oplogReplay选项的情况下运行mongorestore,则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 |
|
Basic mongorestore Operations基本mongorestore操作
mongorestore OperationsThe 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
All MongoDB collections have UUIDs 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.默认情况下,所有MongoDB集合都有UUID。当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 --uri <connection string> <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实例中。
To capture writes that may occur while 要捕获mongodump is running, use mongodump --oplog. mongodump运行时可能发生的写入,请使用mongodump --oplog。mongodump creates an oplog.bson file with oplog entries for each write that occurred during the run. mongodump创建一个oplog.bson文件,其中包含运行期间发生的每次写入的oplog条目。You can apply the oplog operations with 您可以使用mongorestore --oplogReplay.mongorestore --oplogReplay应用oplog操作。
For examples, see mongodump Examples and mongorestore Examples.有关示例,请参阅mongodump示例和mongorestore示例。
All of the data from the oplog.bson file is restored.oplog.bson文件中的所有数据都将被还原。
mongorestore --oplogReplay doesn't allow you to restore data to an arbitrary point in time. 不允许您将数据还原到任意时间点。Use 使用mongorestore --oplogReplay to ensure the restored data is up to date with any writes that occurred during the mongodump --oplog run.mongorestore --oplogReplay确保还原的数据与mongodump-oplog运行期间发生的任何写入保持最新。
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.对于分片集群,包括作为分片环境一部分的副本集,请参阅使用数据库转储备份自管理分片集群。
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实例
mongod InstancesBy default, 默认情况下,mongorestore connects to a MongoDB instance running on the localhost interface and on the default port (27017). mongorestore连接到在本地主机接口和默认端口(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-1