本文說明如何使用 Azure CLI,將 PostgreSQL 資料庫還原到透過 Azure 備份支援的 Azure Database for PostgreSQL 伺服器。 您也可以使用 Azure 入口網站、 Azure PowerShell 和 REST API 來還原 PostgreSQL 資料庫。
由於 PostgreSQL 資料庫是平臺即服務 (PaaS) 資料庫,因此不支援取代現有資料庫來還原的 Original-Location Recovery (OLR) 選項(從其中進行備份)。 您可以從恢復點還原,以在相同的適用於 PostgreSQL 的 Azure 資料庫伺服器或任何其他 PostgreSQL 伺服器中建立新的資料庫。 此選項稱為替代位置復原 (ALR)。 ALR 有助於同時保留來源資料庫和復原後的新資料庫。
本文中的範例是指資源群組TestBkpVault下名為 testBkpVaultRG 的現有備份保存庫。
還原已備份的 PostgreSQL 資料庫
設定權限
備份保存庫會使用受控識別來存取其他 Azure 資源。 若要從備份還原,在資料庫應該還原至其中的適用於 PostgreSQL 的 Azure 資料庫伺服器上,備份保存庫的受控識別需要一組權限。
若要在目標 PostgreSQL 伺服器上指派保存庫系統指派受控識別的相關許可權,請參閱 備份 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-aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
"identity": null,
"kind": "",
"location": "",
"managedBy": "",
"name": "testpostgresql-empdb11-aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
"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-aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e -g testBkpVaultRG --vault-name TestBkpVault
{
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupInstances/testpostgresql-empdb11-aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/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"
}
如果您需要從封存層擷取恢復點, type 中的 recoveryPointDataStoreDetails 變數為 ArchiveStore。
準備還原要求
PostgreSQL 資料庫有各種還原選項。 您可以將復原點還原為另一個資料庫或還原為檔案。 恢復點也可以位於封存層。
還原為資料庫
使用 您指派許可權的目標 PostgreSQL 伺服器,建構新 PostgreSQL 資料庫的 Azure Resource Manager 標識符。 此外,也請建構必要的 PostgreSQL 資料庫名稱。 例如,一個 PostgreSQL 資料庫可以在具有不同訂用帳戶的資源群組emprestored21中的目標 PostgreSQL 伺服器targetossserver下命名為targetrg。
$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
若是以封存為基礎的復原點,您需要:
- 將封存資料存放區解除凍結至保存庫資料存放區。
- 修改來源資料存放區。
- 新增其他參數以指定解除凍結優先順序。
- 指定解除凍結復原點應該保留在保存庫資料存放區中的持續時間。
- 從這個復原點還原為資料庫。
使用下列命令,一次為所有先前提及的作業準備要求:
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。 下列範例會使用名為 testcontainerrestore 且具有不同訂用帳戶的記憶體帳戶底下名為 testossstorageaccount 的容器:
$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-aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e --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