Create a managed disk from a snapshot with CLI (Linux)

This script creates a managed disk from a snapshot. Use it to restore a virtual machine from snapshots of OS and data disks. Create OS and data managed disks from respective snapshots and then create a new virtual machine by attaching managed disks. You can also restore data disks of an existing VM by attaching data disks created from snapshots.

To run this sample, install the latest version of the Azure CLI. To start, run az login to create a connection with Azure.

Samples for the Azure CLI are written for the bash shell. To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

If you don't have an Azure subscription, create a free account before you begin.

Sample script

#Provide the subscription Id of the subscription where you want to create Managed Disks

#Provide the name of your resource group

#Provide the name of the snapshot that will be used to create Managed Disks

#Provide the name of the new Managed Disks that will be create

#Provide the size of the disks in GB. It should be greater than the VHD file size.

#Provide the storage type for Managed Disk. Premium_LRS or Standard_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

Script explanation

This script uses following commands to create a managed disk from a snapshot. Each command in the table links to command specific documentation.

Command Notes
az snapshot show Gets all the properties of a snapshot using the name and resource group properties of the snapshot. Id property is used to create managed disk.
az disk create Creates a managed disk using snapshot Id of a managed snapshot

Next steps

Create a virtual machine by attaching a managed disk as OS disk

For more information on the Azure CLI, see Azure CLI documentation.

Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.