When deploying MongoDB in production, you should have a strategy for capturing and restoring backups in the case of data loss events.在生产环境中部署MongoDB时,您应该有一个在数据丢失事件发生时捕获和恢复备份的策略。
This page covers backup methods for self-managed deployments.本页介绍自我管理部署的备份方法。
To learn more about Backup Methods for deployments hosted in MongoDB Atlas, see Back Up, Restore, and Archive Data.要了解有关MongoDB Atlas中托管部署的备份方法的更多信息,请参阅备份、还原和归档数据。
Back Up with MongoDB Cloud Manager or Ops Manager使用MongoDB云管理器或运维管理器进行备份
MongoDB Cloud Manager is a hosted back up, monitoring, and automation service for MongoDB. MongoDB Cloud Manager supports backing up and restoring MongoDB replica sets and sharded clusters from a graphical user interface.MongoDB Cloud Manager是MongoDB的托管备份、监控和自动化服务。MongoDB Cloud Manager支持从图形用户界面备份和恢复MongoDB副本集和分片集群。
MongoDB Cloud ManagerMongoDB云管理器
Important
To back up your replica sets and sharded clusters using MongoDB Cloud Manager, you must be using MongoDB Enterprise Server. For more information, see Install MongoDB Enterprise.要使用MongoDB云管理器备份副本集和分片集群,您必须使用MongoDB企业服务器。有关更多信息,请参阅安装MongoDB企业版。
The MongoDB Cloud Manager supports the backing up and restoring of MongoDB deployments.MongoDB云管理器支持备份和恢复MongoDB部署。
MongoDB Cloud Manager continually backs up MongoDB replica sets and sharded clusters by reading the oplog data from your MongoDB deployment. MongoDB云管理器通过从MongoDB部署中读取oplog数据,不断备份MongoDB副本集和分片集群。MongoDB Cloud Manager creates snapshots of your data at set intervals, and can also offer point-in-time recovery of MongoDB replica sets and sharded clusters.MongoDB云管理器以设定的时间间隔创建数据快照,还可以提供MongoDB副本集和分片集群的时间点恢复。
Tip
Sharded cluster snapshots are difficult to achieve with other MongoDB backup methods.使用其他MongoDB备份方法很难实现分片集群快照。
To get started with MongoDB Cloud Manager Backup, sign up for MongoDB Cloud Manager. 要开始使用MongoDB云管理器备份,请注册MongoDB云管理器。For documentation on MongoDB Cloud Manager, see the MongoDB Cloud Manager documentation.有关MongoDB云管理器的文档,请参阅MongoDB云管理器文档。
Ops Manager管理器
With Ops Manager, MongoDB subscribers can install and run the same core software that powers MongoDB Cloud Manager on their own infrastructure. Ops Manager is an on-premises solution that has similar functionality to MongoDB Cloud Manager and is available with Enterprise Advanced subscriptions.借助Ops管理器,MongoDB用户可以在自己的基础设施上安装和运行与MongoDB云管理器相同的核心软件。Ops管理器是一种本地解决方案,具有与MongoDB云管理器类似的功能,可用于企业高级订阅。
For more information about Ops Manager, see the MongoDB Enterprise Advanced page and the Ops Manager Manual.有关Ops Manager的更多信息,请参阅MongoDB企业高级页面和Ops管理器手册。
Back Up by Copying Underlying Data Files通过复制基础数据文件进行备份
Note
Considerations for Encrypted Storage Engines using AES256-GCM使用AES256-GCM的加密存储引擎的注意事项
For encrypted storage engines that use 对于使用AES256-GCM加密模式的加密存储引擎,AES256-GCM encryption mode, AES256-GCM requires that every process use a unique counter block value with the key.AES256-GCM要求每个进程在键中使用唯一的计数器块值。
For encrypted storage engine configured with 对于配置了AES256-GCM密码的加密存储引擎:AES256-GCM cipher:
-
Restoring from Hot Backup从热备份恢复Starting in 4.2, if you restore from files taken via "hot" backup (i.e. the从4.2开始,如果您从通过“热”备份(即mongodis running), MongoDB can detect "dirty" keys on startup and automatically rollover the database key to avoid IV (Initialization Vector) reuse.mongod正在运行)获取的文件进行还原,MongoDB可以在启动时检测“脏”键,并自动滚动数据库键以避免IV(初始化向量)重用。
-
Restoring from Cold Backup从冷备份恢复However, if you restore from files taken via "cold" backup (i.e. the但是,如果您从通过“冷”备份获取的文件中还原(即mongodis not running), MongoDB cannot detect "dirty" keys on startup, and reuse of IV voids confidentiality and integrity guarantees.mongod未运行),MongoDB在启动时无法检测到“脏”键,重复使用IV会使机密性和完整性保证失效。Starting in 4.2, to avoid the reuse of the keys after restoring from a cold filesystem snapshot, MongoDB adds a new command-line option从4.2开始,为了避免从冷文件系统快照恢复后重用键,MongoDB添加了一个新的命令行选项--eseDatabaseKeyRollover.--eseDatabaseKeyRollover。When started with the当使用--eseDatabaseKeyRolloveroption, themongodinstance rolls over the database keys configured withAES256-GCMcipher and exits.--eseDatabaseKeyRollover选项启动时,mongod实例会滚动使用AES256-GCM密码配置的数据库键并退出。
In general, if using filesystem based backups for MongoDB Enterprise, use the "hot" backup feature, if possible.一般来说,如果对MongoDB企业版使用基于文件系统的备份,请尽可能使用“热”备份功能。
Back Up with Filesystem Snapshots使用文件系统快照进行备份
You can create a backup of a MongoDB deployment by making a copy of MongoDB's underlying data files.您可以通过复制MongoDB的底层数据文件来创建MongoDB部署的备份。
If the volume where MongoDB stores its data files supports point-in-time snapshots, you can use these snapshots to create backups of a MongoDB system at an exact moment in time. File system snapshots are an operating system volume manager feature, and are not specific to MongoDB. 如果MongoDB存储其数据文件的卷支持时间点快照,则可以使用这些快照在特定时刻创建MongoDB系统的备份。文件系统快照是操作系统卷管理器的一项功能,并不特定于MongoDB。With file system snapshots, the operating system takes a snapshot of the volume to use as a baseline for data backup. The mechanics of snapshots depend on the underlying storage system. For example, on Linux, the Logical Volume Manager (LVM) can create snapshots. Similarly, Amazon's EBS storage system for EC2 supports snapshots.使用文件系统快照,操作系统会拍摄卷的快照,作为数据备份的基线。快照的机制取决于底层存储系统。例如,在Linux上,逻辑卷管理器(LVM)可以创建快照。同样,亚马逊的EC2 EBS存储系统也支持快照。
To get a correct snapshot of a running 要获得正在运行的mongod process, you must have journaling enabled and the journal must reside on the same logical volume as the other MongoDB data files. Without journaling enabled, there is no guarantee that the snapshot will be consistent or valid.mongod进程的正确快照,您必须启用日志记录,并且日志必须与其他MongoDB数据文件位于同一逻辑卷上。如果不启用日志记录,则无法保证快照的一致性或有效性。
To get a consistent snapshot of a sharded cluster, you must disable the balancer and capture a snapshot from every shard as well as a config server at approximately the same moment in time. To backup sharded clusters, see Back Up a Self-Managed Sharded Cluster with a Database Dump.为了获得分片集群的一致快照,您必须禁用平衡器,并在大约相同的时间从每个分片和配置服务器捕获快照。要备份分片集群,请参阅使用数据库转储备份自管理分片集群。
For more information, see the Back Up and Restore a Self-Managed Deployment with Filesystem Snapshots and Back Up a Self-Managed Sharded Cluster with File System Snapshots for complete instructions on using LVM to create snapshots.有关更多信息,请参阅使用文件系统快照备份和还原自管理部署以及使用文件系统快照备份自管理分片群集,以获取有关使用LVM创建快照的完整说明。
Back Up with cp or rsync使用cp或rsync进行备份
cp or rsyncIf your storage system does not support snapshots, you can copy the files directly using 如果存储系统不支持快照,您可以使用cp, rsync, or a similar tool. Since copying multiple files is not an atomic operation, you must stop all writes to the mongod before copying the files. Otherwise, you will copy the files in an invalid state.cp、rsync或类似工具直接复制文件。由于复制多个文件不是原子操作,因此在复制文件之前,必须停止对mongod的所有写入。否则,您将以无效状态复制文件。
Backups produced by copying the underlying data do not support point in time recovery for replica sets and are difficult to manage for larger sharded clusters. 通过复制底层数据生成的备份不支持副本集的时间点恢复,并且对于较大的分片集群难以管理。Additionally, these backups are larger because they include the indexes and duplicate underlying storage padding and fragmentation. 此外,这些备份更大,因为它们包括索引和重复的底层存储填充和分片。相比之下,mongodump, by contrast, creates smaller backups.mongodump创建的备份较小。
Back Up with mongodump使用mongodump备份
mongodumpmongodump reads data from a MongoDB database and creates high fidelity BSON files which the 从MongoDB数据库读取数据,并创建高保真度的BSON文件,mongorestore tool can use to populate a MongoDB database. mongorestore工具可以使用这些文件填充MongoDB数据库。mongodump and mongorestore are simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems.mongodump和mongorestore是备份和恢复小型MongoDB部署的简单高效工具,但不适合捕获大型系统的备份。
mongodump and mongorestore operate against a running mongod process, and can manipulate the underlying data files directly. mongodump和mongorestore对正在运行的mongod进程进行操作,可以直接操纵底层数据文件。By default, 默认情况下,mongodump does not capture the contents of the local database.mongodump不会捕获本地数据库的内容。
mongodump only captures the documents in the database. The resulting backup is space efficient, but mongorestore or mongod must rebuild the indexes after restoring data.mongodump只捕获数据库中的文档。由此产生的备份是节省空间的,但mongorestore或mongod必须在恢复数据后重建索引。
When connected to a MongoDB instance, 当连接到MongoDB实例时,mongodump can adversely affect mongod performance. If your data is larger than system memory, the queries will push the working set out of memory, causing page faults.mongodump会对mongod性能产生不利影响。如果数据大于系统内存,查询将把工作集挤出内存,导致页面错误。
Applications can continue to modify data while 应用程序可以在mongodump captures the output. mongodump捕获输出的同时继续修改数据。For replica sets, 对于副本集,mongodump provides the --oplog option to include in its output oplog entries that occur during the mongodump operation. mongodump提供了--oplog选项,以在其输出中包含mongodump操作期间出现的oplog条目。This allows the corresponding 这允许相应的mongorestore operation to replay the captured oplog. mongorestore操作重放捕获的oplog。To restore a backup created with 要还原使用--oplog, use mongorestore with the --oplogReplay option.--oplog创建的备份,请使用带有--oplogReplay选项的mongorestore。
However, for replica sets, consider MongoDB Cloud Manager or Ops Manager.但是,对于副本集,可以考虑MongoDB云管理器或Ops管理器。
To backup sharded clusters, see Back Up a Self-Managed Sharded Cluster with a Database Dump.要备份分片集群,请参阅使用数据库转储备份自管理分片集群。
Note
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:分片集群还可以使用以下协调的备份和恢复过程之一,以维护跨分片事务的原子性保证:
See Back Up and Restore a Self-Managed Deployment with MongoDB Tools and Back Up a Self-Managed Sharded Cluster with a Database Dump for more information.有关更多信息,请参阅使用MongoDB工具备份和还原自管理部署,以及使用数据库转储备份自管理分片集群。
Compare Backup Methods比较备份方法
The following table compares backup methods for on-premises deployments. You can use it as guidance for developing your own data-protection approaches.下表比较了本地部署的备份方法。您可以将其作为开发自己的数据保护方法的指导。
mongodump/mongorestore | |||
|---|---|---|---|
| Yes | No | No | |
fsync or db.fsyncLock() to reduce inconsistenciesfsync或db.fsyncLock()来减少不一致 | fsync or db.fsyncLock() to reduce inconsistenciesfsync或db.fsyncLock()来减少不一致 | ||
| No | |||
| No | Yes |