Forum Discussion

Vinoth_Azure's avatar
Aug 20, 2019

Azure VM From Region A to Region B by Azure CLI

Azure VM From Region A to Region B by Azure CLI By Darwin Vinoth ☁☁, MCT- Azure Administrator

For some reasons, it might be useful to move or copy an Azure VM to a different region within Microsoft Azure. If the User ask to move VM due to latency or Backup the VM.  One might think this is a big thing (which might become even bigger if the source VM uses „Managed Disks“), but in fact it is just a „Copypaste by Azure Blob Copy “ – if you know how to do it.

Even simpler in Azure CLI writing short code to run the copy paste. Lets do that,

 

Resource Used :

 

 

Azure CLI(MY favorite)

Virtual Machine

Managed Disks

Storage Account

Azure Blob Storage

Azure Blob Copy

 

 

 

 

Steps followed:

  1. Create Disk Snapshots of the Source VM
  1. Creating Destination Storage Container
  2. Copy the Snapshots to a Destination Region-based Storage Account Container
  3. Create a managed disk from VHD file 
  4. Create a New VM within the New Resource Group and Attach the Recovered as Disks to the New VM

 

Source VM Details:

   Name: Source-VM
   Resource Group: VMReplicate-Source
   Location: East US
   Data Disk Name: Source-VM_DataDisk_0

Target VM Details:

   Name: Destination-VM
   Resource Group: VMReplicate-Destination
   Location: (Asia Pacific) South India
   Data Disk Name: Source-VM_DataDisk_0

  Storage Account Name: destinationcontainer

1.Create Disk Snapshots of the Source VM:

To Create a snapshot of Data disk we have to run following Azure Cl commands,

$az snapshot create -g VMReplicate-Source -n SourceDataDisk --source Source-      
      VM_DataDisk_0

 

 

To create a snapshot of OS-Disk run following CLI commands,

$osDiskId=$(az vm show \

   -g VMReplicate-Source \

   -n Source-VM \

   --query "storageProfile.osDisk.managedDisk.id" \

   -o tsv)

$az snapshot create -g VMReplicate-Source --source "$osDiskId" --name osDisk-backup

To Get the active snapshot list,

$az snapshot list -g VMReplicate-Source

 

  1. Create a Storage account and Blob Container Destination Region:

$az storage account create \

    --name destinationcontainer \

    --resource-group VMReplicate-Destination \

    --sku Standard_LRS

 



Specify storage account credentials

$az storage account keys list \

    --account-name destinationcontainer \

    --resource-group VMReplicate-Destination\

    --output table

 

 

Setting Storage account environment variable:

$export AZURE_STORAGE_ACCOUNT="destinationcontainer"

$export AZURE_STORAGE_KEY="4EIYRrgMhvE0ZsV+icrp9WuScXVvk5QdKBDdfRuNyT9xBTqZxYfwgZ9F8wfVKIrsVa7suUY+TUExJrT9uFA24Q=="

 

Blob Creation on destination storage account:

$az storage container create --name destinationsnapshotblob

 

 

 

  1. Copy the Snapshots to a Destination Region-based Storage Account Container

 

 

#Provide the subscription Id where snapshot is created

$subscriptionId=fd7d53ef-e290-4ab1-937e-fec061c00132

 

#Provide the name of your resource group where the snapshot is created

$resourceGroupName=VMReplicate-Source

 

#Provide the snapshot name

$snapshotName1=osDisk-backup

$snapshotName2=SourceDataDisk

 

 

#Provide storage account name where you want to copy the snapshot.

$storageAccountName=destinationcontainer

 

#Name of the storage container where the downloaded snapshot will be stored

$storageContainerName=destinationsnapshotblob

 

#Provide the key of the storage account where you want to copy snapshot.

$storageAccountKey=4EIYRrgMhvE0ZsV+icrp9WuScXVvk5QdKBDdfRuNyT9xBTqZxYfwgZ9F8wfVKIrsVa7suUY+TUExJrT9uFA24Q==

 

#Provide the name of the VHD file to which snapshot will be copied.

$destinationVHDFileName2=osdisk.vhd

$destinationVHDFileName3=datadisk.vhd

 

$az account set --subscription $subscriptionId

 

$sas1=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName1 --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)

 

$sas2=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName2 --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)

 

$az storage blob copy start --destination-blob $destinationVHDFileName2 --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas1

 

$az storage blob copy start --destination-blob $destinationVHDFileName3 --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas2

 

  1. Create a managed disk from VHD file

OS DISK:

#Provide the subscription Id

$subscriptionId=fd7d53ef-e290-4ab1-937e-fec061c00132

 

#Provide the name of your resource group.

#Ensure that the resource group is already created

$resourceGroupName=VMReplicate-Destination

 

#Provide the name of the Managed Disk

$diskName=OS-DiskRecovered

 

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

$diskSize=256

 

 

#Provide the URI of the VHD file that will be used to create Managed Disk.

# VHD file can be deleted as soon as Managed Disk is created.

# e.g. https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd

$vhdUri=https://destinationcontainer.blob.core.windows.net/destinationsnapshotblob/osdisk.vhd

 

#Provide the storage type for the Managed Disk. Premium_LRS or Standard_LRS.

$storageType=Standard_LRS

 

 

#Provide the Azure location (e.g. west us) where Managed Disk will be located.

#The location should be the same as the location of the storage account where the VHD file is stored.

#Get all the Azure location supported for your subscription using the command below:

#az account list-locations

$location=(Asia Pacific) South India

 

#Set the context to the subscription Id where Managed Disk will be created

az account set --subscription $subscriptionId

 

#Create the Managed disk from the VHD file

az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --location $location --size-gb $diskSize --source $vhdUri

Data Disk:

 

#Provide the name of your resource group.

#Ensure that resource group is already created

resourceGroupName=VMReplicate-Destination

 

#Provide the name of the Managed Disk

diskName1=Data-DiskRecovered

 

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

diskSize1=128

 

 

#Provide the URI of the VHD file that will be used to create Managed Disk.

# VHD file can be deleted as soon as Managed Disk is created.

# e.g. https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd

vhdUri1=https://destinationcontainer.blob.core.windows.net/destinationsnapshotblob/datadisk.vhd

#Provide the storage type for the Managed Disk. Premium_LRS or Standard_LRS.

storageType1=Standard_LRS

#Provide the Azure location (e.g. west us) where Managed Disk will be located.

#The location should be the same as the location of the storage account where the VHD file is stored.

#Get all the Azure location supported for your subscription using the command below:

#az account list-locations

location=SouthIndia

#Set the context to the subscription Id where Managed Disk will be created

az account set --subscription $subscriptionId

#Create the Managed disk from the VHD file

az disk create --resource-group $resourceGroupName --name $diskName1 --sku $storageType1 --location $location --size-gb $diskSize1 --source $vhdUri1

Creating in GUI:

  1. Create a New VM within the New Resource Group and Attach the Snapshots as Disks to the New VM

$az vm create -g VMReplicate-Destination -n recoveredvm --attach-os-disk OS-DiskRecovered--os-type linux

 

 

Conclusion: We have moved the VM from the source region to destination VM by using the Snapshot Option  

 

No RepliesBe the first to reply

Resources