Using blob snapshots with PowerShell
Sometime ago, I had a customer who asked me the way to create a blob snapshot of an Azure VM. The process for create a blob snapshot was clear for the customer and you can read the next blog as a reference:
As you read in the article, you can create, list, copy and delete a blob snapshot, but how can you get a particular snapshot that was taken before and not the one you just created?
First, please read the next reference blog to have in mind some consideration of the blob snapshots.
https://azure.microsoft.com/en-us/documentation/articles/storage-blob-snapshots
Now, let’s go to the code.
NOTE: Check that you have PowerShell already installed, details here.
Login to your Azure Subscription.
=============================================
- Open a PowerShell session with Admin rights.
- Run the command : Login-AzureRmAccount
- Provide the credentials that you usually use to Login to the Azure Portal.
To create a snapshot.
=============================================
- Define the storage account and the context.
|
|
- Create the context.
|
|
- Get a reference to a blob.
|
|
- Create a blob snapshot.
|
|
With this procedure you can create a snapshot at any time for the $BlobName defined.
Consider that the name will be the same as the original blob, but at the time the blob snapshot is created, there is a snapshotTime property that includes the precise date/time when the blob snapshot was created. We will use the snapshotTime property to determinate and select later, the blob snapshot that you want to copy and/or to be used for you to promote.
Retrieve the blob snapshot list.
=============================================
- List the blob snapshots.
|
|
The above command will provide all the blob snapshots associated with the $BlobName.
If we check the $ListBlob variable, it will show us the list of blob snapshots, and in one of the columns, we can see the snapshotTime column displaying the time the snapshot was taken (this is in UTC Time zone).
Copy a particular/specific blob snapshot.
=============================================
- Define the variables.
|
|
- Create a table to list the blob snapshots you have (this will let you select the item and the snapshotTime property from this table, so later we can get the snapshot based on the snapshotTime property).
|
|
In the image below, you can see all the blob snapshots have the name "juliocotest2016123225918.vhd" and the way you can identify when the blob snapshot was taken is by checking the snapshotTime property.
- Using the table above, you can select the blob snapshot you want to be copied by using the snapshotTime property, so, let’s said you want to copy the snapshot that was taken last “2/25/2016 6:01:30 AM +00:00”. So, you will use and select the SnapshotTime property for that item “3” (Note: Consider that "Item" is the item from the table to be selected based on the SnapshotTime property that you want to select/copy).
|
|
- Now, select the blob snapshot you want to restore based on the snapshotTime property we got from previous step.
|
|
- Now, let’s convert the blob selected to a CloudBlob type.
|
|
- Finally, you can now copy the blob snapshot to another container.
|
|
As you can see in the image below, I was able to copy that particular snapshot into my “snapshots” container.
Now, I used Azure Storage Explorer, to check the blob.
Here is a screenshot showing the blob I had before copied the blob snapshot, in my “snapshots” container.
And in the next screenshot you can see the “recovery.vhd” page blob that was copied from the snapshot list.
Now, just to end this blog, I would like to show you the way to delete a series of blob snapshots based on the time they were taken (yes, using the snapshotTime property). To do that, you need to list again the blob snapshots you have.
Retrieve the snapshots list.
=============================================
- List the snapshots of a blob.
|
|
The above command will provide all the snapshot associated with the $BlobName
Declare the min and max dates (to be removed).
=============================================
- Declare minimum date, example:
|
|
- Declare maximum date, example:
|
|
The above is telling you that you want to delete the blob snapshots from $minDate ("01/23/2016 9:00 AM") to $maxDate ("02/24/2016 9:00 PM").
Delete the snapshot of a blob.
=============================================
- For this, I did a foreach iteration to validate first if the blob is a snapshot and then to validate if it is in the range of dates I defined in previous step.
|
|
Finally, you can list again the snapshot list and you will see that you already deleted the ones for the dates you defined.
Happy coding !!
Comments
- Anonymous
November 21, 2016
Do I need to stop the VM to take a snapshot or restore it's OS or data disc?- Anonymous
March 21, 2017
Hello Vladimir, You do not need to stop the VM to take the snapshot, however, to restore the OS, if the VM is running, then the VHD will have a lease and this will prevent access to that blob. You will need to remove first the VHD from the running VM and then you can perform the restore operation.
- Anonymous
- Anonymous
January 18, 2017
Thanks very much for this information :)- Anonymous
March 21, 2017
Your welcome Adam.
- Anonymous