您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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

此脚本将托管快照导出到不同区域中的存储帐户。This script exports a managed snapshot to a storage account in different region. 它首先生成快照的 SAS URI,然后使用该 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 CLITo run this sample, make sure you have installed the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

此示例在 Bash shell 中正常工作。This sample works in a Bash shell. 有关在 Windows 客户端上运行 Azure CLI 脚本的选项,请参阅在 Windows 上安装 Azure CLIFor options on running Azure CLI scripts on Windows client, see Install the Azure CLI on Windows.

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

示例脚本Sample script

#Provide the subscription Id where snapshot is created
subscriptionId=dd80b94e-0463-4a65-8d04-c94f403879dc

#Provide the name of your resource group where snapshot is created
resourceGroupName=myResourceGroupName

#Provide the snapshot name 
snapshotName=mySnapshotName

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

#Provide storage account name where you want to copy the snapshot. 
storageAccountName=mystorageaccountname

#Name of the storage container where the downloaded snapshot will be stored
storageContainerName=mystoragecontainername

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

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

az account set --subscription $subscriptionId

sas=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)

az storage blob copy start --destination-blob $destinationVHDFileName --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas





脚本说明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
az snapshot grant-accessaz snapshot grant-access 生成只读 SAS,使用该 SAS 可以将基础 VHD 文件复制到存储帐户或将其下载到本地Generates read-only SAS that is used to copy underlying VHD file to a storage account or download it to on-premises
az storage blob copy startaz storage blob copy start 将 blob 从一个存储帐户异步复制到另一个存储帐户Copies a blob asynchronously from one storage account to another

后续步骤Next steps

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

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

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

可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.