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提供的实用程序创建备份和恢复数据的过程。
MongoDB Atlas
MongoDB Atlas, the official MongoDB cloud service, provides 2 fully-managed methods for backups:MongoDB Atlas是MongoDB的官方云服务,提供了两种完全管理的备份方法:
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小时内从存储的快照或选定的时间点进行恢复。您还可以查询连续备份快照。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. 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 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.mongodump
和mongorestore
不能作为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. 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的工具备份数据时,请考虑以下准则:
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
andmongorestore
is unacceptable for your use case.mongodump
和mongorestore
的性能影响对于您的用例来说是不可接受的,请使用其他备份策略,如文件系统快照或MongoDB Cloud Manager。Use在{{out_tool}}操作期间,使用--oplog
to capture incoming write operations during the {{out_tool}} operation to ensure that the backups reflect a consistent data state.--oplog
捕获传入的写入操作,以确保备份反映一致的数据状态。Ensure that your backups are usable by restoring them to a test MongoDB deployment.通过将备份恢复到测试MongoDB部署来确保备份可用。
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. 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备份方法文档中描述的方法。
Use these tools for backups if other backup methods, such as MongoDB Cloud Manager如果其他备份方法(如MongoDB Cloud Manager or file system snapshots are unavailable.
或文件系统快照)不可用,请使用这些工具进行备份。
Procedures过程
Back Up a Database with mongodump
使用mongodump
备份数据库
mongodump
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.mongodump
和mongorestore
不能作为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
数据库
local
Databasemongodump
excludes the content of the 在其输出中排除本地数据库的内容。local
database in its output.
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
,您必须拥有为每个要备份的数据库授予查找操作的权限。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
时,该命令会连接到本地系统(例如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
和--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=/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. --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. 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
恢复数据库
mongorestore
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.mongodump
和mongorestore
不能作为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 要将数据恢复到启用了访问控制的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
选项的情况下运行restore
,则恢复角色提供从备份中恢复数据的必要权限。
If the backup data includes 如果备份数据包括system.profile
collection data or you run with --oplogReplay
, you need additional privileges:system.profile
集合数据,或者使用--oplogReplay
运行,则需要其他权限:
system.profile | 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 也会尝试创建该集合。createCollection and convertToCapped actions on the system.profile collection for a database.createCollection 和convertToCapped 操作。dbAdmin and dbAdminAnyDatabase provide the additional privileges. dbAdmin 和dbAdminAnyDatabase 都提供了额外的权限。 |
--oplogReplay | --oplogReplay , create a user-defined role that has anyAction on anyResource .--oplogReplay 运行,请在anyResource 上创建一个具有anyAction 的用户定义角色。mongorestore with --oplogReplay . --oplogReplay 运行mongorestore 的用户。 |
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实用程序通过直接连接到正在运行的mongorestore
utility restores data by connecting to a running mongod
directly.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. 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
实例
mongod
InstancesBy 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-1