littlecharva
littlecharva

Reputation: 4244

Azure Powershell: Restoring a blob snapshot to a different VM

Having followed this article (http://blog.greatrexpectations.com/2013/04/24/using-blob-snapshots-to-backup-azure-virtual-machines/) I can create snapshots of a VM, but I now want to restore that snapshot to a different VM - we sometimes need to restore backups to recover individual files and I want to ensure I can do this in Azure before moving to them.

I've tried:

$restorePath = "vhds/my-new-vm.vhd"
$restoreBlob = $blobClient.GetBlobReference($restorePath)
$restoreBlob.CopyFromBlob($snapshots[$snapshots.Length -1])

But it gives me:

Exception calling "CopyFromBlob" with "1" argument(s): "There is currently 
a lease on the blob and no lease ID was specified in the request."

I can't figure out how to get a lease ID, or what to do with it.

Upvotes: 1

Views: 2641

Answers (3)

Doug
Doug

Reputation: 7057

You can also break the lease with PowerShell:

Get-AzureRmStorageAccount -Name "STORAGE_ACCOUNT_NAME" | Get-AzureStorageBlob -name "CONTAINER_NAME").ICloudBlob.BreakLease()

Upvotes: 0

fsbf
fsbf

Reputation: 111

Although this is an old thread, I think the problem has made a new appearance. In the last few weeks, I've noticed that when I restore a VHD on the way to making a new VM, I get a message that there's still a lease on the VHD file. However, if I wait a few hours, the lease is gone. I tried doing a loop to wait on the status of the blog copy, but it seems always to exit instantly.

Upvotes: 0

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

Since your VHD is actually a page blob stored in blob storage, in order to prevent other processes to write to this VHD, what's happening behind the scenes is that an Exclusive Write Lock is acquired on the blob when the VM is created or in other words a lease is acquired on the blob. The lease is of infinite duration.

You're getting this error because you're trying to overwrite a VM which has a lease on it with one of it's snapshots. Because the blob has lease, the operation will not complete. For this first you have to break the lease. You may find this blog post useful for that: http://www.biztalkgurus.com/biztalk_server/biztalk_blogs/b/biztalk/archive/2012/09/26/windows-azure-virtual-hard-disk-blob-storage-cross-account-copy-lease-break-and-management-tool.aspx

I can't figure out how to get a lease ID, or what to do with it.

You can only get the lease id when you acquire the lease. After that you can't get this thing programmatically. You would need lease id to perform various write operations on the blob and to change lease on the blob.

Upvotes: 3

Related Questions