使用 Azure CLI 備份 Azure 受控磁碟

本文說明如何使用 Azure CLI 備份 Azure 受控磁碟

重要

透過 CLI 支援 Azure 受控磁碟的備份與還原,目前仍處於預覽階段,在 Az 2.15.0 版與更新版本中會以延伸模組的形式提供。 該延伸模組將會在您執行 az dataprotection 命令時自動安裝。 深入了解擴充功能。

在本文中,您將學會如何:

  • 建立備份保存庫

  • 建立備份原則

  • 設定 Azure 磁碟的備份

  • 執行隨選備份作業

如需 Azure 磁碟備份區域可用性、支援情節及限制的相關資訊,請參閱支援矩陣

建立備份保存庫

備份保存庫是 Azure 中的儲存體實體,可儲存 Azure 備份所支援各種較新工作負載的備份資料,例如適用於 PostgreSQL 的 Azure 資料庫伺服器、儲存體帳戶中的 Blob 和 Azure 磁碟。 保存庫可讓您輕鬆組織備份資料,同時可減輕管理負擔。 備份保存庫是以 Azure 的 Azure Resource Manager 模型為基礎,可提供增強功能來協助保護備份資料。

在您建立備份保存庫之前,請在保存庫中選擇資料的儲存體備援。 然後,繼續使用該儲存體備援和位置來建立備份保存庫。 在本文中,我們會在 westus 中的資源群組 testBkpVaultRG 下,建立備份保存庫 TestBkpVault。 使用 az dataprotection 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/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testBkpVaultRG/providers/Microsoft.DataProtection/BackupVaults/TestBkpVault",
  "identity": {
    "principalId": "2ca1d5f7-38b3-4b61-aa45-8147d7e0edbc",
    "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "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 磁碟。

建立備份原則

若要瞭解 Azure 磁碟備份的備份原則內部元件,請使用 az dataprotection backup-policy get-default-policy-template 命令來擷取原則範本。 此命令會傳回指定資料來源類型的預設原則範本。 使用此原則範本來建立新的原則。

az dataprotection backup-policy get-default-policy-template --datasource-type AzureDisk
{
  "datasourceTypes": [
    "Microsoft.Compute/disks"
  ],
  "name": "DiskPolicy",
  "objectType": "BackupPolicy",
  "policyRules": [
    {
      "backupParameters": {
        "backupType": "Incremental",
        "objectType": "AzureBackupParams"
      },
      "dataStore": {
        "dataStoreType": "OperationalStore",
        "objectType": "DataStoreInfoBase"
      },
      "name": "BackupHourly",
      "objectType": "AzureBackupRule",
      "trigger": {
        "objectType": "ScheduleBasedTriggerContext",
        "schedule": {
          "repeatingTimeIntervals": [
            "R/2020-04-05T13:00:00+00:00/PT4H"
          ]
        },
        "taggingCriteria": [
          {
            "isDefault": true,
            "tagInfo": {
              "id": "Default_",
              "tagName": "Default"
            },
            "taggingPriority": 99
          }
        ]
      }
    },
    {
      "isDefault": true,
      "lifecycles": [
        {
          "deleteAfter": {
            "duration": "P7D",
            "objectType": "AbsoluteDeleteOption"
          },
          "sourceDataStore": {
            "dataStoreType": "OperationalStore",
            "objectType": "DataStoreInfoBase"
          }
        }
      ],
      "name": "Default",
      "objectType": "AzureRetentionRule"
    }
  ]
}

原則範本是由觸發程序 (決定由什麼項目觸發備份) 和生命週期 (決定何時刪除/複製/移動備份) 所組成。 在 Azure 磁碟備份中,觸發程序的預設值是每 4 小時的排程觸發程序 (PT4H),每個備份保留七天。

排程觸發程序:

"trigger": {
        "objectType": "ScheduleBasedTriggerContext",
        "schedule": {
          "repeatingTimeIntervals": [
            "R/2020-04-05T13:00:00+00:00/PT4H"
          ]
        }
}

預設保留生命週期:

"lifecycles": [
        {
          "deleteAfter": {
            "duration": "P7D",
            "objectType": "AbsoluteDeleteOption"
          },
          "sourceDataStore": {
            "dataStoreType": "OperationalStore",
            "objectType": "DataStoreInfoBase"
          }
        }
      ]

Azure 磁碟備份每天提供多次備份。 如果您需要更頻繁的備份,請選擇 [每小時] 備份頻率,就能夠每 4、6、8 或 12 小時備份一次。 備份會根據選取的時間間隔進行排程。

例如,如果您選取 [每 4 小時],則大約每 4 小時就會備份一次,以平均分散於一天之中備份。 如果一天一次的備份足堪使用,請選擇 [每日] 備份頻率。 在每日備份頻率中,您可以指定當天何時進行備份。

重要

一天中的時間表示備份開始時間,而不是備份完成的時間。

完成備份作業所需的時間取決於各種因素,包括磁碟的大小,以及連續備份之間的流失率。 不過,Azure 磁碟備份是無代理程式備份,並使用不影響生產應用程式效能的增量快照集

注意

雖然選取的保存庫可能具有全域備援設定,但是 Azure 磁碟備份目前僅支援快照集資料存放區。 所有備份都會儲存在您訂用帳戶的資源群組中,而不會複製到備份保存庫儲存體。

若要深入瞭解原則建立的詳細資訊,請參閱 Azure 磁碟備份原則文件。

一旦將範本下載為 JSON 檔案,您就可以視需要編輯排程和保留。 然後使用產生的 JSON 建立新的原則。 如果您想要編輯每小時頻率或保留期間,請使用 az dataprotection backup-policy trigger set 和/或 az dataprotection backup-policy retention-rule set 命令。 一旦原則 JSON 具有所有必要值,請繼續使用 az dataprotection backup-policy create 命令,從原則物件建立新的原則。

az dataprotection backup-policy get-default-policy-template --datasource-type AzureDisk > policy.json
az dataprotection backup-policy create -g testBkpVaultRG --vault-name TestBkpVault -n mypolicy --policy policy.json
{
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/TestBkpVault/backupPolicies/mypolicy",
"name": "mypolicy",
"properties": {
"datasourceTypes": [
"Microsoft.Compute/disks"
],
"objectType": "BackupPolicy",
"policyRules": [
{
"backupParameters": {
"backupType": "Incremental",
"objectType": "AzureBackupParams"
},
"dataStore": {
"dataStoreType": "OperationalStore",
"objectType": "DataStoreInfoBase"
},
"name": "BackupHourly",
"objectType": "AzureBackupRule",
"trigger": {
"objectType": "ScheduleBasedTriggerContext",
"schedule": {
"repeatingTimeIntervals": [
"R/2020-04-05T13:00:00+00:00/PT4H"
]
},
"taggingCriteria": [
{
"criteria": null,
"isDefault": true,
"tagInfo": {
"eTag": null,
"id": "Default_",
"tagName": "Default"
},
"taggingPriority": 99
}
]
}
},
{
"isDefault": true,
"lifecycles": [
{
"deleteAfter": {
"duration": "P7D",
"objectType": "AbsoluteDeleteOption"
},
"sourceDataStore": {
"dataStoreType": "OperationalStore",
"objectType": "DataStoreInfoBase"
},
"targetDataStoreCopySettings": null
}
],
"name": "Default",
"objectType": "AzureRetentionRule"
}
]
},
"resourceGroup": "testBkpVaultRG",
"systemData": null,
"type": "Microsoft.DataProtection/backupVaults/backupPolicies"
}

設定備份

建立保存庫和原則之後,您必須考慮三個重點,以保護 Azure 磁碟。

主要相關實體

要保護的磁碟

擷取要保護的磁碟的 ARM 識別碼和位置。 此將作為磁碟的識別碼。 我們將使用名為 CLITestDisk 的磁碟作為範例,其位於不同訂用帳戶的資源群組 diskrg 下。

$DiskId = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk"

快照資源群組

磁碟快照集儲存在訂用帳戶內的資源群組中。 在指導方針中,我們建議您建立專用的資源群組來作為 Azure 備份服務所要使用的快照集資料存放區。 專用資源群組可限制資源群組的存取權限,讓備份資料更安全且易於管理。 請記下想要放置磁碟快照集的資源群組 ARM 識別碼

$snapshotrg = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourceGroups/snapshotrg"

備份保存庫

備份保存庫需要磁碟和快照集資源群組的權限,才能觸發快照集並管理其生命週期。 系統指派的保存庫受控識別用來指派這類權限。 使用 az dataprotection backup-vault update 命令,為復原服務保存庫啟用系統指派的受控識別。

az dataprotection backup-vault update -g testBkpVaultRG --vault-name TestBkpVault --type SystemAssigned

指派權限

您必須透過 RBAC 將一些權限指派給保存庫 (以保存庫 MSI 表示) 及相關的磁碟和/或磁碟 RG。 您可以透過 Azure 入口網站或 CLI 來執行這些作業。 若要指派相關權限,請參閱設定受控磁碟備份的必要條件

準備要求

設定所有相關的權限之後,請以兩個步驟來執行備份的設定。 首先,我們要使用 az dataprotection backup-instance initialize 命令,藉由使用相關保存庫、原則、磁碟和快照集資源群組來準備相關要求。 初始化命令會傳回 JSON 檔案,然後您必須更新快照集資源群組值。 然後,我們使用 az dataprotection backup-instance create 命令來提交保護磁碟的要求。

az dataprotection backup-instance initialize --datasource-type AzureDisk  -l southeastasia --policy-id "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testBkpVaultRG/providers/Microsoft.DataProtection/backupVaults/TestBkpVault/backupPolicies/mypolicy" --datasource-id "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk" > backup_instance.json

開啟 JSON 檔案,並且在 data_store_parameters_list 區段底下的 resource_group_id 中編輯快照集資源群組識別碼

{
  "backup_instance_name": "diskrg-CLITestDisk-3df6ac08-9496-4839-8fb5-8b78e594f166",
  "properties": {
    "data_source_info": {
      "datasource_type": "Microsoft.Compute/disks",
      "object_type": "Datasource",
      "resource_id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk",
      "resource_location": "southeastasia",
      "resource_name": "CLITestDisk",
      "resource_type": "Microsoft.Compute/disks",
      "resource_uri": ""
    },
    "data_source_set_info": null,
    "object_type": "BackupInstance",
    "policy_info": {
      "policy_id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testBkpVaultRG/providers/Microsoft.DataProtection/BackupVaults/TestBkpVault/backupPolicies/DiskPolicy",
      "policy_parameters": {
        "data_store_parameters_list": [
          {
            "data_store_type": "OperationalStore",
            "object_type": "AzureOperationalStoreParameters",
            "resource_group_id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/snapshotrg"
          }
        ]
      }
    }
  }
}

注意

備份執行個體名稱是由用戶端所產生,因此這會是唯一的值。 這是以資料來源名稱和唯一 GUID 為基礎。 在您列出備份執行個體之後,便應可檢查備份執行個體的名稱以及相關的資料來源名稱。

使用編輯過的 JSON 檔案來建立 Azure 受控磁碟的備份執行個體。

az dataprotection backup-instance create -g testBkpVaultRG --vault-name TestBkpVault --backup-instance backup_instance.json
{
  "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testBkpVaultRG/providers/Microsoft.DataProtection/BackupVaults/TestBkpVault/backupInstances/diskrg-CLITestDisk-3df6ac08-9496-4839-8fb5-8b78e594f166",
  "name": "diskrg-CLITestDisk-3df6ac08-9496-4839-8fb5-8b78e594f166",
  "properties": {
    "currentProtectionState": "ProtectionConfigured",
    "dataSourceInfo": {
      "datasourceType": "Microsoft.Compute/disks",
      "objectType": "Datasource",
      "resourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk",
      "resourceLocation": "southeastasia",
      "resourceName": "CLITestDisk",
      "resourceType": "Microsoft.Compute/disks",
      "resourceUri": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk"
    },
    "dataSourceSetInfo": null,
    "friendlyName": "CLITestDisk",
    "objectType": "BackupInstance",
    "policyInfo": {
      "policyId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testBkpVaultRG/providers/Microsoft.DataProtection/BackupVaults/TestBkpVault/backupPolicies/DiskPolicy",
      "policyParameters": {
        "dataStoreParametersList": [
          {
            "dataStoreType": "OperationalStore",
            "objectType": "AzureOperationalStoreParameters",
            "resourceGroupId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/sarath-rg"
          }
        ]
      },
      "policyVersion": null
    },
    "protectionErrorDetails": null,
    "protectionStatus": {
      "errorDetails": null,
      "status": "ProtectionConfigured"
    },
    "provisioningState": "Succeeded"
  },
  "resourceGroup": "testBkpVaultRG",
  "systemData": null,
  "type": "Microsoft.DataProtection/backupVaults/backupInstances"
}

建立備份執行個體之後,如果您不想等候原則的排程,您可以繼續觸發隨選備份。

執行隨選備份

使用 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 AzureDisk --datasource-id /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk
[
  {
    "datasourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk",
    "extendedLocation": null,
    "id": "//subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testBkpVaultRG/providers/Microsoft.DataProtection/BackupVaults/TestBkpVault/backupInstances/diskrg-CLITestDisk-3df6ac08-9496-4839-8fb5-8b78e594f166",
    "identity": null,
    "kind": "",
    "location": "",
    "managedBy": "",
    "name": "diskrg-CLITestDisk-3df6ac08-9496-4839-8fb5-8b78e594f166",
    "plan": null,
    "properties": {
      "currentProtectionState": "ProtectionConfigured",
      "dataSourceInfo": {
        "baseUri": null,
        "datasourceType": "Microsoft.Compute/disks",
        "objectType": "Datasource",
        "resourceID": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk",
        "resourceLocation": "westus",
        "resourceName": "CLITestDisk",
        "resourceType": "Microsoft.Compute/disks",
        "resourceUri": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx/resourcegroups/diskrg/providers/Microsoft.Compute/disks/CLITestDisk"
      },
      "dataSourceProperties": null,
      "dataSourceSetInfo": null,
      "datasourceAuthCredentials": null,
      "friendlyName": "CLITestDisk",
      "objectType": "BackupInstance",
      "policyInfo": {
        "policyId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testBkpVaultRG/providers/Microsoft.DataProtection/BackupVaults/TestBkpVault/backupPolicies/DiskPolicy",
        "policyParameters": {
          "dataStoreParametersList": [
            {
              "dataStoreType": "OperationalStore",
              "objectType": "AzureOperationalStoreParameters",
              "resourceGroupId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/snapshotrg"
            }
          ]
        },
        "policyVersion": null
      },
      "protectionErrorDetails": null,
      "protectionStatus": {
        "errorDetails": null,
        "status": "ProtectionConfigured"
      },
      "provisioningState": "Succeeded"
    },
    "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
  }
]

您可以在觸發備份時指定規則和標籤名稱。 若要檢視原則中的規則,請查看原則 JSON。 在下列範例中,會顯示名稱為 "BackupDaily" 和標籤名稱 "default" 的規則,我們會將該規則用於隨選備份。

"name": "BackupDaily",
        "objectType": "AzureBackupRule",
        "trigger": {
          "objectType": "ScheduleBasedTriggerContext",
          "schedule": {
            "repeatingTimeIntervals": [
              "R/2022-09-27T23:30:00+00:00/P1D"
            ],
            "timeZone": "UTC"
          },
         "taggingCriteria": [
           {
              "criteria": null,
              "isDefault": true,
              "tagInfo": {
                "eTag": null,
                "id": "Default_",
                "tagName": "Default"
              },
              "taggingPriority": 99
    }

使用 az dataprotection backup-instance adhoc-backup 命令來觸發隨選備份。

az dataprotection backup-instance adhoc-backup --name "diskrg-CLITestDisk-3df6ac08-9496-4839-8fb5-8b78e594f166" --rule-name "BackupDaily" --resource-group "000pikumar" --vault-name "PratikPrivatePreviewVault1" --retention-tag-override "default"

追蹤作業

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

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

az dataprotection job list-from-resourcegraph --datasource-type AzureDisk --status Completed

下一步

使用 Azure CLI 還原 Azure 受控磁碟