- Administration >
- MongoDB备份方案
MongoDB备份方案¶
On this page
When deploying MongoDB in production, you should have a strategy for capturing and restoring backups in the case of data loss events.
为了从MongoDB管理服务的备份快照中恢复一个MongoDB集群,您可以下载一个MongoDB数据文件的压缩版本,然后再重启 mongod 进程之前分发这些文件。¶
通过拷贝基础的数据文件进行备份
您可以通过拷贝MongoDB基础的数据文件创建一个备份。¶
如果MongoDB存储数据文件的容量支持基于时间点的快照,您可以在某一特定时刻及时通过使用这些快照以创建一个MongoDB系统的备份。
文件系统快照是任何一个操作系统容量管理的特性,并不仅仅针对于MongoDB。快照的机制依赖于基础的存储系统。例如,如果您使用的是亚马逊的EBS存储,EC2的系统将会支持快照。此外,Linux和LVM管理器也可以创建快照。
为了得到一个正在运行的 mongod 进程的快照,您必须启用日志记录并且日志必须位于其它MongoDB数据文件的相同逻辑分区上。如果没有启用日志记录,就不能保证快照的一致性和可靠性。
Sharded cluster snapshots are difficult to achieve with other MongoDB backup methods.
为了得到一个分片系统一致的快照,您必须禁止均衡器,并且在差不多相同的时间上从每一个分片和配置服务器上获取一个快照。
Ops Manager¶
With Ops Manager, MongoDB subscribers can install and run the same core software that powers 您可以通过拷贝MongoDB基础的数据文件创建一个备份。 on their own infrastructure. Ops Manager is an on-premise solution that has similar functionality to MongoDB Cloud Manager and is available with Enterprise Advanced subscriptions.
为了了解更多的信息,您可以查阅 Back Up and Restore with Filesystem Snapshots 以及 Back Up a Sharded Cluster with File System Snapshots 来了解使用LVM创建快照完整的命令。此外,您还可以查阅:在Amazon EC2上对MongoDB进行备份和恢复操作 。
通过 mongodump 进行备份¶
如果在 mongodump 创建备份的同时,应用对数据进行修改,那么 mongodump 将会与这些应用竞争资源。¶
You can create a backup of a MongoDB deployment by making a copy of MongoDB’s underlying data files.
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. 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.
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.
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.
For more information, see the Back Up and Restore with Filesystem Snapshots and Back Up a Sharded Cluster with File System Snapshots for complete instructions on using LVM to create snapshots. Also see Back up and Restore Processes for MongoDB on Amazon EC2.
Back Up with cp or rsync¶
If 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.
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.
Back Up with mongodump¶
mongodump reads data from a MongoDB database and creates high fidelity BSON files which the mongorestore tool can use to populate a MongoDB database. 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 and mongorestore operate against a running mongod process, and can manipulate the underlying data files directly. By default, mongodump does not capture the contents of the local database.
Restore a Replica Set from MongoDB Backups
When connected to a MongoDB instance, 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.
Applications can continue to modify data while mongodump captures the output. For replica sets, mongodump provides the --oplog option to include in its output oplog entries that occur during the mongodump operation. This allows the corresponding mongorestore operation to replay the captured oplog. To restore a backup created with --oplog, use mongorestore with the --oplogReplay option.
备份分片集群以及单一分片的详细过程及考量。
从MongoDB数据文件中恢复那些没有正常关闭或者处在不稳定状态的数据。