使用 Azure CLI 还原 Azure Database for PostgreSQL - 灵活服务器

本文介绍如何使用 Azure CLI 还原 Azure Database for PostgreSQL 灵活服务器。

注释

PaaS 数据库不支持原始位置恢复(OLR)选项。 请改用 Alternate-Location 恢复(ALR)从恢复点还原,并在相同或另一个 Azure PostgreSQL 灵活服务器中创建新数据库,同时保留源数据库和还原的数据库。

让我们在示例中的资源组TestBkpVault下使用现有的备份保管库testBkpVaultRG

先决条件

在从 Azure Database for PostgreSQL 灵活服务器备份还原之前,请查看以下先决条件:

  • 确保拥有 还原作所需的权限
  • 确保还原的目标存储帐户的 AllowCrossTenantReplication 属性设置为 true

注释

备份数据作为 Microsoft 租户内的 Blob 存储在备份保管库中。 在还原操作期间,备份数据将跨租户从一个存储帐户复制到另一个存储帐户。

设置还原权限

备份保管库使用托管标识来访问其他 Azure 资源。 若要从备份还原,备份保管库的托管标识需要在将数据库还原到的 Azure PostgreSQL - 灵活服务器上拥有一组特定权限。

若要为目标 PostgreSQL 灵活服务器上的保管库系统分配的托管标识分配相关权限,请检查备份 Azure PostgreSQL 灵活服务器数据库所需的 权限集

若要将恢复点作为文件恢复到存储帐户,备份保管库的系统分配托管身份需要访问目标存储帐户

提取相关恢复点

若要列出保管库中的所有备份实例,请使用 az dataprotection backup-instance list 命令。 然后使用命令提取相关实例 az dataprotection backup-instance show 。 或者,对于大规模场景,可以使用 az dataprotection backup-instance list-from-resourcegraph 命令列出各个保管库和订阅中的备份实例。

az dataprotection backup-instance list-from-resourcegraph --datasource-type AzureDatabaseForPostgreSQLFlexibleServer -subscriptions "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e"

{
    "datasourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/testpostgresql/databases/empdb11",
    "extendedLocation": null,
    "id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupInstances/testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149",
    "identity": null,
    "kind": "",
    "location": "",
    "managedBy": "",
    "name": "testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149",
    "plan": null,
    "properties": {
      "currentProtectionState": "ProtectionConfigured",
      "dataSourceInfo": {
        "baseUri": null,
        "datasourceType": "Microsoft.DBforPostgreSQL/flexibleServers/databases",
        "objectType": "Datasource",
        "resourceID": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/testpostgresql/databases/empdb11",
        "resourceLocation": "chinanorth",
        "resourceName": "postgres",
        "resourceProperties": null,
        "resourceType": "Microsoft.DBforPostgreSQL/flexibleServers/databases",
        "resourceUri": ""
      },
      "dataSourceProperties": null,
      "dataSourceSetInfo": {
        "baseUri": null,
        "datasourceType": "Microsoft.DBforPostgreSQL/flexibleServers/databases",
        "objectType": "DatasourceSet",
        "resourceID": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/testpostgresql",
        "resourceLocation": "chinanorth",
        "resourceName": "testpostgresql",
        "resourceProperties": null,
        "resourceType": "Microsoft.DBforPostgreSQL/flexibleServers",
        "resourceUri": ""
      },
      "datasourceAuthCredentials": {
        "objectType": "SecretStoreBasedAuthCredentials",
        "secretStoreResource": {
          "secretStoreType": "AzureKeyVault",
          "uri": "https://vikottur-test.vault.azure.cn/secrets/dbauth3",
          "value": null
        }
      },
      "friendlyName": "testpostgresql\\empdb11",
      "objectType": "BackupInstance",
      "policyInfo": {
        "policyId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupPolicies/osspol3",
        "policyParameters": null,
        "policyVersion": ""
      },
      "protectionErrorDetails": null,
      "protectionStatus": {
        "errorDetails": null,
        "status": "ProtectionConfigured"
      },
      "provisioningState": "Succeeded",
      "validationType": null
    },
    "protectionState": "ProtectionConfigured",
    "resourceGroup": "testBkpVaultRG",
    "sku": null,
    "subscriptionId": "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
    "tags": null,
    "tenantId": "aaaabbbb-0000-cccc-1111-dddd2222eeee",
    "type": "microsoft.dataprotection/backupvaults/backupinstances",
    "vaultName": "testBkpVault",
    "zones": null
  }
.
.

标识实例后,使用 az dataprotection recovery-point list 命令提取相关的恢复点。

az dataprotection recovery-point list --backup-instance-name testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149 -g testBkpVaultRG --vault-name TestBkpVault

{
  "id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupInstances/testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149/recoveryPoints/9da55e757af94261afa009b43cd3222a",
  "name": "9da55e757af94261afa009b43cd3222a",
  "properties": {
    "friendlyName": "2031fdb43a914114b6ce644eb6fcb5ce",
    "objectType": "AzureBackupDiscreteRecoveryPoint",
    "policyName": "oss-clitest-policy",
    "policyVersion": null,
    "recoveryPointDataStoresDetails": [
      {
        "creationTime": "2021-09-13T15:17:41.209845+00:00",
        "expiryTime": null,
        "id": "beddea84-7b30-42a5-a752-7c75baf96a52",
        "metaData": "{\"objectType\":\"PostgresBackupMetadata\",\"version\":\"1.0\",\"postgresVersion\":\"11\",\"dbName\":\"postgres\",\"serverName\":\"testpostgresql\",\"serverFQDN\":\"testpostgresql.postgres.database.chinacloudapi.cn\",\"usernameUsed\":\"backupadmin@testpostgresql\",\"backupToolPath\":\"postgresql-11.6-1\\\\bin\\\\pg_dump.exe\",\"backupType\":\"Full\",\"backupDumpFormat\":\"CUSTOM\",\"backupToolArgsFormat\":\"--no-acl --no-owner --serializable-deferrable --no-tablespaces --quote-all-identifiers -Fc -d postgres://{0}:{1}@{2}:5432/{3}?sslmode=verify-full&sslrootcert=E:\\\\approot\\\\Plugins\\\\Postgres\\\\..\\\\..\\\\postgres-root.crt\",\"storageUnits\":{\"1\":\"DbBackupDumpData\"},\"streamNamesInFirstStorageUnit\":[\"dbbkpdmpdatastream-1631546260050\"],\"pitId\":\"2031fdb43a914114b6ce644eb6fcb5ce\",\"bytesTransferred\":2063,\"dataSourceSize\":8442527,\"backupToolVersion\":\"11\"}",
        "rehydrationExpiryTime": null,
        "rehydrationStatus": null,
        "state": "COMMITTED",
        "type": "VaultStore",
        "visible": true
      }
    ],
    "recoveryPointId": "9da55e757af94261afa009b43cd3222a",
    "recoveryPointTime": "2021-09-13T15:17:41.209845+00:00",
    "recoveryPointType": "Full",
    "retentionTagName": "default",
    "retentionTagVersion": "637671427933449525"
  },
  "resourceGroup": "testBkpVaultRG",
  "systemData": null,
  "type": "Microsoft.DataProtection/backupVaults/backupInstances/recoveryPoints"
}

准备还原请求

只能将 PostgreSQL 灵活服务器数据库的恢复点还原为文件。

还原为文件

提取容器的 统一资源标识符(URI),该标识符 位于向其分配权限的存储帐户中。 例如,具有不同订阅的存储帐户 testcontainerrestore 下名为 testossstorageaccount 的容器。

$contURI = "https://testossstorageaccount.blob.core.chinacloudapi.cn/testcontainerrestore"

az dataprotection backup-instance restore initialize-for-data-recovery-as-files使用命令准备包含所有相关详细信息的还原请求。

az dataprotection backup-instance restore initialize-for-data-recovery-as-files --datasource-type AzureDatabaseForPostgreSQLFlexibleServer  --restore-location {location} --source-datastore VaultStore -target-blob-container-url $contURI --target-file-name "empdb11_postgresql-chinanorth_1628853549768" --recovery-point-id 9da55e757af94261afa009b43cd3222a > OssRestoreAsFilesReq.JSON

注释

在还原到目标存储帐户完成后,可以使用 pg_restore 实用工具从目标还原 Azure Database for PostgreSQL 的灵活服务器数据库。

若要连接到现有的 PostgreSQL 灵活服务器和现有数据库,请使用以下命令:

pg_restore -h <hostname> -U <username> -d <db name> -Fd -j <NUM> -C <dump directory>

在此脚本中:

  • Fd:目录格式。
  • -j:作业数。
  • -C:使用命令启动输出以创建数据库本身,然后重新连接到该输出。

以下示例演示如何显示语法:

pg_restore -h <hostname> -U <username> -j <Num of parallel jobs> -Fd -C -d <databasename> sampledb_dir_format

如果你有多个数据库要还原,请为每个数据库重新运行前面的命令。 此外,通过使用多个并发作业 -j,可以减少 多 vCore 目标服务器上的大型数据库的还原时间。 作业数可以等于或小于为目标服务器分配的 vCPUs 作业数。

触发还原

若要使用准备好的请求触发还原操作,请使用 az dataprotection backup-instance restore trigger 命令。

az dataprotection backup-instance restore trigger -g testBkpVaultRG --vault-name TestBkpVault --backup-instance-name testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149 --restore-request-object OssRestoreReq.JSON

跟踪作业

使用 az dataprotection job list 命令跟踪所有作业。 你可以列出所有作业并提取特定作业详细信息。

还可使用 Az.ResourceGraph 来跟踪所有备份保管库中的作业。 使用az dataprotection job list-from-resourcegraph命令来获取跨所有备份保管库的相关作业。

az dataprotection job list-from-resourcegraph --datasource-type AzureDatabaseForPostgreSQLFlexibleServer --operation Restore

后续步骤

排查 Azure Database for PostgreSQL 灵活服务器备份和还原操作中的常见错误