次の方法で共有


Azure CLI を使用して PostgreSQL データベースを復元する

この記事では、Azure CLI を使用して、Azure Backup 経由でバックアップした Azure Database for PostgreSQL サーバーに PostgreSQL データベースを復元する方法について説明します。 Azure portalAzure PowerShellREST API を使用して PostgreSQL データベースを復元することもできます。

PostgreSQL データベースはサービスとしてのプラットフォーム (PaaS) データベースであるため、既存のデータベース (バックアップが作成された場所) を置き換えて復元する Original-Location Recovery (OLR) オプションはサポートされていません。 復旧ポイントから復元して、同じ Azure Database for PostgreSQL サーバーまたは他の PostgreSQL サーバーに新しいデータベースを作成できます。 このオプションは、Alternate-Location Recovery (ALR) と呼ばれます。 ALR は、ソース データベースと復元された (新しい) データベースの両方を保持するのに役立ちます。

この記事の例では、リソース グループ TestBkpVaultの下にある testBkpVaultRG という名前の既存の Backup コンテナーを参照します。

バックアップされた PostgreSQL データベースを復元する

アクセス許可の設定

Backup コンテナーでは、マネージド ID を使用して他の Azure リソースにアクセスします。 バックアップから復元するには、Backup コンテナーのマネージド ID には、データベースの復元先となる Azure Database for PostgreSQL サーバーに対する一連のアクセス許可が必要です。

ターゲット PostgreSQL サーバーでコンテナーのシステム割り当てマネージド ID に関連するアクセス許可を割り当てるには、 PostgreSQL データベースのバックアップに必要な一連のアクセス許可を参照してください。

回復ポイントをファイルとしてストレージ アカウントに復元するには、バックアップ コンテナーのシステム割り当てマネージド ID がターゲット ストレージ アカウントにアクセスできる必要があります。

適切な回復ポイントのフェッチ

コンテナー内のすべてのバックアップ インスタンスを一覧表示するには、 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"
}

アーカイブ層から復旧ポイントをフェッチする必要がある場合、typerecoveryPointDataStoreDetails変数はArchiveStore

復元要求を準備する

PostgreSQL データベースにはさまざまな復元オプションがあります。 回復ポイントを別のデータベースとして復元したり、ファイルとして復元したりすることができます。 復旧ポイントは、アーカイブ層に配置することもできます。

データベースとして復元する

アクセス許可を割り当てたターゲット PostgreSQL サーバーを使用して、新しい PostgreSQL データベースの Azure Resource Manager ID を構築します。 また、必要な PostgreSQL データベース名を作成します。 たとえば、PostgreSQL データベースは、別のサブスクリプションを持つリソース グループ emprestored21targetossserver という名前のターゲット PostgreSQL サーバーの下に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

アーカイブ ベースの復旧ポイントの場合は、次の手順を実行する必要があります。

  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 をフェッチします。 次の例では、別のサブスクリプションを持つ 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 コマンドを使用して、すべての Backup コンテナーに関連するジョブを取得します。

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