使用 REST API 還原 Azure VM 中的 SQL Server 資料庫

本文說明如何利用 Azure 備份 建立的還原點,使用 RcEST API 還原 Azure VM 中的 SQL 伺服器資料庫。

閱讀本文後,即會了解如何使用 REST API 來執行下列作業:

  • 檢視已備份 SQL 資料庫的還原點。
  • 查詢 SQL 資料庫與實例的快照恢復點與時間點復原範圍。
  • 擷取復原點中包含的資料庫與資料目錄路徑。
  • 還原完整的 SQL 資料庫。

若要檢視我們今天支援的備份和還原案例,請參閱 支援矩陣。 常見問題請參見 常見問題。

必要條件

本文假設您有已備份的 SQL 資料庫可供還原。 若非如此,請參閱使用 REST API 在 Azure VM 中備份 SQL Server 資料庫來建立備份。

本文會使用到下列資源:

  • 復原服務保存庫:SQLServer2012
  • 資源群組:SQLServerSelfHost
  • SQL Server:sqlserver-0
  • SQL 資料庫:msdb

主要區域還原

若要在主要區域中觸發定期還原工作,請參閱下列各節。 針對跨區域還原,請參閱跨區域還原一節。

擷取 ContainerName 和 ProtectedItemName

對於大部分與還原相關的 API 呼叫,需傳遞 {containerName} 和 {protectedItemName} URI 參數的值。 請在 GET backupprotectableitems 作業的回應主體中使用識別碼屬性來擷取這些參數的值。 本次範例中想要保護的資料庫識別碼如下:

/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.recoveryservices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/sqldatabase;mssqlserver;msdb

這些值的轉譯如下:

  • {containername}:VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0
  • {protectedItemName}:sqldatabase;mssqlserver;msdb

擷取已備份 SQL 資料庫的復原點

如何還原任何已備份的資料庫:

  1. 選取復原點以執行還原作業。

  2. 使用復原點清單 REST API 呼叫,列出已備份項目的可用復原點。 這是具有所有相關值的 GET 作業。

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/{fabricName}/protectionContainers/{containerName}/protectedItems/{protectedItemName}/recoveryPoints?api-version=2016-12-01
    

    設定 URI 值,如下所示:

    • {fabricName}:Azure
    • {vaultName}:SQLServer2012
    • {containerName}:VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0
    • {protectedItemName}:sqldatabase;mssqlserver;msdb
    • {resourceGroupName}:SQLServerSelfHost

    GET URI 具備所有必要參數。 不需要任何額外的請求主體。

    GET "https://management.azure.com/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.RecoveryServices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/sqldatabase;mssqlserver;msdb/recoveryPoints?api-version=2016-12-01"
    

擷取復原點的回應範例

提交 GET URI 之後會傳回 200 回應:

HTTP/1.1 200 OK
Pragma: no-cache
X-Content-Type-Options: nosniff
x-ms-request-id: fab6cc6f-db1e-4ac1-acac-fc82ebdb1fdb
x-ms-client-request-id: 6fb93717-2876-47df-b01f-d53af5f08785; 6fb93717-2876-47df-b01f-d53af5f08785
Strict-Transport-Security: max-age=31536000; includeSubDomains
x-ms-ratelimit-remaining-subscription-reads: 14999
x-ms-correlation-request-id: fab6cc6f-db1e-4ac1-acac-fc82ebdb1fdb
x-ms-routing-request-id: SOUTHINDIA:20180604T061127Z:fab6cc6f-db1e-4ac1-acac-fc82ebdb1fdb
Cache-Control: no-cache
Date: Mon, 04 Jun 2018 06:11:26 GMT
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET

{
  "value": [
    {
      "id": "/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.recoveryservices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/recoveryPoints/55515936059579",
      "name": "55515936059579",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLRecoveryPoint",
        "recoveryPointTimeInUTC": "2018-06-01T22:15:12Z",
        "type": "Full"
      }
    },
    {
      "id": "/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.recoveryservices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/recoveryPoints/62043109781074",
      "name": "62043109781074",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLRecoveryPoint",
        "recoveryPointTimeInUTC": "2018-05-31T22:15:08Z",
        "type": "Full"
      }
    },
    {
      "id": "/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.recoveryservices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/recoveryPoints/69710749096214",
      "name": "69710749096214",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLRecoveryPoint",
        "recoveryPointTimeInUTC": "2018-05-30T22:15:09Z",
        "type": "Full"
      }
    },
    {
      "id": "/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.recoveryservices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/recoveryPoints/55459165802209",
      "name": "55459165802209",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLRecoveryPoint",
        "recoveryPointTimeInUTC": "2018-05-29T22:15:15Z",
        "type": "Full"
      }
    },
    {
      "id": "/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.recoveryservices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/recoveryPoints/56798287946753",
      "name": "56798287946753",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLRecoveryPoint",
        "recoveryPointTimeInUTC": "2018-05-28T13:18:15Z",
        "type": "Full"
      }
    },
    {
      "id": "/subscriptions/00000000-0000-0000-0000-0000000000/resourceGroups/SQLServerSelfHost/providers/microsoft.recoveryservices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/recoveryPoints/DefaultRangeRecoveryPoint",
      "name": "DefaultRangeRecoveryPoint",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLPointInTimeRecoveryPoint",
        "timeRanges": [
          {
            "startTime": "2018-05-28T11:03:34Z",
            "endTime": "2018-06-02T00:02:31Z"
          }
        ],
        "type": "Log"
      }
    }
  ]
}

使用上面回應中的 {name} 欄位可識別復原點。

查詢快照與串流恢復點

對於快照保護的 SQL 實例與資料庫,對下列恢復點查詢,請使用 API 版本 2024-01-31-preview。 將 URI 預留位置替換成保存庫詳細資料,以及透過列出受保護項目傳回的容器和受保護項目名稱。 發送請求時,將篩選表達式進行 URL 編碼。

使用 restorePointQueryType 以選擇復原點類型:

Value 已傳回復原點
FullDifferentialAndSnapshot 資料庫的完整、差異及快照恢復點。
Snapshot 資料庫或實例的快照恢復點。
FullAndDifferential 資料庫的完整與差異恢復點串流。
Log 資料庫的日誌復原時間範圍。

列出完整、差分及快照恢復點

請使用資料庫受保護項目名稱,例如 SQLDataBase;mssqlserver;testdb:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=restorePointQueryType eq 'FullDifferentialAndSnapshot'

列出快照復原點

對於資料庫,請使用其資料庫受保護項目名稱:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=restorePointQueryType eq 'Snapshot'

對於執行個體,請使用其執行個體受保護項目名稱,例如 SQLInstance;mssqlserver:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{instanceProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=restorePointQueryType eq 'Snapshot'

列出串流的完整和差異復原點

你也可以指定時間範圍,並包含軟刪除的恢復點:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=startDate eq '2024-02-15 05:12:12 PM' and endDate eq '2024-02-22 05:12:12 PM' and restorePointQueryType eq 'FullAndDifferential' and includeSoftDeletedRP eq 'true'

用您所需的時間範圍取代範例日期。 value 陣列包含具有 type、recoveryPointTierDetails 和 recoveryPointTimeInUTC 等屬性的復原點。 保留資訊可出現在 recoveryPointProperties、 中,包括 expiryTime、 ruleName和 isSoftDeleted。

查詢時間點還原的記錄恢復範圍

要列出資料庫的日誌恢復範圍,請使用以下請求:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=restorePointQueryType eq 'Log'

回應中包含一項 DefaultRangeRecoveryPoint 物品。 以下範例顯示部分回應欄位:

{
  "value": [
    {
      "name": "DefaultRangeRecoveryPoint",
      "properties": {
        "objectType": "AzureWorkloadSQLPointInTimeRecoveryPoint",
        "timeRanges": [
          {
            "startTime": "2024-02-07T17:12:13.2853054Z",
            "endTime": "2024-02-22T15:30:13Z"
          }
        ],
        "type": "Log"
      }
    }
  ]
}

在顯示的範圍內選擇還原時間。

依恢復模式查詢資料庫日誌範圍

針對串流式復原,請指定 recoveryMode eq 'WorkloadRecovery':

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=restorePointQueryType eq 'Log' and recoveryMode eq 'WorkloadRecovery'

針對快照式恢復,請指定 recoveryMode eq 'SnapshotAttachAndRecover':

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=restorePointQueryType eq 'Log' and recoveryMode eq 'SnapshotAttachAndRecover'

兩種查詢皆回傳先前所示的日誌恢復範圍回應格式。

查詢資料庫日誌範圍以進行實例還原

以點時還原為例,首先選擇一個實例快照復原點並 檢索其包含的資料庫。 對於每個包含的資料庫,使用所選的基底快照詳細資料查詢其日誌範圍:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2024-01-31-preview&$filter=restorePointQueryType eq 'Log' and recoveryMode eq 'SnapshotAttachAndRecover' and baseSnapshotRPId eq '{baseSnapshotRPId}' and baseRPDatasourceId eq '{baseRPDatasourceId}'
  • {baseSnapshotRPId}:所選實例快照復原點的 name
  • {baseRPDatasourceId}:執行個體受保護項目的protectedItemDataSourceId,不是子系資料庫的資料來源識別碼。
  • {databaseProtectedItemName}:被查詢子資料庫的受保護項目名稱。

在實例快照中取得資料庫與磁碟路徑

取得選定的實例復原點,以取得其包含的資料庫和磁碟路徑。 準備實例快照還原或實例時間點還原時,請使用相同的中繼資料:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{instanceProtectedItemName}/recoveryPoints/{snapshotRecoveryPointId}?api-version=2024-01-31-preview

{snapshotRecoveryPointId} 是恢復點的 name。 以下範例顯示部分回應欄位:

{
  "name": "2265756871711897922",
  "properties": {
    "objectType": "AzureWorkloadSQLRecoveryPoint",
    "extendedInfo": {
      "dataDirectoryTimeInUTC": "2024-03-13T08:03:25Z",
      "dataDirectoryPaths": [
        {
          "type": "DiskDrives",
          "path": "F:\\"
        },
        {
          "type": "DiskDrives",
          "path": "G:\\"
        }
      ],
      "includedDatabases": [
        {
          "datasourceId": "2265838378698362078",
          "datasourceName": "MSSQLSERVER;user1"
        }
      ],
      "snapshotRecoveryPointInfo": {}
    },
    "recoveryPointTimeInUTC": "2024-03-13T08:03:25Z",
    "type": "SnapshotFull",
    "recoveryPointTierDetails": [
      {
        "type": "InstantRP",
        "status": "Valid"
      }
    ]
  }
}

extendedInfo.includedDatabases會識別所選快照中擷取的資料庫。 extendedInfo.dataDirectoryPaths會識別可用於進階還原組態的磁碟路徑。

取得資料庫路徑以進行進階還原設定

對於資料庫串流或快照恢復點,請取得恢復點的詳細資料:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints/{recoveryPointId}?api-version=2023-02-01

檢查 properties.extendedInfo.dataDirectoryPaths 資料與日誌檔案路徑及邏輯名稱。 例如:

{
  "dataDirectoryTimeInUTC": "2024-03-04T18:03:35Z",
  "dataDirectoryPaths": [
    {
      "type": "Data",
      "path": "F:\\DATA\\TestDB1.mdf",
      "logicalName": "TestDB1"
    },
    {
      "type": "Log",
      "path": "F:\\LOG\\TestDB1_log.ldf",
      "logicalName": "TestDB1_log"
    }
  ],
  "snapshotRecoveryPointInfo": {}
}

對於資料庫的時間點還原,列出日誌復原範圍時,請要求擴充資訊。 以下範例使用快照式恢復:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/protectionContainers/{containerName}/protectedItems/{databaseProtectedItemName}/recoveryPoints?api-version=2023-02-01&$filter=startDate eq '2024-02-06 07:50:56 AM' and endDate eq '2024-03-07 07:23:05 AM' and extendedInfo eq 'true' and restorePointQueryType eq 'Log' and recoveryMode eq 'SnapshotAttachAndRecover'

將樣本日期替換為所需的查詢視窗。 回應包含日誌恢復範圍和 properties.extendedInfo,包括 dataDirectoryTimeInUTC、、 dataDirectoryPaths和 snapshotRecoveryPointInfo。

前述查詢會擷取恢復點和還原組態中繼資料。 以下章節將說明現有的串流資料庫還原請求。

使用 REST API 的資料庫復原作業

觸發還原是 POST 要求。 若要執行此作業,請使用觸發還原 REST API。

POST https://management.azure.com/Subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/{fabricName}/protectionContainers/{containerName}/protectedItems/{protectedItemName}/recoveryPoints/{recoveryPointId}/restore?api-version=2019-05-13

{containerName} 和 {protectedItemName} 的值會在這裡設定,而 recoveryPointID 是上述復原點的 {name} 欄位。

POST https://management.azure.com/Subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerSelfHost/providers/Microsoft.RecoveryServices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0/protectedItems/sqldatabase;mssqlserver;msdb/recoveryPoints/56798287946753/restore?api-version=2019-05-13'

創建請求主體

將資料庫恢復至相同資料目錄的請求主體範例

以下請求體定義了恢復 SQL 資料庫到相同資料目錄所需的屬性。

{
   "properties":{
        "objectType":"AzureWorkloadSQLRestoreRequest",
        "shouldUseAlternateTargetLocation":false,
        "isNonRecoverable":false,
        "targetInfo":{"ContainerName": "compute;SQLServerPMDemo;sqlserver-0", "DatabaseName" : "SQLINSTANCE/msdb"},
        "alternateDirectoryPaths":null,
        "isFallbackOnDefaultDirectoryEnabled":true,
        "recoveryType":"AlternateLocation",
        "sourceResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerPMDemo/providers/Azure/virtualmachines/compute;SQLServerPMDemo;sqlserver-0"       
    }
}
將資料庫還原至替代資料目錄的要求本文範例

以下請求體定義了恢復 SQL 資料庫到相同資料目錄所需的屬性。

{
    "properties":{ 
        "objectType":"AzureWorkloadSQLRestoreRequest",
        "shouldUseAlternateTargetLocation":true,
        "isNonRecoverable":false,
        "targetInfo":
        {
            "overwriteOption":"Overwrite","containerName":"compute;oneboxrg;oneboxvm","databaseName":"SQLINSTANCE/msdb"},
            "alternateDirectoryPaths":[{"mappingType":"Log","sourcePath":"C:\\SQLfiles\\Default.ldf","targetPath":"C:\\SQLFiles\\Temp.ldf"}],
            "recoveryType":"AlternateLocation",
            "sourceResourceId":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerPMDemo/providers/Azure/virtualmachines/compute;SQLServerPMDemo;sqlserver-0"
        }
    }
}

回應

還原作業的觸發為非同步作業。 此作業會建立另一項需要個別追蹤的作業。

該作業傳回兩個回應:在建立另一項作業時傳回 202 (已接受),並在該作業完成時傳回 200 (OK)。

回應範例
Status Code:
OK

Headers:
Pragma                        : no-cache
Cache-Control                 : no-cache
Server                        : Microsoft-IIS/10.0,Microsoft-IIS/10.0
X-Content-Type-Options        : nosniff
x-ms-request-id               : f17973f5-c788-482f-8aad-6bb50e647a2e
x-ms-client-request-id        : b0356a0e-c68d-4ac2-a53f-4f546685146d,b0356a0e-c68d-4ac2-a53f-4f546685146d
X-Powered-By                  : ASP.NET
Strict-Transport-Security     : max-age=31536000; includeSubDomains
x-ms-ratelimit-remaining-subscription-resource-requests: 149
x-ms-correlation-request-id   : f17973f5-c788-482f-8aad-6bb50e647a2e
x-ms-routing-request-id       : SOUTHINDIA:20210801T104711Z:f17973f5-c788-482f-8aad-6bb50e647a2e
Date                          : Sun, 01 Aug 2021 10:47:11 GMT

{
    "id":"/Subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerSelfHost/providers/Microsoft.RecoveryServices/vaults/SQLServer2012/backupJobs/0bda1a53-73fa-427e-9a1c-72a2016adee3",
    "name": "0bda1a53-73fa-427e-9a1c-72a2016adee3",
    "type": "Microsoft.RecoveryServices/vaults/backupJobs",
    "properties": {
    "jobType": "AzureWorkloadJob",
    "actionsInfo": [
      1
    ],
    "workloadType": "SQLDataBase",
    "duration": "PT1.6543659S",
    "extendedInfo": {
      "tasksList": [
        {
          "taskId": "Transfer data from vault",
          "status": "InProgress"
        }
      ],
      "propertyBag": {
        "Job Type": "Recovery to the original database"
      }
    },
    "isUserTriggered": true,
    "entityFriendlyName": "msdb [sqlserver-0]",
    "backupManagementType": "AzureWorkload",
    "operation": "Restore",
    "status": "InProgress",
    "startTime": "2021-08-01T10:47:09.5865449Z",
    "activityId": "b0356a0e-c68d-4ac2-a53f-4f546685146d"
  }
}

跨區域還原

如果您已啟用跨區域還原,則復原點也會被複製到次要配對區域。 然後,您可以擷取這些復原點並觸發還原至位於配對區域的機器。 使用一般還原時,目標機器應註冊至次要區域中的目標保存庫。 下列步驟會說明端對端流程:

  1. 擷取已複寫到次要區域的備份項目。

    下列範例需具備以下項目:

    • 容器:VMAppContainer;Compute;SQLServerPMDemo;sqlserver-0
    • 受保護的項目:sqldatabase;mssqlserver;msdb,適用於需要還原的資料庫 (使用本文件稍早所述的步驟)。
  2. 擷取複寫至次要區域的復原點 (不同項目和/或記錄)。

  3. 選擇已註冊至次要配對區域內保存庫的目標伺服器。

  4. 觸發還原至該伺服器,並使用 JobId 來追蹤。

備註

次要區域中可供使用的備份資料 RPO 為 12 個小時。 因此,當您開啟 CRR 時,次要區域的 RPO 是 12 小時 + 記錄頻率持續時間 (可設定為至少 15 分鐘)。

從次要區域擷取相異復原點

使用清單復原點 API 來擷取次要區域中資料庫的可用復原點清單。 下列範例會套用選擇性篩選,以擷取指定時間範圍內的完整復原點和差異復原點。

GET "https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerSelfHost/providers/Microsoft.RecoveryServices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/recoveryPoints/?$filter=startDate eq'2021-07-25 08:41:32 AM' and endDate eq '2021-08-01 08:41:45 AM' and restorePointQueryType eq 'FullAndDifferential' and extendedInfo eq 'True'&api-version=2018-12-20"

擷取復原點的範例回應

Headers:
Pragma                        : no-cache
X-Content-Type-Options        : nosniff
x-ms-request-id               : 66b3fbb4-e38a-4a4b-98c7-56db66ab52e6
x-ms-client-request-id        : 35eb7834-8b5c-4a2c-adda-eee2ed02eb08,35eb7834-8b5c-4a2c-adda-eee2ed02eb08
Strict-Transport-Security     : max-age=31536000; includeSubDomains
x-ms-ratelimit-remaining-subscription-resource-requests: 149
x-ms-correlation-request-id   : 66b3fbb4-e38a-4a4b-98c7-56db66ab52e6
x-ms-routing-request-id       : SOUTHINDIA:20210801T102906Z:66b3fbb4-e38a-4a4b-98c7-56db66ab52e6
Cache-Control                 : no-cache
Date                          : Sun, 01 Aug 2021 10:29:06 GMT
Server                        : Microsoft-IIS/10.0
X-Powered-By                  : ASP.NET

Body:
{
  "value": [
    {
      "id":
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerSelfHost/providers/Microsoft.RecoveryServices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/RecoveryPoints/932604119111216382",
      "name": "932604119111216382",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLRecoveryPoint",
        "recoveryPointTimeInUTC": "2021-07-31T16:33:48Z",
        "type": "Full"
      }
    },
    {
      "id":
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerSelfHost/providers/Microsoft.RecoveryServices/vaults/SQLServer2012/backupFabrics/Azure/protectionContainers/VMAppContainer;compute;SQLServerPMDemo;sqlserver-0/protectedItems/SQLDataBase;mssqlserver;msdb/RecoveryPoints/932599942005436803",
      "name": "932599942005436803",
      "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/recoveryPoints",
      "properties": {
        "objectType": "AzureWorkloadSQLRecoveryPoint",
        "recoveryPointTimeInUTC": "2021-07-30T16:33:49Z",
        "type": "Full"
      }
    },
.....

使用上面回應中的 {name} 欄位可識別復原點。

取得存取憑證

若要執行跨區域還原,會需要以存取權杖啟用 Azure 備份服務之間的正確通訊。 若要取得存取權杖,請遵循下列步驟:

  1. 使用 Microsoft Entra 屬性 API 來對次要區域 (下列範例中的 westus) 擷取 Microsoft Entra 屬性。

    GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.RecoveryServices/locations/westus/backupAadProperties?api-version=2018-12-20
    

    傳回的回應格式如下:

    {
      "properties": {
        "tenantId": "00000000-0000-0000-0000-000000000000",
        "audience": "https://RecoveryServices/IaasCoord/aadmgmt/wus",
        "servicePrincipalObjectId": "00000000-0000-0000-0000-000000000000"
      }
    }
    
  2. 使用取得存取權杖 API 來取得存取權杖,以啟用 Azure 備份服務之間的通訊。

    POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/{fabricName}/protectionContainers/{containerName}/protectedItems/{protectedItemName}/recoveryPoints/{recoveryPointId}/accessToken?api-version=2018-12-20
    

    請將上一個步驟中 Microsoft Entra 屬性 API 所傳回的回應內容貼上到請求主體。

    傳回的回應格式如下:

    {
      "protectableObjectUniqueName": "MSSQLSERVER/model",
        "protectableObjectFriendlyName": "msdb",
        "protectableObjectWorkloadType": "SQL",
        "protectableObjectProtectionState": "Protected",
        "protectableObjectContainerHostOsName": "sqlserver-0",
        "protectableObjectParentLogicalContainerName": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerPMDemo/providers/Microsoft.Compute/virtualMachines/sqlserver-0",
        "containerId": "0000000",
        "policyName": "HourlyLogBackup",
        "policyId": "00000000-0000-0000-0000-000000000000",
        "objectType": "WorkloadCrrAccessToken",
        "accessTokenString": "<access-token-string>",
        "subscriptionId": "00000000-0000-0000-0000-000000000000",
        "resourceGroupName": "SQLServerSelfHost",
        "resourceName": "SQLServer2012",
        "resourceId": "0000000000000000000",
        "protectionContainerId": 0000000,
        "recoveryPointId": "932603497994988273",
        "recoveryPointTime": "7/31/2021 4:33:17 PM",
        "containerName": "Compute;SQLServerPMDemo;sqlserver-0",
        "containerType": "VMAppContainer",
        "backupManagementType": "AzureWorkload",
        "datasourceType": "SQLDataBase",
        "datasourceName": "msdb",
        "datasourceId": "932350676859704517",
        "datasourceContainerName": "Compute;SQLServerPMDemo;sqlserver-0",
        "coordinatorServiceStampId": "00000000-0000-0000-0000-000000000000",
        "coordinatorServiceStampUri": "https://pod01-wbcm1.eus.backup.windowsazure.com",
        "protectionServiceStampId": "00000000-0000-0000-0000-000000000000",
        "protectionServiceStampUri": "https://pod01-prot1j-int.eus.backup.windowsazure.com",
        "rpOriginalSAOption": false,
        "rpIsManagedVirtualMachine": false,
        "bMSActiveRegion": "EastUS"
    }
    

將磁碟還原至次級區域

使用跨區域還原觸發 API,將項目還原至次要地區。

POST https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.RecoveryServices/locations/{azureRegion}/backupCrossRegionRestore?api-version=2018-12-20

要求本文應該有兩個部分:

  1. crossRegionRestoreAccessDetails:貼上前一個步驟中執行的取得存取權杖 API 要求回應之屬性區塊,以填入要求本文的此區段。

  2. restoreRequest:若要填入要求本文的 restoreRequest 區段,您必須傳遞容器 (已註冊至次要區域中的保存庫) 的詳細資料,該容器為必須還原資料庫的目的地,並提供要儲存已還原資料庫的名稱。 若要將完整備份還原至次要區域,請將 AlternateLocation 指定為復原類型。

將 VM 磁碟還原至次要區域的範例請求主體如下所示:

  {
  "crossRegionRestoreAccessDetails": {
	      "protectableObjectUniqueName": "MSSQLSERVER/model",
        "protectableObjectFriendlyName": "msdb",
        "protectableObjectWorkloadType": "SQL",
        "protectableObjectProtectionState": "Protected",
        "protectableObjectContainerHostOsName": "sqlserver-0",
        "protectableObjectParentLogicalContainerName": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SQLServerPMDemo/providers/Microsoft.Compute/virtualMachines/sqlserver-0",
        "containerId": "0000000",
        "policyName": "HourlyLogBackup",
        "policyId": "00000000-0000-0000-0000-000000000000",
        "objectType": "WorkloadCrrAccessToken",
        "accessTokenString": "<access-token-string>",
        "subscriptionId": "00000000-0000-0000-0000-000000000000",
        "resourceGroupName": "SQLServerSelfHost",
        "resourceName": "SQLServer2012",
        "resourceId": "0000000000000000000",
        "protectionContainerId": 0000000,
        "recoveryPointId": "932603497994988273",
        "recoveryPointTime": "7/31/2021 4:33:17 PM",
        "containerName": "Compute;SQLServerPMDemo;sqlserver-0",
        "containerType": "VMAppContainer",
        "backupManagementType": "AzureWorkload",
        "datasourceType": "SQLDataBase",
        "datasourceName": "msdb",
        "datasourceId": "932350676859704517",
        "datasourceContainerName": "Compute;SQLServerPMDemo;sqlserver-0",
        "coordinatorServiceStampId": "00000000-0000-0000-0000-000000000000",
        "coordinatorServiceStampUri": "https://pod01-wbcm1.eus.backup.windowsazure.com",
        "protectionServiceStampId": "00000000-0000-0000-0000-000000000000",
        "protectionServiceStampUri": "https://pod01-prot1j-int.eus.backup.windowsazure.com",
        "rpOriginalSAOption": false,
        "rpIsManagedVirtualMachine": false,
        "bMSActiveRegion": "EastUS"
    },
    "restoreRequest": {
        "objectType": "AzureWorkloadSQLRestoreRequest",
        "shouldUseAlternateTargetLocation": true,
        "isNonRecoverable": false,
        "alternateDirectoryPaths": [],
        "recoveryType": "AlternateLocation",
        "sourceResourceId":"/subscriptions/600000000-0000-0000-0000-000000000000/resourceGroups/SQLServerPMDemo/providers/Microsoft.Compute/virtualMachines/sqlserver-0",
        "targetInfo": {
            "overwriteOption": "FailOnConflict",
          "containerId":"/Subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/RestoreRG/providers/Microsoft.RecoveryServices/vaults/wusRestoreVault/backupFabrics/Azure/protectionContainers/vmappcontainer;compute;restorerg;wusrestorevm",
          "databaseName": "MSSQLSERVER/msdb_restored_8_1_2021_1758"
        }
      }
  }

對受快照政策保護的 SQL 實例執行跨區域時間點還原

使用此流程還原 SQL 執行個體快照中包含的資料庫,並將其記錄備份重播至次要區域的共同時間點。

Important

所選的時間點必須落在你想從實例快照中還原的每個資料庫的記錄檔範圍內。 為 SQL 執行個體受保護項目及所選的具體快照復原點要求存取權杖。

查看實例快照與日誌範圍

  1. 列出 SQL 實例保護項目的復原點。

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{sourceVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{sourceVaultName}/backupFabrics/Azure/protectionContainers/{sourceContainerName}/protectedItems/SQLInstance%3Bmssqlserver/recoveryPoints?api-version=2023-01-15
    

    選擇一個回應包含以下所有數值的復原點:

    • properties.type 是 SnapshotFull。
    • properties.crossRegionRestoreState 是 Supported。
    • properties.recoveryPointTierDetails 包含 HardenedRP 一個狀態為 Valid的條目。
  2. 取得所選的還原點以件所其權威擴充資訊。

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{sourceVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{sourceVaultName}/backupFabrics/Azure/protectionContainers/{sourceContainerName}/protectedItems/SQLInstance%3Bmssqlserver/recoveryPoints/{instanceSnapshotRecoveryPointId}?api-version=2023-01-15
    

    用 properties.extendedInfo.includedDatabases 來識別快照中包含的子資料庫。 回應同時包含還原所需的來源磁碟路徑與快照元資料。

  3. 取得 SQL 實例保護項目並儲存 properties.protectedItemDataSourceId。

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{sourceVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{sourceVaultName}/backupFabrics/Azure/protectionContainers/{sourceContainerName}/protectedItems/SQLInstance%3Bmssqlserver?api-version=2023-01-15
    
  4. 對於每個包含的資料庫,列出與所選實例快照相關的日誌範圍。

    GET "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{sourceVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{sourceVaultName}/backupFabrics/Azure/protectionContainers/{sourceContainerName}/protectedItems/SQLDataBase%3Bmssqlserver%3B{databaseName}/recoveryPoints?api-version=2023-01-15&$filter=restorePointQueryType eq 'Log' and recoveryMode eq 'SnapshotAttachAndRecover' and baseSnapshotRPId eq '{instanceSnapshotRecoveryPointId}' and baseRPDatasourceId eq '{instanceProtectedItemDataSourceId}'"
    

    回應包含一個 DefaultRangeRecoveryPoint. 選擇一個 UTC properties.timeRanges,其落在每個正在還原的資料庫所回傳的 pointInTime 範圍內。

取得實例快照的存取權杖

取得次要區域的 Microsoft Entra 屬性。

GET "https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.RecoveryServices/locations/{secondaryRegion}/backupAadProperties?api-version=2023-01-15&$filter=backupManagementType eq 'AzureWorkload'"

當你為 SQL 實例和所選快照復原點請求存取權杖時,將回傳的 properties 物件傳入請求本文。

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{sourceVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{sourceVaultName}/backupFabrics/Azure/protectionContainers/{sourceContainerName}/protectedItems/SQLInstance%3Bmssqlserver/recoveryPoints/{instanceSnapshotRecoveryPointId}/accessToken?api-version=2023-01-15
{
  "properties": {
    "tenantId": "<tenant-id>",
    "audience": "<audience-returned-by-the-service>",
    "servicePrincipalObjectId": "<service-principal-object-id>",
    "identityVersion": 1
  }
}

觸發 SQL 執行個體時間點還原

AzureWorkloadSQLPointInTimeRestoreRequest 與 SnapshotAttachAndRecover 一起使用。 將從存取權杖回覆複製的完整properties物件貼到crossRegionRestoreAccessDetails。

POST https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.RecoveryServices/locations/{secondaryRegion}/backupCrossRegionRestore?api-version=2023-01-15
{
  "crossRegionRestoreAccessDetails": {
    "objectType": "WorkloadCrrAccessToken",
    "accessTokenString": "<access-token-string>",
    "recoveryPointId": "<instance-snapshot-recovery-point-id>",
    "datasourceType": "SQLInstance",
    "datasourceName": "mssqlserver"
  },
  "restoreRequest": {
    "objectType": "AzureWorkloadSQLPointInTimeRestoreRequest",
    "shouldUseAlternateTargetLocation": true,
    "recoveryMode": "SnapshotAttachAndRecover",
    "isNonRecoverable": false,
    "recoveryType": "AlternateLocation",
    "sourceResourceId": "/subscriptions/{subscriptionId}/resourceGroups/{sourceVmResourceGroup}/providers/Microsoft.Compute/virtualMachines/{sourceVmName}",
    "targetVirtualMachineId": "/subscriptions/{subscriptionId}/resourceGroups/{targetVmResourceGroup}/providers/Microsoft.Compute/virtualMachines/{targetVmName}",
    "targetResourceGroupName": "{targetVmResourceGroup}",
    "pointInTime": "2026-08-09T17:18:23.500Z",
    "alternateDirectoryPaths": [
      {
        "mappingType": "DiskDrives",
        "sourcePath": "F:\\",
        "targetPath": "J:\\"
      },
      {
        "mappingType": "DiskDrives",
        "sourcePath": "G:\\",
        "targetPath": "K:\\"
      }
    ],
    "targetInfo": {
      "containerId": "/subscriptions/{subscriptionId}/resourceGroups/{targetVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{targetVaultName}/backupFabrics/Azure/protectionContainers/{targetContainerName}",
      "overwriteOption": "FailOnConflict",
      "databaseName": "MSSQLSERVER/*_{databaseNameSuffix}"
    },
    "userAssignedManagedIdentityDetails": {
      "identityArmId": "/subscriptions/{subscriptionId}/resourceGroups/{identityResourceGroup}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}",
      "userAssignedIdentityProperties": {
        "clientId": "<managed-identity-client-id>",
        "principalId": "<managed-identity-principal-id>"
      }
    }
  }
}

上方簡稱的crossRegionRestoreAccessDetails物件顯示識別執行個體情境的欄位。 在實際要求中,複製所有存取權杖 API 傳回的屬性,包括存在時的tokenExtendedInformation。

對受快照政策保護的子資料庫執行跨區域日誌還原

使用此流程將一個子資料庫還原至指定時間點,當該資料庫作為 SQL 實例快照政策的一部分時。

Important

對於子系資料庫記錄還原,請在子系SQLDataBase受保護項目上要求存取權杖和DefaultRangeRecoveryPoint。 不要在父系 SQL 執行個體還原點上要求權杖。

發現子系資料庫記錄範圍

列出子資料庫保護項目的日誌恢復點。

GET "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{sourceVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{sourceVaultName}/backupFabrics/Azure/protectionContainers/{sourceContainerName}/protectedItems/SQLDataBase%3Bmssqlserver%3B{databaseName}/recoveryPoints?api-version=2023-01-15&$filter=restorePointQueryType eq 'Log'"

會副包含一個含有一或多個 UTC 範圍的 DefaultRangeRecoveryPoint:

{
  "value": [
    {
      "name": "DefaultRangeRecoveryPoint",
      "properties": {
        "objectType": "AzureWorkloadSQLPointInTimeRecoveryPoint",
        "timeRanges": [
          {
            "startTime": "2026-07-26T19:25:29.9811766Z",
            "endTime": "2026-08-10T18:31:23Z"
          }
        ],
        "type": "Log"
      }
    }
  ]
}

選擇一個回傳範圍內的UTC pointInTime 。

取得子資料庫日誌範圍的存取權杖

使用 SQL 實例區段所示的 Azure 工作負載過濾器,取得次要區域的 Microsoft Entra 屬性。 請求子資料庫存取權杖時,傳遞回傳的 properties 物件。

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{sourceVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{sourceVaultName}/backupFabrics/Azure/protectionContainers/{sourceContainerName}/protectedItems/SQLDataBase%3Bmssqlserver%3B{databaseName}/recoveryPoints/DefaultRangeRecoveryPoint/accessToken?api-version=2023-01-15

回傳的存取權杖屬性會以如下值標示所選的子資料庫:

{
  "properties": {
    "protectableObjectUniqueName": "MSSQLSERVER/{databaseName}",
    "protectableObjectFriendlyName": "{databaseName}",
    "objectType": "WorkloadCrrAccessToken",
    "accessTokenString": "<access-token-string>",
    "recoveryPointId": "DefaultRangeRecoveryPoint",
    "datasourceType": "SQLDataBase",
    "datasourceName": "{databaseName}"
  }
}

觸發子資料庫日誌還原

AzureWorkloadSQLPointInTimeRestoreRequest 與 SnapshotAttachAndRecover 一起使用。 目標資料庫名稱必須使用 MSSQLSERVER/{targetDatabaseName} 邏輯路徑格式。

POST https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.RecoveryServices/locations/{secondaryRegion}/backupCrossRegionRestore?api-version=2023-01-15
{
  "crossRegionRestoreAccessDetails": {
    "objectType": "WorkloadCrrAccessToken",
    "accessTokenString": "<access-token-string>",
    "recoveryPointId": "DefaultRangeRecoveryPoint",
    "datasourceType": "SQLDataBase",
    "datasourceName": "{databaseName}"
  },
  "restoreRequest": {
    "objectType": "AzureWorkloadSQLPointInTimeRestoreRequest",
    "shouldUseAlternateTargetLocation": true,
    "recoveryMode": "SnapshotAttachAndRecover",
    "isNonRecoverable": false,
    "recoveryType": "AlternateLocation",
    "sourceResourceId": "/subscriptions/{subscriptionId}/resourceGroups/{sourceVmResourceGroup}/providers/Microsoft.Compute/virtualMachines/{sourceVmName}",
    "targetVirtualMachineId": "/subscriptions/{subscriptionId}/resourceGroups/{targetVmResourceGroup}/providers/Microsoft.Compute/virtualMachines/{targetVmName}",
    "targetResourceGroupName": "{targetVmResourceGroup}",
    "pointInTime": "2026-08-03T06:58:26Z",
    "alternateDirectoryPaths": [
      {
        "mappingType": "DiskDrives",
        "sourcePath": "F:\\",
        "targetPath": "J:\\"
      },
      {
        "mappingType": "DiskDrives",
        "sourcePath": "G:\\",
        "targetPath": "K:\\"
      }
    ],
    "targetInfo": {
      "containerId": "/subscriptions/{subscriptionId}/resourceGroups/{targetVaultResourceGroup}/providers/Microsoft.RecoveryServices/vaults/{targetVaultName}/backupFabrics/Azure/protectionContainers/{targetContainerName}",
      "overwriteOption": "FailOnConflict",
      "databaseName": "MSSQLSERVER/{targetDatabaseName}"
    },
    "userAssignedManagedIdentityDetails": {
      "identityArmId": "/subscriptions/{subscriptionId}/resourceGroups/{identityResourceGroup}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}",
      "userAssignedIdentityProperties": {
        "clientId": "<managed-identity-client-id>",
        "principalId": "<managed-identity-principal-id>"
      }
    }
  }
}

與實例請求相同,將完整的存取權杖 properties 物件複製到 crossRegionRestoreAccessDetails。 把 accessTokenString 和 tokenExtendedInformation 當作機密資料,不要寫入日誌或包含在共享成品中。

在這兩種時間點情境中,觸發都是非同步的。 依照 Azure-AsyncOperation、 Location、 和 Retry-After 回應標頭,並在將還原視為成功前,確認結果的 Recovery Services Vault 作業的終端狀態。

下一步

使用 Azure 入口網站、 Azure CLI、 REST API 管理 Azure VM 中的 SQL Server 資料庫。

如需 Azure 備份 REST API 的詳細資訊,請參閱下列文件: