使用 PowerShell 将托管快照作为 VHD 导出/复制到不同区域中的存储帐户

此脚本将托管快照导出到不同区域中的存储帐户。 它首先会生成快照的 SAS URI,然后使用它将快照复制到不同区域中的存储帐户。 使用此脚本将托管磁盘的备份保留在灾难恢复的不同区域。

必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell 模块,然后运行 Connect-AzAccount -Environment AzureChinaCloud 创建与 Azure 的连接。 此外,用户配置文件的 .ssh 目录中需具备名为 id_rsa.pub 的 SSH 公钥。

如果没有 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://learn.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 将快照复制到存储帐户。 表中的每条命令均链接到特定于命令的文档。

命令 说明
Grant-AzSnapshotAccess 生成快照的 SAS URI,用于将快照复制到存储帐户。
New-AzureStorageContext 使用帐户名和密钥创建存储帐户上下文。 此上下文可用于对存储帐户执行读/写操作。
Start-AzureStorageBlobCopy 将快照的基础 VHD 复制到存储帐户

后续步骤

从 VHD 创建托管磁盘

从托管磁盘创建虚拟机

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档

可以在 Azure Linux VM 文档中找到其他虚拟机 PowerShell 脚本示例。