How to enable Azure Blob storage versioning
Hi everyone, in this post, I will show you how to enable blob versioning on your Azure storage account using PowerShell and Azure CLI. When blob versioning is enabled, you can restore a previous version of a blob to recover data if modified or deleted by mistake. **What Microsoft Says About Blob Versioning: As part of your comprehensive data protection strategy, Microsoft recommends enabling all of the following blob data protection features:_
- Blob versioning.
- Container soft delete.
- Blob soft delete.
Important: Disabling blob versioning does not delete existing blob versions. When you turn off blob versioning, any existing versions remain accessible, but no new versions are subsequently created.
Prerequisites
- You can use an existing Storage Account, or you can create a new one. If you want to know how to create a Storage Account using PowerShell, check out this link.
- The Az.Storage module version 2.3.0 or later must be installed.
Azure PowerShell Workaround #
If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.
Connect-AzAccount
This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:
Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"
Once you set your default subscription, you’re ready to start.
Set the variables #
Here, we define the characteristics of our environment and the resource’s properties.
$resourceGroupName = "RG-DEMO-WE"
$stoAccountName = "storageaccountdemowe"
Enable blob versioning #
To enable blob versioning, you should use the Update-AzStorageBlobServiceProperty cmdlet with the following syntax.
Update-AzStorageBlobServiceProperty `
-StorageAccountName $stoAccountName `
-ResourceGroupName $resourceGroupName `
-IsVersioningEnabled $true
Verify the changes made #
You can verify that versioning is enabled by using the Get-AzStorageBlobServiceProperty cmdlet with the following syntax.
Get-AzStorageBlobServiceProperty `
-StorageAccountName $stoAccountName `
-ResourceGroupName $resourceGroupName
Then, you can check that the versions are created on the write operation using the following commands. In the following example, I have a container, “folder01,” containing a blob “Text_Document.txt.”
$stoaccount = Get-AzStorageAccount `
-Name $stoAccountName `
-ResourceGroupName $resourceGroupName
Get-AzStorageContainer `
-Name folder01 `
-Context $stoaccount.Context `
| Get-AzStorageBlob -IncludeVersion
As you can see, using the following command, you can list all the blob versions.
Get-AzStorageContainer `
-Name folder01 `
-Context $stoaccount.Context `
| Get-AzStorageBlob -IncludeVersion `
| Select-Object Name, VersionId, LastModified, IsLatestVersion
Disable blob versioning #
To disable blob versioning, you should use the Update-AzStorageBlobServiceProperty cmdlet with the following syntax.
Update-AzStorageBlobServiceProperty `
-StorageAccountName $stoAccountName `
-ResourceGroupName $resourceGroupName `
-IsVersioningEnabled $false
Azure CLI Workaround #
In this case, we will use Azure Cloud Shell, a browser-based shell built into Azure Portal. This allows us to use the Azure command-line tools (Azure CLI and Azure PowerShell) directly from a browser. If you want to know more about Azure Cloud Shell, check out this link. First, we define the characteristics of our environment and store the values in variables.
resourceGroupName="RG-DEMO-WE"
stoAccountName="storageaccountdemowe"
Enable blob versioning #
To enable blob versioning in your storage account, you should use the following command.
az storage account blob-service-properties update \
--account-name $stoAccountName \
--resource-group $resourceGroupName \
--enable-versioning true
Verify the changes made #
You can verify that blob versioning has been enabled by using the following command.
az storage account blob-service-properties show \
--account-name $stoAccountName \
--resource-group $resourceGroupName
Then, you can check that the versions are created on the write operation using the following commands. In the next example, I have a container, “folder01,” containing a blob “Text_Document.txt.”
az storage blob list \
--account-name $stoAccountName \
--container-name "folder01" \
--include v \
--output table
As you can see, using the following command, you can list all the blob versions.
az storage blob list \
--account-name $stoAccountName \
--container-name "folder01" \
--include v \
--query '[].{Container:container, Name:name, VersionID:versionId, IsCurrentVersion:isCurrentVersion}' \
--output table
Disable blob versioning #
To disable blob versioning, you should use the following command.
az storage account blob-service-properties update \
--account-name $stoAccountName \
--resource-group $resourceGroupName \
--enable-versioning false
Thanks for reading my post. I hope you find it helpful. If you want to know more about Azure blob versioning, check out this link.