使用 CLI 从快照创建托管磁盘
此脚本从快照创建托管磁盘。 使用它从 OS 和数据磁盘的快照还原虚拟机。 从各自的快照创建 OS 和数据托管磁盘,然后通过附加托管磁盘创建新的虚拟机。 还可以通过附加从快照创建的数据磁盘还原现有 VM 的数据磁盘。
若要运行此示例,请确保已安装最新的 Azure CLI 2.0。 若要开始,请运行 az login
以创建与 Azure 的连接。
Note
在运行 az login
之前,请先运行 az cloud set -n AzureChinaCloud
以更改云环境。 如果要切换回全球 Azure,请再次运行 az cloud set -n AzureCloud
。
此示例在 Bash shell 中正常工作。 有关在 Windows 客户端上运行 Azure CLI 脚本的选项,请参阅在 Windows 中运行 Azure CLI。
Note
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
示例脚本
#Provide the subscription Id of the subscription where you want to create Managed Disks
subscriptionId=<your_subscription_id>
#Provide the name of your resource group
resourceGroupName=myResourceGroupName
#Provide the name of the snapshot that will be used to create Managed Disks
snapshotName=mySnapshotName
#Provide the name of the new Managed Disks that will be create
diskName=myDiskName
#Provide the size of the disks in GB. It should be greater than the VHD file size.
diskSize=128
#Provide the storage type for Managed Disk. Premium_LRS or Standard_LRS.
storageType=Premium_LRS
#Set the context to the subscription Id where Managed Disk will be created
az account set --subscription $subscriptionId
#Get the snapshot Id
snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)
#Create a new Managed Disks using the snapshot Id
#Note that managed disk will be created in the same location as the snapshot
az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --size-gb $diskSize --source $snapshotId
脚本说明
此脚本使用以下命令从快照创建托管磁盘。 表中的每条命令均链接到特定于命令的文档。
命令 | 说明 |
---|---|
az snapshot show | 使用快照的名称和资源组属性获取该快照的所有属性。 使用 ID 属性创建托管磁盘。 |
az disk create | 使用托管快照的快照 ID 创建托管磁盘 |
后续步骤
有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档。
可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。