|
| 1 | +--- |
| 2 | +title: Restore Azure PostgreSQL databases via Azure CLI |
| 3 | +description: Learn how to restore Azure PostgreSQL databases using Azure CLI. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 10/25/2021 |
| 6 | +author: v-amallick |
| 7 | +ms.service: backup |
| 8 | +ms.author: v-amallick |
| 9 | +--- |
| 10 | + |
| 11 | +# Restore Azure PostgreSQL databases using Azure CLI |
| 12 | + |
| 13 | +This article explains how to restore [Azure PostgreSQL databases](/azure/postgresql/overview#azure-database-for-postgresql---single-server) to an Azure PostgreSQL server backed-up by Azure Backup. |
| 14 | + |
| 15 | +Being a PaaS database, the Original-Location Recovery (OLR) option to restore by replacing the existing database (from where the backups were taken) isn't supported. You can restore from a recovery point to create a new database in the same Azure PostgreSQL server or in any other PostgreSQL server. This is called Alternate-Location Recovery (ALR) that helps to keep both - the source database and the restored (new) database. |
| 16 | + |
| 17 | +In this article, you'll learn how to: |
| 18 | + |
| 19 | +- Restore to create a new PostgreSQL database |
| 20 | + |
| 21 | +- Track the restore operation status |
| 22 | + |
| 23 | +We'll refer to an existing Backup vault _TestBkpVault_, under the resource group _testBkpVaultRG_ in the examples. |
| 24 | + |
| 25 | +## Restore a backed-up PostgreSQL database |
| 26 | + |
| 27 | +### Set up permissions |
| 28 | + |
| 29 | +Backup vault uses Managed Identity to access other Azure resources. To restore from backup, Backup vault’s managed identity requires a set of permissions on the Azure PostgreSQL server to which the database should be restored. |
| 30 | + |
| 31 | +To assign the relevant permissions for vault's system-assigned managed identity on the target PostgreSQL server, see the [set of permissions needed to backup Azure PostgreSQL database](/azure/backup/backup-azure-database-postgresql-overview#set-of-permissions-needed-for-azure-postgresql-database-restore). |
| 32 | + |
| 33 | +To restore the recovery point as files to a storage account, the [Backup vault's system-assigned managed identity needs access on the target storage account](/azure/backup/restore-azure-database-postgresql#restore-permissions-on-the-target-storage-account). |
| 34 | + |
| 35 | +### Fetch the relevant recovery point |
| 36 | + |
| 37 | +To list all backup instances within a vault, use [az dataprotection backup-instance list](/cli/azure/dataprotection/backup-instance?view=azure-cli-latest&preserve-view=true#az_dataprotection_backup_instance_list) command, and then fetch the relevant instance using the [az dataprotection backup-instance show](/cli/azure/dataprotection/backup-instance?view=azure-cli-latest&preserve-view=true#az_dataprotection_backup_instance_show) command. Alternatively, for _at-scale_ scenarios, you can list backup instances across vaults and subscriptions using the [az dataprotection backup-instance list-from-resourcegraph](/cli/azure/dataprotection/backup-instance?view=azure-cli-latest&preserve-view=true#az_dataprotection_backup_instance_list_from_resourcegraph) command. |
| 38 | + |
| 39 | +```azurecli |
| 40 | +az dataprotection backup-instance list-from-resourcegraph --datasource-type AzureDatabaseForPostgreSQL -subscriptions "xxxxxxxx-xxxx-xxxx-xxxx" |
| 41 | +
|
| 42 | + { |
| 43 | + "datasourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/servers/testpostgresql/databases/empdb11", |
| 44 | + "extendedLocation": null, |
| 45 | + "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupInstances/testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149", |
| 46 | + "identity": null, |
| 47 | + "kind": "", |
| 48 | + "location": "", |
| 49 | + "managedBy": "", |
| 50 | + "name": "testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149", |
| 51 | + "plan": null, |
| 52 | + "properties": { |
| 53 | + "currentProtectionState": "ProtectionConfigured", |
| 54 | + "dataSourceInfo": { |
| 55 | + "baseUri": null, |
| 56 | + "datasourceType": "Microsoft.DBforPostgreSQL/servers/databases", |
| 57 | + "objectType": "Datasource", |
| 58 | + "resourceID": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/servers/testpostgresql/databases/empdb11", |
| 59 | + "resourceLocation": "westus", |
| 60 | + "resourceName": "postgres", |
| 61 | + "resourceProperties": null, |
| 62 | + "resourceType": "Microsoft.DBforPostgreSQL/servers/databases", |
| 63 | + "resourceUri": "" |
| 64 | + }, |
| 65 | + "dataSourceProperties": null, |
| 66 | + "dataSourceSetInfo": { |
| 67 | + "baseUri": null, |
| 68 | + "datasourceType": "Microsoft.DBforPostgreSQL/servers/databases", |
| 69 | + "objectType": "DatasourceSet", |
| 70 | + "resourceID": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ossdemoRG/providers/Microsoft.DBforPostgreSQL/servers/testpostgresql", |
| 71 | + "resourceLocation": "westus", |
| 72 | + "resourceName": "testpostgresql", |
| 73 | + "resourceProperties": null, |
| 74 | + "resourceType": "Microsoft.DBforPostgreSQL/servers", |
| 75 | + "resourceUri": "" |
| 76 | + }, |
| 77 | + "datasourceAuthCredentials": { |
| 78 | + "objectType": "SecretStoreBasedAuthCredentials", |
| 79 | + "secretStoreResource": { |
| 80 | + "secretStoreType": "AzureKeyVault", |
| 81 | + "uri": "https://vikottur-test.vault.azure.net/secrets/dbauth3", |
| 82 | + "value": null |
| 83 | + } |
| 84 | + }, |
| 85 | + "friendlyName": "testpostgresql\\empdb11", |
| 86 | + "objectType": "BackupInstance", |
| 87 | + "policyInfo": { |
| 88 | + "policyId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupPolicies/osspol3", |
| 89 | + "policyParameters": null, |
| 90 | + "policyVersion": "" |
| 91 | + }, |
| 92 | + "protectionErrorDetails": null, |
| 93 | + "protectionStatus": { |
| 94 | + "errorDetails": null, |
| 95 | + "status": "ProtectionConfigured" |
| 96 | + }, |
| 97 | + "provisioningState": "Succeeded", |
| 98 | + "validationType": null |
| 99 | + }, |
| 100 | + "protectionState": "ProtectionConfigured", |
| 101 | + "resourceGroup": "testBkpVaultRG", |
| 102 | + "sku": null, |
| 103 | + "subscriptionId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 104 | + "tags": null, |
| 105 | + "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 106 | + "type": "microsoft.dataprotection/backupvaults/backupinstances", |
| 107 | + "vaultName": "testBkpVault", |
| 108 | + "zones": null |
| 109 | + } |
| 110 | +. |
| 111 | +. |
| 112 | +. |
| 113 | +. |
| 114 | +. |
| 115 | +``` |
| 116 | + |
| 117 | +Once the instance is identified, fetch the relevant recovery point using the [az dataprotection recovery-point list](/cli/azure/dataprotection/recovery-point?view=azure-cli-latest&preserve-view=true#az_dataprotection_recovery_point_list) command. |
| 118 | + |
| 119 | +```azurecli |
| 120 | +az dataprotection recovery-point list --backup-instance-name testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149 -g testBkpVaultRG --vault-name TestBkpVault |
| 121 | +
|
| 122 | +{ |
| 123 | + "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/testBkpVault/backupInstances/testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149/recoveryPoints/9da55e757af94261afa009b43cd3222a", |
| 124 | + "name": "9da55e757af94261afa009b43cd3222a", |
| 125 | + "properties": { |
| 126 | + "friendlyName": "2031fdb43a914114b6ce644eb6fcb5ce", |
| 127 | + "objectType": "AzureBackupDiscreteRecoveryPoint", |
| 128 | + "policyName": "oss-clitest-policy", |
| 129 | + "policyVersion": null, |
| 130 | + "recoveryPointDataStoresDetails": [ |
| 131 | + { |
| 132 | + "creationTime": "2021-09-13T15:17:41.209845+00:00", |
| 133 | + "expiryTime": null, |
| 134 | + "id": "beddea84-7b30-42a5-a752-7c75baf96a52", |
| 135 | + "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\"}", |
| 136 | + "rehydrationExpiryTime": null, |
| 137 | + "rehydrationStatus": null, |
| 138 | + "state": "COMMITTED", |
| 139 | + "type": "VaultStore", |
| 140 | + "visible": true |
| 141 | + } |
| 142 | + ], |
| 143 | + "recoveryPointId": "9da55e757af94261afa009b43cd3222a", |
| 144 | + "recoveryPointTime": "2021-09-13T15:17:41.209845+00:00", |
| 145 | + "recoveryPointType": "Full", |
| 146 | + "retentionTagName": "default", |
| 147 | + "retentionTagVersion": "637671427933449525" |
| 148 | + }, |
| 149 | + "resourceGroup": "testBkpVaultRG", |
| 150 | + "systemData": null, |
| 151 | + "type": "Microsoft.DataProtection/backupVaults/backupInstances/recoveryPoints" |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +If you need to fetch the recovery point from archive tier, then the _type_ variable in _recoveryPointDataStoreDetails_ will be _ArchiveStore_. |
| 156 | + |
| 157 | +### Prepare the restore request |
| 158 | + |
| 159 | +There are various restore options for a PostgreSQL database. You can restore the recovery point as another database or restore as files. The recovery point can be on archive tier as well. |
| 160 | + |
| 161 | +#### Restore as database |
| 162 | + |
| 163 | +Construct the Azure Resource Manager ID (ARM ID) of the new PostgreSQL database to be created (with the target PostgreSQL server to which permissions were assigned as detailed [above](#set-up-permissions)) and the required PostgreSQL database name. For example, a PostgreSQL database can be named **emprestored21** under a target PostgreSQL server **targetossserver** in resource group **targetrg** with a different subscription. |
| 164 | + |
| 165 | +```azurecli |
| 166 | +$targetOssId = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourceGroups/targetrg/providers/providers/Microsoft.DBforPostgreSQL/servers/targetossserver/databases/emprestored21" |
| 167 | +``` |
| 168 | + |
| 169 | +Use the [az dataprotection backup-instance restore initialize-for-data-recovery](/cli/azure/dataprotection/backup-instance/restore?view=azure-cli-latest&preserve-view=true#az_dataprotection_backup_instance_restore_initialize_for_data_recovery) command to prepare the restore request with all relevant details. |
| 170 | + |
| 171 | +```azurecli |
| 172 | +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 |
| 173 | +``` |
| 174 | + |
| 175 | +For an archive-based recovery point, you need to: |
| 176 | + |
| 177 | +1. Rehydrate from archive datastore to vault store |
| 178 | +1. Modify the source datastore. |
| 179 | +1. Add other parameters to specify the rehydration priority. |
| 180 | +1. Specify the duration for which the rehydrated recovery point should be retained in the vault data store. |
| 181 | +1. Restore as a database from this recovery point. |
| 182 | + |
| 183 | +Use the following command to prepare the request for all the above mentioned operations, at once. |
| 184 | + |
| 185 | +```azurecli |
| 186 | +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 |
| 187 | +``` |
| 188 | + |
| 189 | +#### Restore as files |
| 190 | + |
| 191 | +Fetch the URI of the container, within the storage account to which permissions were assigned as detailed [above](#set-up-permissions). For example, a container named **testcontainerrestore** under a storage account **testossstorageaccount** with a different subscription. |
| 192 | + |
| 193 | +```azurecli |
| 194 | +$contURI = "https://testossstorageaccount.blob.core.windows.net/testcontainerrestore" |
| 195 | +``` |
| 196 | + |
| 197 | +Use the [az dataprotection backup-instance restore initialize-for-data-recovery-as-files](/cli/azure/dataprotection/backup-instance/restore?view=azure-cli-latest&preserve-view=true#az_dataprotection_backup_instance_restore_initialize_for_data_recovery_as_files) command to prepare the restore request with all relevant details. |
| 198 | + |
| 199 | +```azurecli |
| 200 | +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 |
| 201 | +``` |
| 202 | + |
| 203 | +For archive-based recovery point, modify the source datastore, and add the rehydration priority and the retention duration, in days, of the rehydrated recovery point as mentioned below: |
| 204 | + |
| 205 | +```azurecli |
| 206 | +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 |
| 207 | +``` |
| 208 | + |
| 209 | +You can also validate if the JSON file will succeed to create new resources using the [az dataprotection backup-instance validate-for-restore](/cli/azure/dataprotection/backup-instance?view=azure-cli-latest&preserve-view=true#az_dataprotection_backup_instance_validate_for_restore) command. |
| 210 | + |
| 211 | +### Trigger the restore |
| 212 | + |
| 213 | +Use the [az dataprotection backup-instance restore trigger](/cli/azure/dataprotection/backup-instance/restore?view=azure-cli-latest&preserve-view=true#az_dataprotection_backup_instance_restore_trigger) command to trigger the restore operation with the request prepared above. |
| 214 | + |
| 215 | +```azurecli-interactive |
| 216 | +az dataprotection backup-instance restore trigger -g testBkpVaultRG --vault-name TestBkpVault --backup-instance-name testpostgresql-empdb11-957d23b1-c679-4c94-ade6-c4d34635e149 --parameters OssRestoreReq.JSON |
| 217 | +``` |
| 218 | + |
| 219 | +## Tracking job |
| 220 | + |
| 221 | +Track all jobs using the [az dataprotection job list](/cli/azure/dataprotection/job?view=azure-cli-latest&preserve-view=true#az_dataprotection_job_list) command. You can list all jobs and fetch a particular job detail. |
| 222 | + |
| 223 | +You can also use _Az.ResourceGraph_ to track all jobs across all Backup vaults. Use the [az dataprotection job list-from-resourcegraph](/cli/azure/dataprotection/job?view=azure-cli-latest&preserve-view=true#az_dataprotection_job_list_from_resourcegraph) command to get the relevant job that is across all Backup vaults. |
| 224 | + |
| 225 | +```azurecli |
| 226 | +az dataprotection job list-from-resourcegraph --datasource-type AzureDatabaseForPostgreSQL --operation Restore |
| 227 | +``` |
| 228 | + |
| 229 | +## Next steps |
| 230 | + |
| 231 | +- [Azure PostgreSQL Backup overview](backup-azure-database-postgresql-overview.md) |
0 commit comments