共用方式為


還原 適用於 PostgreSQL 的 Azure 資料庫 - 使用 Azure CLI 的彈性伺服器 (預覽)

本文說明如何將 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器還原至 適用於 PostgreSQL 的 Azure 資料庫 - Azure 備份 備份的彈性伺服器。

在這裡,我們會在範例中使用資源群組 testBkpVaultRG 下現有的備份保存庫 TestBkpVault

還原已備份的 PostgreSQL 資料庫

設定權限

備份保存庫使用受控識別來存取其他 Azure 資源。 若要從備份還原,備份保存庫的受控識別需要一組要還原伺服器之記憶體帳戶的許可權。 受控識別應該透過記憶體帳戶指派記憶體 Blob 數據參與者 角色。

擷取相關復原點

若要列出保存庫內的所有備份實例,請使用 az data protection backup-instance list 命令。 然後使用 az data protection backup-instance show 命令擷取相關的實例。 或者,針對大規模案例,您可以使用 az data protection backup-instance list-from-resourcegraph 命令,跨保存庫和訂用帳戶列出備份實例。

az dataprotection backup-instance list-from-resourcegraph --datasource-type AzureDatabaseForPostgreSQLFlexibleServer -subscriptions 00001111-aaaa-2222-bbbb-3333cccc4444

  {
    "datasourceId": "/subscriptions/00001111-aaaa-2222-bbbb-3333cccc4444/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/testpgflex",
    "extendedLocation": null,
    "id": "/subscriptions/00001111-aaaa-2222-bbbb-3333cccc4444/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupInstances/testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149",
    "identity": null,
    "kind": "",
    "location": "",
    "managedBy": "",
    "name": "testpgflex-957d23b1-c679-4c94-ade6-c4d34635e149",
    "plan": null,
    "properties": {
      "currentProtectionState": "ProtectionConfigured",
      "dataSourceInfo": {
        "baseUri": null,
        "datasourceType": "Microsoft.DBforPostgreSQL/flexibleServers",
        "objectType": "Datasource",
        "resourceID": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/testpgflex",
        "resourceLocation": "westus",
        "resourceName": "postgres",
        "resourceProperties": null,
        "resourceType": "Microsoft.DBforPostgreSQL/flexibleServers",
        "resourceUri": ""
      },
      "dataSourceProperties": null,
      "dataSourceSetInfo": {
        "baseUri": null,
        "datasourceType": "Microsoft.DBforPostgreSQL/flexibleServers",
        "objectType": "DatasourceSet",
        "resourceID": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/testpgflex",
        "resourceLocation": "westus",
        "resourceName": "testpgflex",
        "resourceProperties": null,
        "resourceType": "Microsoft.DBforPostgreSQL/flexibleServers",
        "resourceUri": ""
      },
      "friendlyName": "testpgflex",
      "objectType": "BackupInstance",
      "policyInfo": {
        "policyId": "/subscriptions/00001111-aaaa-2222-bbbb-3333cccc4444/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": bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f,
    "tags": null,
    "tenantId": aaaabbbb-0000-cccc-1111-dddd2222eeee,
    "type": "microsoft.dataprotection/backupvaults/backupinstances",
    "vaultName": "testBkpVault",
    "zones": null
  }
.
.
.
.
.

識別實例之後,請使用 az data protection recovery-point list 命令擷取相關的恢復點。

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

{
  "id": "/subscriptions/00001111-aaaa-2222-bbbb-3333cccc4444/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupInstances/testpgflex-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"
}

準備還原要求

您可以將 PostgreSQL – 彈性伺服器資料庫的恢復點還原為僅限檔案。

還原為檔案

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

$contURI = "https://testossstorageaccount.blob.core.windows.net/testcontainerrestore"

使用 az data protection 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 --recovery-point-id 9da55e757af94261afa009b43cd3222a > OssRestoreAsFilesReq.JSON

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

觸發還原

使用 az data protection backup-instance restore trigger 命令,透過先前準備的要求來觸發還原作業。

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

追蹤作業

使用 az data protection job list 命令來追蹤所有作業。 您可以列出所有作業,並擷取特定的作業詳細資料。

您也可以使用 Az.ResourceGraph 追蹤所有備份保存庫的作業。 使用 az data protection job list-from-resourcegraph 命令,取得跨所有備份保存庫的相關作業。

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

建立 PostgreSQL - 從還原的記憶體帳戶彈性伺服器

還原完成至目標記憶體帳戶之後,您可以使用 pg_restore 公用程式,從目標還原 適用於 PostgreSQL 的 Azure 資料庫 – 彈性伺服器資料庫。 使用下列命令連線到現有的 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 目標伺服器上還原大型資料庫所需的時間。 作業數目可以等於或小於為目標伺服器配置的 vCPU 數目。

下一步