Database Manual / Self-Managed Deployments / Administration / Backup Methods

Back Up and Restore a Self-Managed Deployment with Filesystem Snapshots使用文件系统快照备份和恢复自我管理部署

This document describes a procedure for creating backups of MongoDB standalone servers and replica sets using system-level tools, such as LVM or storage appliance, as well as the corresponding restoration strategies. 本文档描述了使用LVM或存储设备等系统级工具创建MongoDB独立服务器和副本集备份的过程,以及相应的恢复策略。For information on sharded clusters, see Back Up a Self-Managed Sharded Cluster with File System Snapshots.有关分片群集的信息,请参阅使用文件系统快照备份自管理分片群集

These filesystem snapshots, or "block-level" backup methods, use system level tools to create copies of the device that holds MongoDB's data files. These methods complete quickly and work reliably, but require additional system configuration outside of MongoDB.这些文件系统快照或“块级”备份方法使用系统级工具创建保存MongoDB数据文件的设备的副本。这些方法完成得很快,工作可靠,但需要在MongoDB之外进行额外的系统配置。

Snapshots Overview快照概述

Snapshots work by creating pointers between the live data and a special snapshot volume. These pointers are theoretically equivalent to "hard links." As the working data diverges from the snapshot, the snapshot process uses a copy-on-write strategy. As a result, the snapshot only stores modified data.快照通过在实时数据和特殊快照卷之间创建游标来工作。这些游标在理论上相当于“硬链接”。当工作数据与快照分离时,快照过程使用写时复制策略。因此,快照仅存储修改后的数据。

After making the snapshot, you mount the snapshot image on your file system and copy data from the snapshot. The resulting backup contains a full copy of all data.制作快照后,您可以在文件系统上挂载快照映像,并从快照中复制数据。生成的备份包含所有数据的完整副本。

Considerations注意事项

WiredTiger Storage EngineWiredTiger存储引擎

MongoDB supports volume-level back up using the WiredTiger storage engine when the MongoDB instance's data files and journal files reside on separate volumes. 当MongoDB实例的数据文件和日志文件位于单独的卷上时,MongoDB支持使用WiredTiger存储引擎进行卷级备份。However, to create a coherent backup, the database must be locked and all writes to the database must be suspended during the backup process.但是,要创建一致的备份,必须锁定数据库,并且在备份过程中必须暂停对数据库的所有写入。

Encrypted Storage Engine (MongoDB Enterprise Only)加密存储引擎(仅限MongoDB企业版)

For encrypted storage engines that use AES256-GCM encryption mode, AES256-GCM requires that every process use a unique counter block value with the key.对于使用AES256-GCM加密模式的加密存储引擎AES256-GCM要求每个进程在键中使用唯一的计数器块值。

For encrypted storage engine configured with AES256-GCM cipher:对于配置了AES256-GCM密码的加密存储引擎

  • Restoring from Hot Backup从热备份恢复
    Starting in 4.2, if you restore from files taken via "hot" backup (i.e. the mongod is running), MongoDB can detect "dirty" keys on startup and automatically rollover the database key to avoid IV (Initialization Vector) reuse.从4.2开始,如果您从通过“热”备份(即mongod正在运行)获取的文件进行还原,MongoDB可以在启动时检测“脏”键,并自动滚动数据库键以避免IV(初始化向量)重用。
  • Restoring from Cold Backup从冷备份恢复

    However, if you restore from files taken via "cold" backup (i.e. the mongod is 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 --eseDatabaseKeyRollover. When started with the --eseDatabaseKeyRollover option, the mongod instance rolls over the database keys configured with AES256-GCM cipher and exits.从4.2开始,为了避免从冷文件系统快照恢复后重用键,MongoDB添加了一个新的命令行选项--eseDatabaseKeyRollover。当使用--eseDatabaseKeyRollover选项启动时,mongod实例会滚动使用AES256-GCM密码配置的数据库键并退出。

Valid Database at the Time of Snapshot快照时的有效数据库

The database must be valid when the snapshot takes place. This means that all writes accepted by the database need to be fully written to disk: either to the journal or to data files.快照发生时,数据库必须有效。这意味着数据库接受的所有写入都需要完全写入磁盘:要么写入日志,要么写入数据文件。

If there are writes that are not on disk when the backup occurs, the backup will not reflect these changes.如果备份时磁盘上没有写入内容,则备份将不会反映这些更改。

For the WiredTiger storage engine, the data files reflect a consistent state as of the last checkpoint. 对于WiredTiger存储引擎,数据文件反映了截至最后一个检查点的一致状态。Checkpoints occur with every 2 GB of data or every minute.检查点每2GB数据或每分钟出现一次。

Stale Data失效数据

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.备份提供数据库当前状态的快照。从备份还原时,还原的数据库不包括备份后所做的任何更改,这可能会导致数据丢失。

Entire Disk Image整个磁盘映像

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.快照创建整个磁盘映像的映像。除非您需要备份整个系统,否则请考虑将MongoDB数据文件、日志(如果适用)和配置隔离在一个不包含任何其他数据的逻辑磁盘上。

Alternately, store all MongoDB data files on a dedicated device so that you can make backups without duplicating extraneous data.或者,将所有MongoDB数据文件存储在专用设备上,这样您就可以在不复制无关数据的情况下进行备份。

Site Failure Precaution现场故障预防

Ensure that you copy data from snapshots onto other systems. This ensures that data is safe from site failures.确保将快照中的数据复制到其他系统上。这确保了数据在站点故障时是安全的。

No Incremental Backups无增量备份

This tutorial does not include procedures for incremental backups. Although different snapshot methods provide different features, the LVM method outlined below does not provide any capacity for capturing incremental backups.本教程不包括增量备份的过程。尽管不同的快照方法提供了不同的功能,但下面概述的LVM方法不提供任何捕获增量备份的能力。

Snapshots With Journaling带日志功能的快照

If your mongod instance has journaling enabled, then you can use any kind of file system or volume/block level snapshot tool to create backups.如果mongod实例启用了日志记录,那么您可以使用任何类型的文件系统或卷/块级快照工具来创建备份。

If you manage your own infrastructure on a Linux-based system, configure your system with LVM to provide your disk packages and provide snapshot capability. You can also use LVM-based setups within a cloud/virtualized environment.如果您在基于Linux的系统上管理自己的基础架构,请使用LVM配置系统,以提供磁盘包并提供快照功能。您还可以在云/虚拟化环境中使用基于LVM的设置。

Note

Running LVM provides additional flexibility and enables the possibility of using snapshots to back up MongoDB.运行LVM提供了额外的灵活性,并允许使用快照备份MongoDB。

Snapshots with Amazon EBS in a RAID 10 ConfigurationRAID 10配置中使用Amazon EBS的快照

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:如果部署依赖于在实例中配置了RAID的亚马逊弹性块存储(EBS),则不可能使用平台的快照工具在所有磁盘上获得一致的状态。作为替代方案,您可以执行以下操作之一:

Back Up and Restore Using LVM on Linux在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.本节概述了在Linux系统上使用LVM的简单备份过程。虽然系统上的工具、命令和路径可能(略有)不同,但以下步骤提供了备份操作的高级概述。

Note

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.仅将以下过程用作备份系统和基础架构的指南。生产备份系统必须考虑许多特定于应用程序的要求和特定环境特有的因素。

For information on sharded clusters, see Back Up a Self-Managed Sharded Cluster with File System Snapshots.有关分片群集的信息,请参阅使用文件系统快照备份自管理分片群集

Create a Snapshot创建快照

For the purpose of volume-level backup of MongoDB instances using WiredTiger, the data files and the journal are no longer required to reside on a single volume.为了使用WiredTiger对MongoDB实例进行卷级备份,数据文件和日志不再需要驻留在单个卷上。

To create a snapshot with LVM, issue a command as root in the following format:要使用LVM创建快照,请以root身份发出以下格式的命令:

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

This command creates an LVM snapshot (with the --snapshot option) named mdb-snap01 of the mongodb volume in the vg0 volume group.此命令创建vg0卷组中mongodb卷的名为mdb-snap01LVM快照(带有--snapshot选项)。

This example creates a snapshot named mdb-snap01 located at /dev/vg0/mdb-snap01. The location and paths to your systems volume groups and devices may vary slightly depending on your operating system's LVM configuration.此示例创建了一个名为mdb-snap01的快照,位于/dev/vg0/mdb-snap001。系统卷组和设备的位置和路径可能会因操作系统的LVM配置而略有不同。

The snapshot has a cap of at 100 megabytes, because of the parameter --size 100M. 由于参数--size 100M,快照的上限为100 MB。This size does not reflect the total amount of the data on the disk, but rather the quantity of differences between the current state of /dev/vg0/mongodb and the creation of the snapshot (i.e. /dev/vg0/mdb-snap01.)此大小并不反映磁盘上的数据总量,而是反映/dev/vg0/mongodb的当前状态与创建快照(即/dev/vgO/mdb-snap01)之间的差异量

Warning

Ensure that you create snapshots with enough space to account for data growth, particularly for the period of time that it takes to copy data out of the system or to a temporary image.确保创建的快照具有足够的空间来考虑数据增长,特别是将数据从系统中复制出来或复制到临时映像所需的时间。

If your snapshot runs out of space, the snapshot image becomes unusable. Discard this logical volume and create another.如果快照空间不足,快照映像将无法使用。丢弃此逻辑卷并创建另一个。

The snapshot will exist when the command returns. You can restore directly from the snapshot at any time or by creating a new logical volume and restoring from this snapshot to the alternate image.命令返回时,快照将存在。您可以随时直接从快照还原,也可以创建新的逻辑卷并从此快照还原到备用映像。

While snapshots are great for creating high quality backups quickly, they are not ideal as a format for storing backup data. Snapshots typically depend and reside on the same storage infrastructure as the original disk images. Therefore, it's crucial that you archive these snapshots and store them elsewhere.虽然快照非常适合快速创建高质量的备份,但它们并不是存储备份数据的理想格式。快照通常依赖于并驻留在与原始磁盘映像相同的存储基础架构上。因此,将这些快照存档并存储在其他地方至关重要。

Archive a Snapshot存档快照

After creating a snapshot, mount the snapshot and copy the data to separate storage. Your system might try to compress the backup images as you move them offline. Alternatively, take a block level copy of the snapshot image, such as with the following procedure:创建快照后,装载快照并将数据复制到单独的存储中。当您将备份映像脱机时,系统可能会尝试压缩它们。或者,拍摄快照映像的块级副本,例如使用以下过程:

umount /dev/vg0/mdb-snap01
dd if=/dev/vg0/mdb-snap01 | gzip > mdb-snap01.gz

The above command sequence does the following:上述命令序列执行以下操作:

  • Ensures that the /dev/vg0/mdb-snap01 device is not mounted. Never take a block level copy of a filesystem or filesystem snapshot that is mounted.确保未安装/dev/vg0/mdb-snap01设备。切勿对已挂载的文件系统或文件系统快照进行块级复制。
  • Performs a block level copy of the entire snapshot image using the dd command and compresses the result in a gzipped file in the current working directory.使用dd命令对整个快照映像执行块级复制,并将结果压缩到当前工作目录中的gzip压缩文件中。

    Warning

    This command will create a large gz file in your current working directory. Make sure that you run this command in a file system that has enough free space.此命令将在当前工作目录中创建一个大型gz文件。确保在有足够可用空间的文件系统中运行此命令。

Restore a Snapshot还原快照

To restore a snapshot created with LVM, issue the following sequence of commands:要还原使用LVM创建的快照,请发出以下命令序列:

lvcreate --size 1G --name mdb-new vg0
gzip -d -c mdb-snap01.gz | dd of=/dev/vg0/mdb-new
mount /dev/vg0/mdb-new /srv/mongodb

The above sequence does the following:上述顺序执行以下操作:

  • Creates a new logical volume named mdb-new, in the /dev/vg0 volume group. The path to the new device will be /dev/vg0/mdb-new./dev/vg0卷组中创建一个名为mdb-new的新逻辑卷。新设备的路径将是/dev/vg0/mdb-new

    Warning

    This volume will have a maximum size of 1 gigabyte. The original file system must have had a total size of 1 gigabyte or smaller, or else the restoration will fail.此卷的最大大小为1GB。原始文件系统的总大小必须为1GB或更小,否则还原将失败。

    Change 1G to your desired volume size.1G更改为所需的卷大小。

  • Uncompresses and unarchives the mdb-snap01.gz into the mdb-new disk image.解压缩mdb-snap01.gz并将其解压缩到mdb-new磁盘映像中。
  • Mounts the mdb-new disk image to the /srv/mongodb directory. Modify the mount point to correspond to your MongoDB data file location, or other location as needed.mdb-new磁盘映像装载到/srv/mongodb目录。根据需要修改挂载点,使其与MongoDB数据文件位置或其他位置相对应。

Note

The restored snapshot will have a stale mongod.lock file. 还原的快照将有一个过时的mongod.lock文件。If you do not remove this file from the snapshot, and MongoDB may assume that the stale lock file indicates an unclean shutdown. 如果不从快照中删除此文件,MongoDB可能会认为过时的锁文件表示不干净的关机。If you use db.fsyncLock() you will need to remove the mongod.lock file.如果使用db.fsyncLock(),则需要删除mongod.lock文件。

Restore Directly from a Snapshot直接从快照还原

To restore a backup without writing to a compressed gz file, use the following sequence of commands:要还原备份而不写入压缩的gz文件,请使用以下命令序列:

umount /dev/vg0/mdb-snap01
lvcreate --size 1G --name mdb-new vg0
dd if=/dev/vg0/mdb-snap01 of=/dev/vg0/mdb-new
mount /dev/vg0/mdb-new /srv/mongodb

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的更多信息,请参阅集合

Remote Backup Storage远程备份存储

You can implement off-system backups using the combined process and SSH.您可以使用组合过程和SSH实现系统外备份。

This sequence is identical to procedures explained above, except that it archives and compresses the backup on a remote system using SSH.此序列与上述过程相同,除了它使用SSH在远程系统上存档和压缩备份。

Consider the following procedure:考虑以下程序:

umount /dev/vg0/mdb-snap01
dd if=/dev/vg0/mdb-snap01 | ssh username@example.com gzip > /opt/backup/mdb-snap01.gz
lvcreate --size 1G --name mdb-new vg0
ssh username@example.com gzip -d -c /opt/backup/mdb-snap01.gz | dd of=/dev/vg0/mdb-new
mount /dev/vg0/mdb-new /srv/mongodb

Back up Instances with Journal Files on Separate Volume or without Journaling备份实例,将日志文件放在单独的卷上或不进行日志记录

For the purpose of volume-level backup of MongoDB instances using WiredTiger, the data files and the journal are no longer required to reside on a single volume. However, the database must be locked and all writes to the database must be suspended during the backup process to ensure the consistency of the backup.为了使用WiredTiger对MongoDB实例进行卷级备份,数据文件和日志不再需要驻留在单个卷上。但是,必须锁定数据库,并且在备份过程中必须暂停对数据库的所有写入,以确保备份的一致性。

If your mongod instance is either running without journaling or has the journal files on a separate volume, you must flush all writes to disk and lock the database to prevent writes during the backup process. 如果mongod实例在没有日志记录的情况下运行,或者日志文件位于单独的卷上,则必须刷新所有对磁盘的写入并锁定数据库,以防止在备份过程中写入。If you have a replica set configuration, then for your backup use a secondary which is not receiving reads (i.e. hidden member).如果您有一个副本集配置,那么对于备份,请使用不接收读取的secondary(即隐藏成员)。

1

Flush writes to disk and lock the database to prevent further writes.刷新磁盘写入并锁定数据库以防止进一步写入。

To flush writes to disk and to "lock" the database, issue the db.fsyncLock() method in mongosh:要刷新对磁盘的写入并“锁定”数据库,请在mongosh中发出db.fsyncLock()方法:

db.fsyncLock();
2

Perform the backup operation described in Create a Snapshot.执行创建快照中描述的备份操作。

3

After you create the snapshot, unlock the database.创建快照后,解锁数据库。

To unlock the database after you create the snapshot, use the following command in mongosh:要在创建快照后解锁数据库,请在mongosh中使用以下命令:

db.fsyncUnlock();