使用 Azure CLI 将托管磁盘导出/复制到存储帐户Export/Copy a managed disk to a storage account using the Azure CLI

此脚本将托管磁盘的基础 VHD 导出到相同或不同区域中的存储帐户。This script exports the underlying VHD of a managed disk to a storage account in same or different region. 它首先生成托管磁盘的 SAS URI,然后使用该 SAS URI 将 VHD 复制到存储帐户。It first generates the SAS URI of the managed disk and then uses it to copy the VHD to a storage account. 使用此脚本将托管磁盘复制到另一区域以进行区域扩展。Use this script to copy managed disks to another region for regional expansion. 若要在 Azure 市场中发布托管磁盘的 VHD 文件,可以使用此脚本将 VHD 文件复制到存储帐户,然后生成已复制的 VHD 的 SAS URI,以便在市场中发布。If you want to publish the VHD file of a managed disk in Azure Marketplace, you can use this script to copy the VHD file to a storage account and then generate a SAS URI of the copied VHD to publish it in the Marketplace.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

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

示例脚本Sample script

#Provide the subscription Id where managed disk is created
subscriptionId=yourSubscriptionId

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

#Provide the managed disk name 
diskName=myDiskName

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

#Provide storage account name where you want to copy the underlying VHD file of the managed disk. 
storageAccountName=mystorageaccountname

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

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

#Provide the name of the destination VHD file to which the VHD of the managed disk will be copied.
destinationVHDFileName=myvhdfilename.vhd

az account set --subscription $subscriptionId

sas=$(az disk grant-access --resource-group $resourceGroupName --name $diskName --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 将基础 VHD 复制到存储帐户。This script uses following commands to generate the SAS URI for a managed disk and copies the underlying VHD to a storage account using the SAS URI. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
az disk grant-accessaz disk grant-access 生成只读 SAS,使用该 SAS 可以将基础 VHD 文件复制到存储帐户或将其下载到本地Generates read-only SAS that is used to copy the 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.