Share via


使用 Azure CLI 來還原 Azure PostgreSQL 資料庫

本文說明如何將 Azure PostgreSQL 資料庫還原至由 Azure 備份所支援的 Azure PostgreSQL 伺服器。

作為 PaaS 資料庫,不支援透過取代現有資料庫 (取得備份的位置) 進行還原的 [原始位置復原 (OLR)] 選項。 您可以從復原點還原,在相同的 Azure PostgreSQL 伺服器或任何其他 PostgreSQL 伺服器中建立新的資料庫。 這稱為替代位置復原 (ALR),有助於同時保留來源資料庫和還原 (新的) 資料庫。

在本文中,您將學會如何:

  • 還原以建立新的 PostgreSQL 資料庫

  • 追蹤還原作業狀態

我們將會參考範例中,資源群組 testBkpVaultRG 下現有的備份保存庫 TestBkpVault

還原已備份的 PostgreSQL 資料庫

設定權限

備份保存庫使用受控識別來存取其他 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 AzureDatabaseForPostgreSQL -subscriptions "xxxxxxxx-xxxx-xxxx-xxxx"

  {
    "datasourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/servers/testpostgresql/databases/empdb11",
    "extendedLocation": null,
    "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/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/servers/databases",
        "objectType": "Datasource",
        "resourceID": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/servers/testpostgresql/databases/empdb11",
        "resourceLocation": "westus",
        "resourceName": "postgres",
        "resourceProperties": null,
        "resourceType": "Microsoft.DBforPostgreSQL/servers/databases",
        "resourceUri": ""
      },
      "dataSourceProperties": null,
      "dataSourceSetInfo": {
        "baseUri": null,
        "datasourceType": "Microsoft.DBforPostgreSQL/servers/databases",
        "objectType": "DatasourceSet",
        "resourceID": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/servers/testpostgresql",
        "resourceLocation": "westus",
        "resourceName": "testpostgresql",
        "resourceProperties": null,
        "resourceType": "Microsoft.DBforPostgreSQL/servers",
        "resourceUri": ""
      },
      "datasourceAuthCredentials": {
        "objectType": "SecretStoreBasedAuthCredentials",
        "secretStoreResource": {
          "secretStoreType": "AzureKeyVault",
          "uri": "https://vikottur-test.vault.azure.net/secrets/dbauth3",
          "value": null
        }
      },
      "friendlyName": "testpostgresql\\empdb11",
      "objectType": "BackupInstance",
      "policyInfo": {
        "policyId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/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": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "tags": null,
    "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "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/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/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.azure.com\",\"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"
}

如果您需要從封存層擷取復原點,則 recoveryPointDataStoreDetails 中的 type 變數會是 ArchiveStore

準備還原要求

PostgreSQL 資料庫有各種還原選項。 您可以將復原點還原為另一個資料庫或還原為檔案。 復原點也可以位於封存層。

還原為資料庫

建構新 PostgreSQL 資料庫的 Azure Resource Manager 識別碼 (ARM 識別碼)。 您必須使用作為權限指派目標的 PostgreSQL 伺服器來建立此項目。 此外,也請建構必要的 PostgreSQL 資料庫名稱。 例如,在具有不同訂用帳戶之資源群組 targetrg 中目標 PostgreSQL 伺服器 targetossserver 下,PostgreSQL 資料庫可以命名為 emprestored21

$targetOssId = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourceGroups/targetrg/providers/providers/Microsoft.DBforPostgreSQL/servers/targetossserver/databases/emprestored21"

使用 az dataprotection backup-instance restore initialize-for-data-recovery 命令,可準備具有所有相關詳細資料的還原要求。

az dataprotection backup-instance restore initialize-for-data-recovery --datasource-type AzureDatabaseForPostgreSQL  --restore-location {location} --source-datastore VaultStore --target-resource-id $targetOssId --recovery-point-id 9da55e757af94261afa009b43cd3222a --secret-store-type AzureKeyVault --secret-store-uri "https://restoreoss-test.vault.azure.net/secrets/dbauth3" > OssRestoreReq.JSON

若是以封存為基礎的復原點,您需要:

  1. 從封存資料存放區解除凍結至保存庫存放區
  2. 修改來源資料存放區
  3. 新增其他參數以指定解除凍結優先順序
  4. 指定解除凍結復原點應該保留在保存庫資料存放區中的持續時間
  5. 從這個復原點還原為資料庫

使用下列命令,一次為所有先前提及的作業準備要求。

az dataprotection backup-instance restore initialize-for-data-recovery --datasource-type AzureDatabaseForPostgreSQL  --restore-location {location} --source-datastore ArchiveStore --target-resource-id $targetOssId --recovery-point-id 9da55e757af94261afa009b43cd3222a --secret-store-type AzureKeyVault --secret-store-uri "https://restoreoss-test.vault.azure.net/secrets/dbauth3" --rehydration-priority Standard --rehydration-duration 12 > OssRestoreFromArchiveReq.JSON

還原為檔案

在作為權限指派目的地的儲存體帳戶內擷取容器的統一資源識別項 (URI)。 例如,使用不同的訂用帳戶,在儲存體帳戶 testossstorageaccount 下名為 testcontainerrestore 的容器。

$contURI = "https://testossstorageaccount.blob.core.windows.net/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 AzureDatabaseForPostgreSQL  --restore-location {location} --source-datastore VaultStore -target-blob-container-url $contURI --target-file-name "empdb11_postgresql-westus_1628853549768" --recovery-point-id 9da55e757af94261afa009b43cd3222a > OssRestoreAsFilesReq.JSON

針對以封存為基礎的復原點,請在下列指令碼中:

  • 修改來源資料存放區。
  • 新增解除凍結優先順序以及所解除凍結復原點的保留持續時間 (以天為單位)。
az dataprotection backup-instance restore initialize-for-data-recovery-as-files --datasource-type AzureDatabaseForPostgreSQL  --restore-location {location} --source-datastore ArchiveStore -target-blob-container-url $contURI --target-file-name "empdb11_postgresql-westus_1628853549768" --recovery-point-id 9da55e757af94261afa009b43cd3222a --rehydration-priority Standard --rehydration-duration 12 > OssRestoreAsFilesReq.JSON

若要驗證 JSON 檔案是否能成功建立新的資源,請使用 az dataprotection backup-instance validate-for-restore 命令。

觸發還原

使用 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 AzureDatabaseForPostgreSQL --operation Restore

下一步