다음을 통해 공유


Taking Snapshots on a Microsoft Azure VM and restoring it

Usually, one of the most valuable functions in a virtual environment is taking snapshots. In Microsoft Azure, this function is not a simple "right click, take snapshot" issue, but with some PowerShell magic, it's still possible.

First, you require the Microsoft Azure SDK: SDK

Second, you need your Blob Storage Account's username and password:

http://2.bp.blogspot.com/-y2j5a-aQN9Y/VZu3Iwci4YI/AAAAAAAA_9k/K13WDTCKq-Y/s1600/1.jpg

To get the Disk URI,  we will go to our Blob Storage and then "Containers" in the Classic Azure Portal:

http://2.bp.blogspot.com/-7I9Df6gmkn4/VZu8ik-uzjI/AAAAAAAA_90/s6rf5aRGaGM/s1600/2.jpg

Now let's run (and explain) the code:

Add-Type -Path 'C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.4\bin\Microsoft.WindowsAzure.StorageClient.dll'

$AzureAccountName = "MyStorageAccountName"
$AzureAccountKey = "MyStorageAccountKey"

$blobPath = "https://MyStorageAccountName.blob.core.windows.net/container/disk.vhd"
$creds = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey($AzureAccountName,$AzureAccountKey)
$blobClient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($blobPath, $creds)
$blob = $blobClient.GetBlobReference($blobPath)
$snapshot = $blob.CreateSnapshot()

Now let's explain:

  • With the "Add Type" command, we've added a reference to the DLL that represents the Azure API to us.
  • We've set variables that set our Account and Key to the Blob Storage.
  • We've created a variable to our Disk URI path.
  • We've set a credentials variable.
  • We've created a client object to the blob with our URI.
  • We've created a snapshot.

How to create an Azure disk from our Snapshot? There are a few options, but this is a favorite:

Create a new empty VHD file and add it to the Azure account:

New-VHD –Path d:\new-vhd01.vhd –SizeBytes 3MB
Add-AzureVhd -LocalFilePath d:\new-vhd01.vhd -Destination https://MyStorageAccountName.blob.core.windows.net/container/new-vhd01.vhd

Reference the correct Azure account:

$AzureAccountName = "MyStorageAccountName"
$AzureAccountKey = "MyStorageAccountKey"

Set the new Blob Path we created and Credentials variable:

$blobPath = "https://MyStorageAccountName.blob.core.windows.net/snaps/new-vhd01.vhd"
$creds = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey($AzureAccountName,$AzureAccountKey)

Create the Client and reference it:

$blobClient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($blobPath, $creds)
$blob = $blobClient.GetBlobReference($blobPath)

Get a snapshot list for this specific disk:

$snap = $snapshots | where {$_.Uri -ilike "*disk.vhd*" -and $_.SnapshotTime -ne $null}

Restore the snapshot you want to the new disk:

$blob.CopyFromBlob($snap[3])

Create a Windows disk for it:

Add-AzureDisk -DiskName 'CopiedBlob' -Label 'CopiedBlob' -MediaLocation https://MyStorageAccountName.blob.core.windows.net/snaps/new-vhd01.vhd -OS Windows

Now you can create an Azure VM from this snapshot.