使用 PowerShell 將受控快照集匯出/複製到不同區域的儲存體帳戶當做 VHD

此指令碼會將受控快照集匯出到不同區域的儲存體帳戶。 它會先產生快照集的 SAS URI,然後用它來將快照集複製到不同區域的儲存體帳戶。 使用此指令碼維護不同區域中受控磁碟的備份,以進行災害復原。

您可以視需要使用 Azure PowerShell 指南 (英文) 中的指示來安裝 Azure PowerShell 模組,然後執行 Connect-AzAccount 來建立與 Azure 的連線。 此外,您在使用者設定檔的 .ssh 目錄中需要有一個名為 id_rsa.pub 的 SSH 公開金鑰。

如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

範例指令碼

#Provide the subscription Id of the subscription where snapshot is created
$subscriptionId = "yourSubscriptionId"

#Provide the name of your resource group where snapshot is created
$resourceGroupName ="yourResourceGroupName"

#Provide the snapshot name 
$snapshotName = "yourSnapshotName"

#Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
#Know more about SAS here: https://docs.microsoft.com/en-us/Az.Storage/storage-dotnet-shared-access-signature-part-1
$sasExpiryDuration = "3600"

#Provide storage account name where you want to copy the snapshot. 
$storageAccountName = "yourstorageaccountName"

#Name of the storage container where the downloaded snapshot will be stored
$storageContainerName = "yourstoragecontainername"

#Provide the key of the storage account where you want to copy snapshot. 
$storageAccountKey = 'yourStorageAccountKey'

#Provide the name of the VHD file to which snapshot will be copied.
$destinationVHDFileName = "yourvhdfilename"


# Set the context to the subscription Id where Snapshot is created
Select-AzSubscription -SubscriptionId $SubscriptionId

#Generate the SAS for the snapshot 
$sas = Grant-AzSnapshotAccess -ResourceGroupName $ResourceGroupName -SnapshotName $SnapshotName  -DurationInSecond $sasExpiryDuration -Access Read
#Create the context for the storage account which will be used to copy snapshot to the storage account 
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

#Copy the snapshot to the storage account 
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName

指令碼說明

此指令碼會使用下列命令來產生受控快照集的 SAS URI,並使用 SAS URI 將快照集複製到儲存體帳戶。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
Grant-AzSnapshotAccess 產生快照集的 SAS URI 並用它來將快照集複製到儲存體帳戶。
New-AzureStorageContext 使用帳戶名稱與金鑰建立儲存體帳戶內容。 此內容可用來對儲存體帳戶執行讀取/寫入作業。
Start-AzureStorageBlobCopy 將快照集的底層 VHD 複製到儲存體帳戶

後續步驟

從 VHD 建立受控磁碟

從受控磁碟建立虛擬機器

如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件

您可以在 Azure Linux VM 文件中找到其他的虛擬機器 PowerShell 指令碼範例。