In the last post, I showed how to replace disks in Azure Virtual Machines using the PowerShell script. You only need a disk image and you can quickly perform the replacement. Today’s post will complement the previous one and will show you how to create a disk image and move it to any location (region). We will take a snapshot of the VM (virtual machine) disk and move it to another region.
Snapshot
Snapshot, that is, simply speaking, a copy of the disk made at a given moment. You can do it at any time, even using the portal, finding the disk and clicking on ‘Create snapshot‘.
Now the stairs are just beginning. You can create a snapshot by Azure portal only in the subscription in which your disk is located, and only in the region where it is located. This has the consequence that you will not be able to use it so easily in a different location (region) or subscription.
When you create snapshot, you will not be able to transfer it via the portal to other region.
However, not all is lost. As long as you cannot easily transfer your snapshot using the portal, it is not a problem using PowerShell.
Copying the snapshot
First you need to have a disk snapshot created, this can be done via the portal as shown in the example above. When you have created a snapshot, you can log in to your Azure subscription from your computer using PowerShell:
Connect-AzAccount
You can also use Cloud Shell:
Before running the script below, you need to correct the variable names in it.
Enter the name of the snapshot you want to move and its resource group.
The destination will be Storage Account. We give its name, the name of the BLOB to which the disk image will go, and the name of the resource group in which it is located. Finally, the name of the created disk image.
If you do not have a Storage Account created in the target location, you need to create it.
# SOURCE
$SnapshotResourceGroup = "ResourceGroupNAME1"
$SnapshotName = "SnapshotName"
# DESTINATION
$StorageAccount = "StorageAccountNAME"
$StorageAccountBlob = "StorageAccountBlobNAME"
$storageaccountResourceGroup = "ResourceGroupNAME2"
$vhdname = "disk_image_name"
#SA_KEY
$StorageAccountKey = (Get-AzStorageAccountKey -Name $StorageAccount -ResourceGroupName $StorageAccountResourceGroup).value[0]
$snapshot = Get-AzSnapshot -ResourceGroupName $SnapshotResourceGroup -SnapshotName $SnapshotName
#GRANTING ACCESS
$snapshotaccess = Grant-AzSnapshotAccess -ResourceGroupName $SnapshotResourceGroup -SnapshotName $SnapshotName -DurationInSecond 3600 -Access Read -ErrorAction stop
$DestStorageContext = New-AzStorageContext –StorageAccountName $storageaccount -StorageAccountKey $StorageAccountKey -ErrorAction stop
Write-Output "START COPY"
Start-AzStorageBlobCopy -AbsoluteUri $snapshotaccess.AccessSAS -DestContainer $StorageAccountBlob -DestContext $DestStorageContext -DestBlob "$($vhdname).vhd" -Force -ErrorAction stop
Write-Output "END COPY"
Create a disk
When you have move the snapshot to a new location (region), you can easily create a disk using the portal. When you creating a disk as a source, select “Storage blob” and select the image you have just created. Please note that the created disk cannot be smaller than a snapshot.
You can learn how to replace the disk itself in a virtual machine from the previous article: https://lepczynski.it/en/azure_en/swap-the-os-disk-of-an-azure-vm/
Of course, you can automate everything. I gave a simple example that you can move a snapshot to another region. You might as well create a more complicated script that, after specifying the source disk name, resource group, and target location:
- create a Storage Account in the target location,
- create a snapshot,
- move the snapshot to the target location,
- create a snapshot disk at the target location,
- delete unnecessary snapshot,
- remove unnecessary Storage Account.
It all depends mainly on our ingenuity, remember that. If you made it to the end and would like more advanced stuff, leave comments and I will try to do a more advanced post.
Another way to move the disk
By the way, there are other ways that you can move the drive to another region without even using PowerShell. Assuming that we have a VM in region A that we want to copy to region B, we can, for example … Create a new resource group in region A. Copy the VM to it and move the entire resource group (containing a copy of our VM) to a new location, region B. This operation can be performed entirely via the portal, but for me it is more time consuming and I do not use it;)
Moving Azure VMs to another Azure region: link with documentation.
Thank you, this worked like a charm
Great, I’m glad I could help 🙂
It’s a shame that we can’t view the copy status. Can you provide an update to that?
Tks.
Hi, I wanted to simplify the command and I didn’t need the status. If you want to check the status you can use
Get-AzStorageBlobCopyState -Blob “ContosoPlanning2015” -Container “ContosoUploads”
here is the link for documentation:
https://docs.microsoft.com/en-us/powershell/module/az.storage/get-azstorageblobcopystate?view=azps-5.9.0
This worked for a 30gb snapshot file I had, but a larger one (250gb) failed.
Maybe the connection was unstable or it was broken. It also worked for me with disks larger than 30gb.
When I have more free time, I will check the transfer of larger files, maybe something has changed.
the credentials per the script are only valid for 3600s which probably isnt long enough to copy 250GB across regions. try bumping it up to a few hours and try the copy again!
To view the copy status based on the original code provided:
Get-AzureStorageBlobCopyState -Container $StorageAccountBlob -Blob “$($vhdname).vhd” -Context $DestStorageContext -WaitForComplete
Good Job, Thanks Aaron!
Nailed it! Worked like a charm. Thank you.
Thanks ! this worked fine 🙂
to check the status of the copy you may modify the code like this :
Write-Output “START COPY”
$destBlob = Start-AzStorageBlobCopy -AbsoluteUri $snapshotaccess.AccessSAS -DestContainer $StorageAccountBlob -DestContext $DestStorageContext -DestBlob “$($vhdname).vhd” -Force -ErrorAction stop
while (($destBlob | Get-AzStorageBlobCopyState).status -eq “Pending”) {
Write-Host “Copy pending”
Start-Sleep 30
}
Write-Output “END COPY”
It looks nice 🙂 Thanks Omar!
It worked without any errors, thanks a lot for your effort.
No problem, I’m glad I could help
Does this work for a full snapshot or only for incremental snapshots
Snapshots are saved incrementally. When you use a snapshot, you use all the data. If you need detailed information, please refer to the documentation.
worked for me, cheers 🙂
Great help… managed to copy the snapshot over to the newly created Storage Account in the required Region and create the disk.
Every day is a learning day! 🙂
Comments are closed.