本文說明如何備份 Azure PostgreSQL 資料庫 - 使用 Azure CLI 的彈性伺服器 。
在本文中,您將學會如何:
- 建立備份保存庫
- 建立備份原則
- 設定 適用於 PostgreSQL 的 Azure 資料庫的備份 - 彈性伺服器
- 執行隨選備份作業
如需 PostgreSQL 資料庫支援案例和限制的資訊,請參閱 支援矩陣。
建立備份保存庫
備份保存庫是 Azure 中的儲存體實體。 這會儲存 Azure 備份所支援的新工作負載備份資料。 例如,適用於 PostgreSQL 的 Azure 資料庫 – 彈性伺服器、記憶體帳戶中的 Blob 和 Azure 磁碟。 保存可協助您組織備份資料,同時減輕管理負擔。 備份保存庫是以 Azure 的 Azure Resource Manager 模型為基礎,可提供增強功能來協助保護備份資料。
在您建立備份保存庫之前,請在保存庫中選擇資料的儲存體備援。 然後,繼續使用該儲存體備援和位置來建立備份保存庫。
在本文中,我們會在資源群組 testBkpVaultRG 下的區域 westus 中建立備份保存庫 TestBkpVault。 使用 az data protection vault create 命令來建立備份保存庫。 深入了解如何建立備份保存庫。
az dataprotection backup-vault create -g testBkpVaultRG --vault-name TestBkpVault -l westus --type SystemAssigned --storage-settings datastore-type="VaultStore" type="LocallyRedundant"
{
"eTag": null,
"id": "/subscriptions/00001111-aaaa-2222-bbbb-3333cccc4444/resourcegroups/testBkpVaultRG/providers/Microsoft.DataProtection/BackupVaults/TestBkpVault",
"identity": {
"principalId": "00001111-aaaa-2222-bbbb-3333cccc4444",
"tenantId": "aaaabbbb-0000-cccc-1111-dddd2222eeee",
"type": "SystemAssigned"
},
"location": "westus",
"name": "TestBkpVault",
"properties": {
"provisioningState": "Succeeded",
"storageSettings": [
{
"datastoreType": "VaultStore",
"type": "LocallyRedundant"
}
]
},
"resourceGroup": "testBkpVaultRG",
"systemData": null,
"tags": null,
"type": "Microsoft.DataProtection/backupVaults"
}
建立保存庫之後,讓我們建立備份原則來保護 Azure PostgreSQL – 彈性伺服器。
建立備份原則
了解 PostGreSQL 備份原則
讓我們瞭解 PostgreSQL 的備份原則物件 - 彈性伺服器。
- PolicyRule
- BackupRule
- BackupParameter
- BackupType (在此情況下為完整伺服器備份)
- 初始資料存放區 (備份所在的位置)
- 觸發程序 (觸發備份的方式)
- 依據排程
- 預設標記準則 (所有已排程備份的預設「標記」。此標記會將備份連結至保留規則)
- BackupParameter
- 預設保留規則 (預設會套用至初始資料存放區上所有備份的規則)
- BackupRule
此物件會定義觸發的備份類型、觸發方式(透過排程)、其標記的內容、落地位置(資料存放區),以及資料存放區中備份數據的生命週期。 PostgreSQL 的預設物件表示每周觸發完整備份,並到達保存庫,儲存了三個月。
擷取原則範本
若要瞭解 Azure PostgreSQL 資料庫備份備份備份原則的內部元件,請使用 az data protection backup-policy get-default-policy-template 命令來擷取原則範本 。 此命令會傳回指定資料來源類型的預設原則範本。 使用此原則範本來建立新的原則。
az dataprotection backup-policy get-default-policy-template --datasource-type AzureDatabaseForPostgreSQLFlexibleServer
{
"datasourceTypes": [
"Microsoft.DBforPostgreSQL/flexibleServers"
],
"name": "OssPolicy1",
"objectType": "BackupPolicy",
"policyRules": [
{
"backupParameters": {
"backupType": "Full",
"objectType": "AzureBackupParams"
},
"dataStore": {
"dataStoreType": "VaultStore",
"objectType": "DataStoreInfoBase"
},
"name": "BackupWeekly",
"objectType": "AzureBackupRule",
"trigger": {
"objectType": "ScheduleBasedTriggerContext",
"schedule": {
"repeatingTimeIntervals": [
"R/2021-08-15T06:30:00+00:00/P1W"
],
"timeZone": "UTC"
},
"taggingCriteria": [
{
"isDefault": true,
"tagInfo": {
"id": "Default_",
"tagName": "Default"
},
"taggingPriority": 99
}
]
}
},
{
"isDefault": true,
"lifecycles": [
{
"deleteAfter": {
"duration": "P3M",
"objectType": "AbsoluteDeleteOption"
},
"sourceDataStore": {
"dataStoreType": "VaultStore",
"objectType": "DataStoreInfoBase"
},
"targetDataStoreCopySettings": []
}
],
"name": "Default",
"objectType": "AzureRetentionRule"
}
]
}
原則範本是由觸發程序 (決定由什麼項目觸發備份) 和生命週期 (決定何時刪除/複製/移動備份) 所組成。 在 Azure PostgreSQL - 彈性伺服器備份中,觸發程式的預設值是排程的每周觸發程式(每七天一次備份),並保留每個備份三個月。
排程觸發程序:
"trigger": {
"objectType": "ScheduleBasedTriggerContext",
"schedule": {
"repeatingTimeIntervals": [
"R/2021-08-15T06:30:00+00:00/P1W"
],
"timeZone": "UTC"
}
預設保留規則生命週期:
{
"isDefault": true,
"lifecycles": [
{
"deleteAfter": {
"duration": "P3M",
"objectType": "AbsoluteDeleteOption"
},
"sourceDataStore": {
"dataStoreType": "VaultStore",
"objectType": "DataStoreInfoBase"
},
"targetDataStoreCopySettings": []
}
],
"name": "Default",
"objectType": "AzureRetentionRule"
}
正在修改原則範本
重要
在 Azure PowerShell 中,可以將物件作為暫存位置,來執行所有修改作業。 由於 Azure CLI 沒有物件的概念,因此我們必須使用檔案。 每項編輯作業都應該重新導向至新的檔案,該檔案會從輸入檔中讀取內容,並重新導向至輸出檔案。 在指令碼中使用檔案時,您可視需要將其重新命名。
修改排程
預設原則範本每週備份一次。 您可以修改備份的排程,將其改為每週提供多次備份。 若要修改排程,請使用 az data protection backup-policy trigger set 命令。
下列範例會修改每週備份,以在每個星期日、星期三和星期五進行備份。 排程日期陣列會提及日期,一週的星期幾即會視這些日期為星期幾而定。 也需將這些排程指定為每週重複一次。 因此,排程間隔為 1,而間隔類型為 [每週]。
az dataprotection backup-policy trigger create-schedule --interval-type Weekly --interval-count 1 --schedule-days 2021-08-15T22:00:00 2021-08-18T22:00:00 2021-08-20T22:00:00
[
"R/2021-08-15T22:00:00+00:00/P1W",
"R/2021-08-18T22:00:00+00:00/P1W",
"R/2021-08-20T22:00:00+00:00/P1W"
]
az dataprotection backup-policy trigger set --policy .\OSSPolicy.json --schedule R/2021-08-15T22:00:00+00:00/P1W R/2021-08-18T22:00:00+00:00/P1W R/2021-08-20T22:00:00+00:00/P1W > EditedOSSPolicy.json
新增保留規則
默認範本具有預設保留規則下初始數據存放區的生命週期。 在此案例中,規則表示會在三個月後刪除備份資料。 使用 az data protection backup-policy retention-rule create-lifecycle 命令來建立新的生命週期,並使用 az data protection backup-policy retention-rule set 命令將它們與新規則或現有規則產生關聯。
下列範例會建立名為 Monthly 的新保留規則,其中每個月的第一個成功備份應該保留在保存庫中六個月。
az dataprotection backup-policy retention-rule create-lifecycle --retention-duration-count 6 --retention-duration-type Months --source-datastore VaultStore > VaultLifeCycle.JSON
az dataprotection backup-policy retention-rule set --lifecycles .\VaultLifeCycle.JSON --name Monthly --policy .\EditedOSSPolicy.json > AddedRetentionRulePolicy.JSON
新增標記和相關準則
建立保留規則後,您便可在備份原則的觸發程序屬性中,建立對應的標記。 使用 az data protection backup-policy tag create-absolute-criteria 命令來建立新的標記準則,並使用 az data protection backup-policy tag set 命令來更新現有的標記或建立新的標記。
下列範例會針對當月的初次成功備份,建立新的標記和準則。 標記會與要套用的對應保留規則具有相同名稱。
在此範例中,標記準則應命名為每月。
az dataprotection backup-policy tag create-absolute-criteria --absolute-criteria FirstOfMonth > tagCriteria.JSON
az dataprotection backup-policy tag set --criteria .\tagCriteria.JSON --name Monthly --policy .\AddedRetentionRulePolicy.JSON > AddedRetentionRuleAndTag.JSON
現在,如果慣用的排程是每周多個備份(本範例中指定的每個星期日、星期三、星期四),而且您想要封存星期日和星期五備份,則可以使用 az data protection backup-policy tag create-generic-criteria 命令來變更標記準則 。
az dataprotection backup-policy tag create-generic-criteria --days-of-week Sunday Friday > tagCriteria.JSON
az dataprotection backup-policy tag set --criteria .\tagCriteria.JSON --name Monthly --policy .\AddedRetentionRulePolicy.JSON > AddedRetentionRuleAndTag.JSON
建立新的 PostgreSQL - 彈性伺服器備份原則
根據需求修改範本之後,請使用 az data protection backup-policy create 命令,使用修改過的範本建立原則。
az dataprotection backup-policy create --backup-policy-name FinalOSSPolicy --policy AddedRetentionRuleAndTag.JSON --resource-group testBkpVaultRG --vault-name TestBkpVault
設定備份
建立保存庫和原則之後,您必須考慮三個重點,以保護 Azure PostgreSQL 資料庫。
主要相關實體
要保護的 PostGreSQL 資料庫
擷取 PostgreSQL 的 Azure Resource Manager 識別符 (Azure Resource Manager 標識符) - 要保護的彈性伺服器。 此標識碼可作為伺服器的識別碼。 我們使用PostgreSQL伺服器 testpgflex 的範例,其存在於不同訂用帳戶下的資源群組 ossrg 中。
下列範例會使用 Bash。
ossId="/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/ossrg/providers/Microsoft.DBforPostgreSQL/flexibleServers/testpgflex"
備份保存庫
備份保存庫必須連線到 PostgreSQL - 彈性伺服器,因此需要存取此伺服器。 存取權會授與備份保存庫的受控系統識別(MSI)。
查看您應該授與 PostgreSQL 上備份保存庫受控系統識別 (MSI) 的許可權 - 彈性伺服器。
準備要求
設定所有相關的權限之後,請以兩個步驟來執行備份的設定。
- 我們會使用相關的保存庫、原則、PostgreSQL 資料庫,使用 az data protection backup-instance initialize 命令來準備相關要求。
- 我們會提交要求,以使用 az data protection backup-instance create 命令來保護伺服器。
az dataprotection backup-instance initialize --datasource-id $ossId --datasource-type AzureDatabaseForPostgreSQLFlexibleServer -l <vault-location> --policy-id <policy_arm_id> > OSSBkpInstance.JSON
az dataprotection backup-instance create --resource-group testBkpVaultRG --vault-name TestBkpVault TestBkpvault --backup-instance .\OSSBkpInstance.JSON
執行隨選備份
您必須在觸發備份時指定保留規則。 若要檢視原則中的保留規則,請瀏覽保留規則的原則 JSON 檔案。 在下列範例中,有兩個名稱為預設和每月的保留規則。 我們會針對隨選備份使用 每月 規則。
az dataprotection backup-policy show -g ossdemorg --vault-name ossdemovault-1 --subscription eaaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e --name osspol5
{
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/ossdemorg/providers/Microsoft.DataProtection/backupVaults/ossdemovault-1/backupPolicies/osspol5",
"name": "osspol5",
"properties": {
"datasourceTypes": [
"Microsoft.DBforPostgreSQL/flexibleServers"
],
"objectType": "BackupPolicy",
"policyRules": [
{
"backupParameters": {
"backupType": "Full",
"objectType": "AzureBackupParams"
},
"dataStore": {
"dataStoreType": "VaultStore",
"objectType": "DataStoreInfoBase"
},
"name": "BackupWeekly",
"objectType": "AzureBackupRule",
"trigger": {
"objectType": "ScheduleBasedTriggerContext",
"schedule": {
"repeatingTimeIntervals": [
"R/2020-04-04T20:00:00+00:00/P1W",
"R/2020-04-01T20:00:00+00:00/P1W"
],
"timeZone": "UTC"
},
"taggingCriteria": [
{
"criteria": [
{
"absoluteCriteria": [
"FirstOfMonth"
],
"daysOfMonth": null,
"daysOfTheWeek": null,
"monthsOfYear": null,
"objectType": "ScheduleBasedBackupCriteria",
"scheduleTimes": null,
"weeksOfTheMonth": null
}
],
"isDefault": false,
"tagInfo": {
"eTag": null,
"id": "Monthly_",
"tagName": "Monthly"
},
"taggingPriority": 15
},
{
"criteria": null,
"isDefault": true,
"tagInfo": {
"eTag": null,
"id": "Default_",
"tagName": "Default"
},
"taggingPriority": 99
}
]
}
},
{
"isDefault": false,
"lifecycles": [
{
"deleteAfter": {
"duration": "P10Y",
"objectType": "AbsoluteDeleteOption"
},
"sourceDataStore": {
"dataStoreType": "VaultStore",
"objectType": "DataStoreInfoBase"
},
"targetDataStoreCopySettings": []
}
],
"name": "Monthly",
"objectType": "AzureRetentionRule"
},
{
"isDefault": true,
"lifecycles": [
{
"deleteAfter": {
"duration": "P1Y",
"objectType": "AbsoluteDeleteOption"
},
"sourceDataStore": {
"dataStoreType": "VaultStore",
"objectType": "DataStoreInfoBase"
},
"targetDataStoreCopySettings": []
}
],
"name": "Default",
"objectType": "AzureRetentionRule"
}
]
},
"resourceGroup": "ossdemorg",
"systemData": null,
"type": "Microsoft.DataProtection/backupVaults/backupPolicies"
}
若要觸發隨選備份,請使用 az data protection backup-instance adhoc-backup 命令。
az dataprotection backup-instance adhoc-backup --name "ossrg-testpgflex" --rule-name "Monthly" --resource-group testBkpVaultRG --vault-name TestBkpVault
追蹤作業
使用 az data protection job list 命令來追蹤所有作業。 您可以列出所有工作,並擷取特定的工作詳細資料。
您也可以使用 Az.ResourceGraph,追蹤所有備份保存庫的所有作業。 使用 az data protection job list-from-resourcegraph 命令來擷取跨備份保存庫的相關作業。
az dataprotection job list-from-resourcegraph --datasource-type AzureDatabaseForPostgreSQLFlexibleServer --status Completed