Tip: Azure Storage: create snapshots of Azure BLOBs via PowerShell

Azure Storage supports the creation of snapshots of BLOBs (binary large objects). Snapshots are a simple and fast way to keep and restore a certain version of a file at a certain point of time.

There are several tools which implement a snapshot function very comfortable. For example the ClumsyLeaf CloudXplorer which I use frequently. Microsoft has published a large list about other storage explorers in the Microsoft Azure Storage Team Blog. Here everyone can select his favourite tool.

Here is an example how the function looks like and works in the CloudXplorer. The first screenshot shows the creation of a snapshot and the second one shows how to get a list of snapshots and how to restore one.

snapshot1 snapshot2

Create snapshots via PowerShell

The PowerShell commands have no direct command to create a snapshot. But there is an important object type which we need for our purposes: ICloudBlob. This object has a method called CreateSnapshot(). As the name suggests this method creates a snapshot for a BLOB.

The following script sample shows in the first two steps how to get a BLOB with the name “version.txt” from the container “source” from a storage account. From this BLOB I retrieve the property ICloudBlob and invoke the method CreateSnapshot().

In the third step I show how filter only for snapshots. Only for the snapshots the property SnapshotTime is set. BLOBs which have no snapshots have the property SnapshotTime not set (equals to $null).

The the last and forth step I show how to download a BLOB with the PowerShell command Get-AzureStorageBlobContent. Hereby is does not matter if the BLOB is a snapshot or not.

# step 1: get file
$blobFile = Get-AzureStorageBlob -Container source | Where-Object -Property Name -eq "version.txt"

# step 2: create snapshot
$cloudBlob = $blobFile.ICloudBlob
$cloudBlob.CreateSnapshot()

# step 3: get snapshots
$snapshot = Get-AzureStorageBlob -Container source | Where-Object -Property SnapshotTime

# step 4: download snapshot
$snapshot[0] | Get-AzureStorageBlobContent

This sample is also available on GitHub Gist: CreateSnapshot.ps1.

The German version of this blog post can be found in my MSDN blog.

Advertisements
Tagged with: , , ,
Posted in Uncategorized
In Archive