Azure – IAAS VM + SQL export snapshot disks

Hi there

In this article I’m covering a need that my client had when his IAAS SQL VM was ready for Dev, UAT environment and he didn’t want to use Azure backup. He asked for the VMs disks to be exported to VHD.

  1. Shutdown the VM
  2. Dealocated the VM
  3. go to VM \ Settings \ Disks
  4. on each disk do the following
  5. select the disk you want to snapshot it
  6. then click “+” create snapshot
  7. go back to the VM resource group
  8. filter items by snapshot (unselect all unneeded resources)

to export the disk or even download them do the following

create a container on an available storage account on Azure (I used a different storage account for this exercise once I didn’t want unauthorized access to my production storage accounts)

  1. create a new storage account
  2. create a container under this storage account
  3. select the storage account \ overview \ containers
  4. “+” container to create a new container
  5. give it a name and select “Container (anonymous read access for container and blob)” at Public access level
  6. click create

Now use the following script to copy one exported vhd (snapshot) to the container you created

#Provide the subscription Id of the subscription where snapshot is created
$subscriptionId = "13a058b9-c7a4-4ba8-bca7-80743571b1ce"

#Provide the name of your resource group where snapshot is created
$resourceGroupName ="Callisto"

#Provide the snapshot name
$snapshotName = "sqldisk1"


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

#Provide storage account name where you want to copy the snapshot.
$storageAccountName = "thiagobeierblog"

#Name of the storage container where the downloaded snapshot will be stored
$storageContainerName = "callisto"

#Provide the key of the storage account where you want to copy snapshot.
$storageAccountKey = 'ntFXNy1Hzjus7IPcKYdmTxghmzj3hnxmQFEIPYUeWLWrjFP4KShmSvYMhT/WN2YC+zsK/m2Tg3x0KOMHvtReOg=='

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

# 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

You can use this one to copy as many VHDs (snapshots) you have created

#Provide the subscription Id of the subscription where snapshot is created
$subscriptionId = "13a058b9-c7a4-4ba8-bca7-80743571b1ce"

#Provide the name of your resource group where snapshot is created
$resourceGroupName ="Callisto"

#Provide the snapshot name
$snapshotName = "sqldisk1"
$snapshotName1 = "sqldisk2"

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

#Provide storage account name where you want to copy the snapshot.
$storageAccountName = "thiagobeierblog"

#Name of the storage container where the downloaded snapshot will be stored
$storageContainerName = "callisto"

#Provide the key of the storage account where you want to copy snapshot.
$storageAccountKey = 'ntFXNy1Hzjus7IPcKYdmTxghmzj3hnxmQFEIPYUeWLWrjFP4KShmSvYMhT/WN2YC+zsK/m2Tg3x0KOMHvtReOg=='

#Provide the name of the VHD file to which snapshot will be copied.
$destinationVHDFileName = "vhd1"
$destinationVHDFileName1 = "vhd2"


# 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
$sas = Grant-AzSnapshotAccess -ResourceGroupName $ResourceGroupName -SnapshotName $SnapshotName1Ā  -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
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName1

You can now download the vhd files reaching the Blob storage URL

Check my Github repository

References

https://docs.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-windows-powershell-sample-copy-snapshot-to-storage-account

Thanks,

Thiago Beier
TwitterLinkedInFacebookRSS