Friday, May 6, 2016

Azure: Migrating VMs

This post says about how do you migrate Azure VM's from one region to other region OR move one VM from one storage account to another storage account.

Following are the steps needed: 

1. Powershell

Open an Azure Management Powershell with Admin privileges

2. Connect to tennant

2. Log on to Azure in the PowerShell window by typing the following command
  Add-AzureAccount

3. Select the subscription of the source VM

  Select-AzureSubscription -Name "BizSpark Plus"

4. Get details for source VM

$disk = Get-AzureDisk | Where-Object { $_.AttachedTo.RoleName -eq "VM_NAME" }  #VM_NAME is the source VM
$mediaLink = $disk.MediaLink
echo $mediaLink  # Get the storage name from here e.g. by copying the storage name before the first "." e.g. portalvhdsgp72f86dj50g3
The above step would give you the storage account. The log on to the portal, get the storage , key as shown below.
You would need to spend about 10 min in getting these details. 












5. Source VM commands

From the above screenshots, we can get the values of Storage Account Name, Source Key (master key), Container and Virtual Hard Disk(vhd).
$sourceStorageAccountName = "portalvhdsgp72f86dj50g5"
$sourceKey = "d/MxXjv3SMw4TP+zSSYMJsSUHOtgBKLmFmouJgNFRYkhGVf742vdPzmYAXQW4cBJwWlccgkOtV3PCiGZEU3nAw=="
$sourceContainer ="vhds"
$sourceContext = New-AzureStorageContext –StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceKey
$blobName = "VM_Name.vhd" 

 If you want to put your destination VM or disk under a particular storage account follow step 4 to get the details of the storage account, container, and key.
If you need to create a new storage, container then create it and note down its values.

6. Destination VM commands

If the subscription to the destination VM is same, then dont select the subscription else you would need to select azure subscription
$destinationStorageAccountName = "portalvhds56k8sxsm0mns9"
$destinationKey = "uKFhc8ebZ6kA2Z4FxWNXQsWfa7k8GVKnVHs+B/FzbGM4/Jbo9NJVyz+V0+5/YaUUkQ9O0Fdb/oxpV+3KknhIVg=="
$destinationContext = New-AzureStorageContext –StorageAccountName $destinationStorageAccountName -StorageAccountKey $destinationKey   
$destinationContainerName = "migratedvhds"

6. Copy the blob now from source to target

$blobCopy = Start-AzureStorageBlobCopy -DestContainer $destinationContainerName -DestContext $destinationContext   -SrcBlob $blobName  -Context $sourceContext  -
SrcContainer $sourceContainer

7. Check the status of blog copy.

The blob copy may take up to few hours depending on the size of the blog e.g. I have seen 1 TB of data disk to be moved takes 
~4 hours
$blobCopy | Get-AzureStorageBlobCopyState   #to check the status
Once the blog gets completely copied, the next step is to create an Azure disk as shown below. If you need to use new Azure Portal, search "OS Disks" and create one.
   
Use the VHD URL explorer to select the blob from the destination container that was copied earlier. If you migrating the VM please select “The VHD contains an 
operating system.” If you are migrating data disk uncheck the previous check box. 
After the disk is created, if the disk created is of type "operating system" then create a new VM.
  
When creating the VM select the disk which just was created. This would ensure you have the same copy of the virtual machine as of the source. 

If the disk created above is a data disk, then add this disk to an existing VM using "Add Disks" option.

8. Clean up

Last and final step is clean up the source VM's, disks and blobs. 
Detach the disks you attached.
Delete the VMs.

No comments: