使用 PowerShell 将托管快照作为 VHD 导出/复制到不同区域中的存储帐户Export/Copy managed snapshots as VHD to a storage account in different region with PowerShell

此脚本将托管快照导出到不同区域中的存储帐户。This script exports a managed snapshot to a storage account in different region. 它首先会生成快照的 SAS URI,然后使用它将快照复制到不同区域中的存储帐户。It first generates the SAS URI of the snapshot and then uses it to copy it to a storage account in different region. 使用此脚本将托管磁盘的备份保留在灾难恢复的不同区域。Use this script to maintain backup of your managed disks in different region for disaster recovery.

必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell 模块,然后运行 Connect-AzAccount -Environment AzureChinaCloud 创建与 Azure 的连接。If needed, install the Azure PowerShell module using the instructions found in the Azure PowerShell guide, and then run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure. 此外,用户配置文件的 .ssh 目录中需具备名为 id_rsa.pub 的 SSH 公钥。Also, you need to have an SSH public key named id_rsa.pub in the .ssh directory of your user profile.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

示例脚本Sample script

# Sign-in the Azure China Cloud
Connect-AzAccount -Environment AzureChinaCloud

#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.
$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

脚本说明Script explanation

此脚本使用以下命令为托管快照生成 SAS URI,并使用 SAS URI 将快照复制到存储帐户。This script uses following commands to generate SAS URI for a managed snapshot and copies the snapshot to a storage account using SAS URI. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
Grant-AzSnapshotAccessGrant-AzSnapshotAccess 生成快照的 SAS URI,用于将快照复制到存储帐户。Generates SAS URI for a snapshot that is used to copy it to a storage account.
New-AzureStorageContext -Environment AzureChinaCloudNew-AzureStorageContext -Environment AzureChinaCloud 使用帐户名和密钥创建存储帐户上下文。Creates a storage account context using the account name and key. 此上下文可用于对存储帐户执行读/写操作。This context can be used to perform read/write operations on the storage account.
Start-AzureStorageBlobCopyStart-AzureStorageBlobCopy 将快照的基础 VHD 复制到存储帐户Copies the underlying VHD of a snapshot to a storage account

后续步骤Next steps

从 VHD 创建托管磁盘Create a managed disk from a VHD

从托管磁盘创建虚拟机Create a virtual machine from a managed disk

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 PowerShell 脚本示例。Additional virtual machine PowerShell script samples can be found in the Azure Linux VM documentation.