Move/Copy Snapshot from one region to another

We all know we have ASR to move VM from one region to another but there are situations where we have to use manual approach via PS to move the snapshot from one region to another into a VHD and create either snapshot or disk or VM eventually with the help of that. In the previous post we have already discussed how to do the last part i mentioned .i.e creation of disk from VHD and VM from Disk and in today's post we are discussing copying snapshot from one region to another into a VHD.
Below PS Code would help you -


$ResourceGroupName = "RG"
$SnapshotName = "snapshotname"
$sasExpiryDuration = "15000"
$storageAccountName = "destinationstorageaccountname"
$storageAccountKey = "Accesskey"
$storageContainerName = "containername"
$destinationVHDFileName = "Vhdname"

#create the sas token to access 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

Get-AzStorageBlobCopyState -Context $destinationContext -Blob $destinationVHDFileName -Container $storageContainerName

Now get-azstorageblobcopystate will give you the status of copy , it will give you pending untill copying is done and then success. Once VHD file got created at the destination , you can use that to create managed disk with the help of below code -

# create manage disk from VHD

$storageType = 'Premium_LRS'
$location = "Central Us"
$storageAccountId = "storageaccountid"
$sourceVHDURI = "VHDURI"
$resourceGroupName = 'RG'
$diskName = 'diskname'

$diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Import -StorageAccountId $storageAccountId -SourceUri $sourceVHDURI -OsType Windows
Â
New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName

# Creating VM from managed disk

This part you can easily perform from Portal as you would have an option of create VM on the newly created disk overview however if yo wanna use PS then you can get code here :-

https://pachehra.blogspot.com/2019/06/create-vm-grayed-out.html


No comments:

Post a Comment

Azure DevOps

Azure DevOps is a suite of development tools provided by Microsoft, designed to support the entire development lifecycle of a software proje...