Search This Blog

Friday, January 16, 2015

Backup Azure Virtual Machine By Using Blob Snapshot


We have setup Virtual Machine Azure. Next question you may wonder on how to backup Azure Virtual Machine. Too bad at this moment, we cannot using System Center Data Protection Manager or Azure Backup to backup virtual machine. However can use these backup to backup application data.

Then we have search around and found out that we can use Blob Snapshot to perform backup by referring to Keith Mayer guide. You can refer to here.

There are four (4) steps that you need to in order to backup Azure Virtual Machine.

  • Select virtual machine to back up
  • Identify virtual hard disks
  • Create cloud storage container for storing backups
  • Back up virtual machines in Windows Azure to cloud storage
  • Backup VM Configuration by using Export-AzureVM

Well, we follow the guide provide by Keith Mayer and summarize it in this script:-

#to list down entire VM in Azure

#Select 1 VM. Example: MS4UDir
$VM = Get-AzureVM -ServiceName "MS4UDIR" -Name "MS4UDIR"

#Temporary shutdown to a state when the virtual machine is not running but its configuration  is kept in a provisioned state
$VM | Stop-AzureVM -StayProvisioned

#to get os disk

$VMOsDisk = $VM | Get-AzureOSDisk

#to get data diskname and media link
$VMDataDisks = $VM | Get-AzureDataDisk

#determine the name of Windows Azure Storage Account by using MediaLink property
$storageAccountName= $vmOSDisk.MediaLink.Host.Split('.')[0]

#Setting the current storage account
Get-AzureSubscription | Set-AzureSubscription -CurrentStorageAccount $storageAccountName

#Creating a new Windows Azure Storage Container
$backupContainerName ="backupvhd"
New-AzureStorageContainer -Name $backupContainerName -Permission Off

#Confirming creation of storage container

#Backup virtual machine
$vmOsBlobName = $vmOsDisk.MediaLink.Segments[-1]
$vmOsContainerName = $vmOsDisk.MediaLink.Segments[-2].Split('/')[0]
Start-AzureStorageBlobCopy -SrcContainer $vmOsContainerName -SrcBlob $vmOsBlobName -DestContainer $backupContainerName

#Get-AzureStorageBlobCopyState to confirm that the copy process completed
Get-AzureStorageBlobCopyState -Container $backupContainerName -Blob $vmOsBlobName -WaitForComplete

#Backup Data Disk
ForEach ($vmDataDisk in $vmDataDisks) {
$vmDataBlobName = $vmDataDisk.MediaLink.Segments[-1]
$vmDataContainerName = $vmDataDisk.MediaLink.Segments[-2].Split('/')[0]
Start-AzureStorageBlobCopy -SrcContainer $vmDataContainerName -SrcBlob $vmDataBlobName -DestContainer $backupContainerName -Force
Get-AzureStorageBlobCopyState -Container $backupContainerName -Blob $vmDataBlobName -WaitForComplete

#Confirm that a copy of each virtual hard disk now exist in the backup storage container location.
Get-AzureStorageBlob -Container $backupContainerName

#Start VM after backup complete
$vm | Start-AzureVM







The process is length but at last we managed to backup the virtual machine.