MongoDB 使用文件系统快照方式备份和恢复 [译]

本文档主要阐述使用系统级别的工具来创建 MongoDB 备份的过程,例如 LVM 或者储存设备,以及相应的恢复策略。

这些文件系统快照,或者“块级”备份方法,使用系统级别的工具来创建保存 MongoDB 数据文件的设备备份。这些方法能够快速、可靠地完成工作,但是需要进行额外的系统配置。

3.2版本的变更:MongoDB 3.2 增加了使用 WiredTiger 储存引擎进行卷级别备份的支持,即使 MongoDB 实例的数据文件和日志文件驻留在单独的卷中。

3.1之前使用 WiredTiger 进行卷级别备份需要将数据文件和日志文件放在同一个卷中。

更多:MongoDB Backup MethodsBack Up and Restore with MongoDB Tools.

快照概述

快照通过在实时数据和指定快照卷中创建指针来工作。这些指针理论上相同于“硬链接”。随着工作数据偏离快照,快照使用写入时复制的策略来处理。因此,快照仅存储修改的数据。

生成快照之后,你可以在文件系统中装载快照镜像并复制数据。这种方式的备份包含了完整的数据副本。

注意事项

有效的数据库快照的时间

当执行快照备份时,数据库必须是有效的。就是说所有被数据库接收到的写操作需要完整写到磁盘中︰可以是日志或数据文件。

当备份发生时,如果这些写操作没有在磁盘中记录,备份将不会包含这些更改。

For the MMAPv1 storage engine, if writes are in progress when the backup occurs, the data files will reflect an inconsistent state. With journaling, all data file states resulting from in-progress writes are recoverable; without journaling, you must flush all pending writes to disk before running the back up operation and must ensure that no writes occur during the entire back up procedure. If you do use journaling, the journal must reside on the same volume as the data.

For the WiredTiger storage engine, the data files reflect a consistent state as of the last checkpoint. Checkpoints occur with every 2 GB of data or every minute.

整个磁盘镜像

Snapshots create an image of an entire disk image. Unless you need to back up your entire system, consider isolating your MongoDB data files, journal (if applicable), and configuration on one logical disk that doesn’t contain any other data.
Alternately, store all MongoDB data files on a dedicated device so that you can make backups without duplicating extraneous data.

站点故障预防措施

请确保从快照中把数据复制到了其他系统里。这可以确保故障站点的数据安全。

没有增量备份

本教程不包括增量备份的处理。虽然不同的快照方法提供了不同的功能,下面的 LVM 方法不提供任何用于捕获增量备份的功能。

带日志的快照

如果您的 mongod 实例已经启用日志功能,那么你可以使用任何类型的文件系统或卷/块级别的快照工具来创建备份。
如果你在 Linux-based 系统上管理自己的架构,配置您的系统使用 LVM 来提供磁盘包和快照功能。您还可以在云/虚拟化环境中使用 LVM-based 设置。

LVM 拥有更大的灵活性,并提供了使用快照备份 MongoDB 的可能性。

Snapshots with Amazon EBS in a RAID 10 Configuration

If your deployment depends on Amazon’s Elastic Block Storage (EBS) with RAID configured within your instance, it is impossible to get a consistent state across all disks using the platform’s snapshot tool. As an alternative, you can do one of the following:

  • Flush all writes to disk and create a write lock to ensure consistent state during the backup process.
    If you choose this option see Back up Instances with Journal Files on Separate Volume or without Journaling.
  • Configure LVM to run and hold your MongoDB data files on top of the RAID within your system.
    If you choose this option, perform the LVM backup operation described in Create a Snapshot.

在 Linux 上使用 LVM 备份和恢复

This section provides an overview of a simple backup process using LVM on a Linux system. While the tools, commands, and paths may be (slightly) different on your system the following steps provide a high level overview of the backup operation.

提示:

Only use the following procedure as a guideline for a backup system and infrastructure. Production backup systems must consider a number of application specific requirements and factors unique to specific environments.

创建快照

3.2版本的变更:从 MongoDB 3.2 开始,使用 WiredTiger 进行卷级别的备份,数据文件和日志文件不再需要在同一个卷中。

要使用 LVM 创建快照,使用 root 执行下面的命令︰

lvcreate --size 100M --snapshot --name mdb-snap01 /dev/vg0/mongodb

此命令创建了一个 LVM 快照(通过 --snapshot 选项),名称是 mdb-snap01,放在 vg0 组中的 mongodb 卷下。

实例创建了一个名为 mdb-snap01 的快照,位置在 /dev/vg0/mdb-snap01中。你系统卷组和设备的路径位置可能略有不同,这取决于你系统中 LVM 的配置。

快照的上限是100M,因为参数 --size 的值为 100M。此大小不能反映的磁盘上的数据总量,/dev/vg0/mongodb 的当前状态和创建的快照 (例如:/dev/vg0/mdb-snap01) 之间存在相当数量的差异。

警告:
请确保有足够的空间来应付数据的增长,尤其当需要从系统外部或者临时镜像中复制数据的时候。
如果快照耗尽了磁盘空间,快照图像会变得不可用。请放弃这个逻辑卷,并创建另一个。

命令返回时快照就已经成功存储了。你可以随时直接从快照还原,或者创建一个新的逻辑卷并从快照中还原到备份镜像。

虽然用快照来快速创建高质量的备份很实用,但作为一个格式化存储备份数据的方式并不是那么理想。快照通常依赖和驻留在相同的存储架构作为原始的磁盘映像。因此,你存档这些快照和把他们存储在其他地方是极其重要的。

快照存档

待译

恢复快照

待译

直接从快照恢复

待译

远程备份存储

待译

Back up Instances with Journal Files on Separate Volume or without Journaling

待译

Additional Resources

待译

你可能感兴趣的:(MongoDB 使用文件系统快照方式备份和恢复 [译])