Azure Cosmos DB 中的联机备份和按需数据还原Online backup and on-demand data restore in Azure Cosmos DB

Azure Cosmos DB 会定期自动备份数据。Azure Cosmos DB automatically takes backups of your data at regular intervals. 自动备份不会影响数据库操作的性能或可用性。The automatic backups are taken without affecting the performance or availability of the database operations. 所有备份单独存储在一个存储服务中并进行多区域复制,以便针对区域性灾难进行复原。All the backups are stored separately in a storage service, and those backups are multiple-regionally replicated for resiliency against regional disasters. 如果意外删除或更新了 Azure Cosmos 帐户、数据库或容器,而稍后需要恢复数据,那么在这种情况下自动备份非常有用。The automatic backups are helpful in scenarios when you accidentally delete or update your Azure Cosmos account, database, or container and later require the data recovery.

自动联机备份Automatic and online backups

使用 Azure Cosmos DB,数据和数据备份都高度冗余,并且具有可复原性,能抵御区域性灾难。With Azure Cosmos DB, not only your data, but also the backups of your data are highly redundant and resilient to regional disasters. 以下步骤演示 Azure Cosmos DB 如何执行数据备份:The following steps show how Azure Cosmos DB performs data backup:

  • Azure Cosmos DB 每 4 小时自动备份数据库一次,在任何时间点都只存储最新的 2 个备份。Azure Cosmos DB automatically takes a backup of your database every 4 hours and at any point of time, only the latest 2 backups are stored. 不过,如果删除了容器或数据库,Azure Cosmos DB 会将给定容器或数据库中的现有快照保留 30 天。However, if the container or database is deleted, Azure Cosmos DB retains the existing snapshots of a given container or database for 30 days.

  • Azure Cosmos DB 将这些备份存储在 Azure Blob 存储中,而实际数据以本地形式驻留在 Azure Cosmos DB 中。Azure Cosmos DB stores these backups in Azure Blob storage whereas the actual data resides locally within Azure Cosmos DB.

  • 为了保证低延迟,备份的快照存储在 Azure Blob 存储中,且所在区域与 Azure Cosmos 数据库帐户的当前写入区域(如果有多主数据库配置,则为其中一个写入区域)相同。To guarantee low latency, the snapshot of your backup is stored in Azure Blob storage in the same region as the current write region (or one of the write regions, in case you have a multi-master configuration) of your Azure Cosmos database account. 对于针对区域性灾难的恢复,Azure Blob 存储中备份数据的屏幕快照将通过异地冗余存储 (GRS) 再次复制到另一区域。For resiliency against regional disaster, each snapshot of the backup data in Azure Blob storage is again replicated to another region through geo-redundant storage (GRS). 根据源区域以及与源区域关联的区域对确定备份复制的目标区域。The region to which the backup is replicated is based on your source region and the regional pair associated with the source region. 不能直接访问此数据库。You cannot access this backup directly. Azure Cosmos DB 仅在启动备份还原时使用此备份。Azure Cosmos DB will use this backup only if a backup restore is initiated.

  • 备份不会影响应用程序的性能或可用性。The backups are taken without affecting the performance or availability of your application. Azure Cosmos DB 在后台执行数据备份,不会消耗任何其他预配吞吐量 (RU),也不会影响数据库的性能和可用性。Azure Cosmos DB performs data backup in the background without consuming any additional provisioned throughput (RUs) or affecting the performance and availability of your database.

  • 如果意外删除或损坏了数据,则应在 8 小时内联系 Azure 支持,以便 Azure Cosmos DB 团队帮助你从备份中还原数据。If you have accidentally deleted or corrupted your data, you should contact Azure support within 8 hours so that the Azure Cosmos DB team can help you restore the data from the backups.

下图显示了如何在中国北部的远程 Azure Blob 存储帐户中备份 Azure Cosmos 容器(其三个主要物理分区全部位于中国北部),然后将其复制到中国东部:The following image shows how an Azure Cosmos container with all the three primary physical partitions in China North is backed up in a remote Azure Blob Storage account in China North and then replicated to China East:

GRS Azure 存储中所有 Cosmos DB 实体的定期完整备份

管理自己的备份的选项Options to manage your own backups

通过 Azure Cosmos DB SQL API 帐户,还可采用以下某一方法维护备份:With Azure Cosmos DB SQL API accounts, you can also maintain your own backups by using one of the following approaches:

  • 使用 Azure 数据工厂定期将数据移至所选的存储。Use Azure Data Factory to move data periodically to a storage of your choice.

  • 使用 Azure Cosmos DB 更改源定期读取数据来进行完整备份和增量更改,并将其存储在自己的存储中。Use Azure Cosmos DB change feed to read data periodically for full backups, as well as for incremental changes, and store it in your own storage.

备份保留期Backup retention period

Azure Cosmos DB 每四个小时拍摄一次数据快照。Azure Cosmos DB takes snapshots of your data every four hours. 在任何给定时间,只保留最后两个快照。At any given time, only the last two snapshots are retained. 不过,如果删除了容器或数据库,Azure Cosmos DB 会将给定容器或数据库中的现有快照保留 30 天。However, if the container or database is deleted, Azure Cosmos DB retains the existing snapshots of a given container or database for 30 days.

从联机备份还原数据Restoring data from online backups

在以下情况下可能会意外删除或修改数据:Accidental deletion or modification of data can happen in one of the following scenarios:

  • 删除了整个 Azure Cosmos 帐户The entire Azure Cosmos account is deleted

  • 删除了一个或多个 Azure Cosmos 数据库One or more Azure Cosmos databases are deleted

  • 删除了一个或多个 Azure Cosmos 容器One or more Azure Cosmos containers are deleted

  • 删除或修改了容器中的 Azure Cosmos 项(例如文档)。Azure Cosmos items (for example, documents) within a container are deleted or modified. 此特定情况通常称为"数据损坏"。This specific case is typically referred to as "data corruption".

  • 删除或损坏了共享产品数据库中的共享产品数据库或容器A shared offer database or containers within a shared offer database are deleted or corrupted

在上述所有情况中,Azure Cosmos DB 均可还原数据。Azure Cosmos DB can restore data in all the above scenarios. 还原进程始终会创建一个新的 Azure Cosmos 帐户来保存还原数据。The restore process always creates a new Azure Cosmos account to hold the restored data. 如果未指定新帐户的名称,则其名称将采用 <Azure_Cosmos_account_original_name>-restored1 格式。The name of the new account, if not specified, will have the format <Azure_Cosmos_account_original_name>-restored1. 如果尝试多次还原,则最后一位数将递增。The last digit is incremented, if multiple restores are attempted. 不能将数据还原到预先创建的 Azure Cosmos 帐户中。You can't restore data to a pre-created Azure Cosmos account.

删除某个 Azure Cosmos 帐户后,如果该帐户名未被使用,则可将数据还原到相同名称的帐户中。When an Azure Cosmos account is deleted, we can restore the data into an account with the same name, provided that the account name is not in use. 在这种情况下,建议不要在删除后重新创建帐户,因为这样不仅会阻止还原数据使用相同的名称,还会加大确定正确还原帐户的难度。In such cases, it's recommended to not re-create the account after deletion, because it not only prevents the restored data to use the same name, but also makes discovering the right account to restore from more difficult.

删除 Azure Cosmos 数据库后,可以还原该数据库中的整个数据库或容器子集。When an Azure Cosmos database is deleted, it is possible to restore the whole database or a subset of the containers within that database. 还可以跨数据库选择容器并还原它们,然后将所有还原的数据存放在新的 Azure Cosmos 帐户中。It is also possible to select containers across databases and restore them and all the restored data is placed in a new Azure Cosmos account.

如果意外删除或更改了容器中的一个或多个项(即数据损坏),需要指定还原到的时间。When one or more items within a container are accidentally deleted or changed (the data corruption case), you will need to specify the time to restore to. 对于这种情况,时间至关重要。Time is of essence for this case. 由于容器是实时的,所以备份仍在运行,因此如果超过了保持期(默认值为 8 小时),备份将被覆盖。Since the container is live, the backup is still running, so if you wait beyond the retention period (the default is eight hours) the backups would be overwritten. 如果数据已被删除,则不再存储数据,因为它们不会被备份周期覆盖。In the case of deletes, your data is no longer stored because they won't be overwritten by the backup cycle. 已删除数据库或容器的备份将保存 30 天。Backups for deleted databases or containers are saved for 30 days.

如果在数据库级别预配吞吐量(即,一组容器共享预配吞吐量),那么在这种情况下,将对整个数据库,而不是单个容器进行备份和还原。If you provision throughput at the database level (that is, where a set of containers shares the provisioned throughput), the backup and restore process in this case happen at the entire database level, and not at the individual containers level. 在这种情况下,不可选择还原容器子集。In such cases, selecting a subset of containers to restore is not an option.

将数据迁移到原始帐户Migrating data to the original account

数据还原的主要目标是提供一种方法来恢复意外删除或修改的任何数据。The primary goal of the data restore is to provide a way to recover any data that you delete or modify accidentally. 因此,建议先检查已还原数据,确保其中包含所需内容。So, we recommend that you first inspect the content of the recovered data to ensure it contains what you are expecting. 然后再将数据迁移回主帐户。Then work on migrating the data back to the primary account. 尽管可以使用已还原帐户作为实时帐户,但如果有生产工作负载则不建议这样做。Although it is possible to use the restored account as the live account, it's not a recommended option if you have production workloads.

可通过以下方式将数据迁移回原始的 Azure Cosmos 帐户:The following are different ways to migrate data back to the original Azure Cosmos account:

迁移完成后立即删除已还原帐户,因为它们会持续产生费用。Delete the restored accounts as soon as you are done migrating, because they will incur ongoing charges.

后续步骤Next steps

接下来可了解如何从 Azure Cosmos 帐户还原数据,或了解如何将数据迁移到 Azure Cosmos 帐户Next you can learn about how to restore data from an Azure Cosmos account or learn how to migrate data to an Azure Cosmos account