How to backup Azure blob storage using PowerShell

How to backup Azure blob storage PowerShell

In this azure tutorial, we will discuss How to backup Azure blob storage using PowerShell. Apart from this, we will also discuss on few other topics

  • Azure blob storage backup
  • Configure incremental backup Azure blob storage account
  • How to configure full backup Azure blob storage account
  • Configure the restore Azure blob storage account

How to backup Azure blob storage using PowerShell

In, my last article we have discussed, How to Upload and Download File From Azure Blob Storage Using C# and PowerShell.

We will discuss here azure blob storage backup using PowerShell.

Microsoft has introduced Azure Site Recovery (ASR) and Azure Backup together with the Azure Backup Agent to achieve the same functionality.

But, what if we will try to write a PowerShell script, that will help us to take the backup of the Azure Blob storage.

First, we will have to check if the file has changed or we need to find a way to check if a file has changed.If you will upload a file that has been changed 2 days ago, it will store the date and time when it was uploaded or it will overwrite the date and time.

So ultimately, We will have to store the modification date of the local file in Azure.

We can also use the MD5 hash of the file to check this. You can get the Content-MD5 option in the Blob Properties window in the Azure portal. So we can calculate the MD5 of the local file using the below script.

Function MDF5HashDetails {
    Param (
        [Parameter(Mandatory=$true)][String]$Path
    )
 
    If (Calculate -Path $Path) {
        try {
            
            $MyCryptocalc = [System.Security.Cryptography.MD5]::Create()
            $myContent = Get-Content -Path $Path -Encoding byte
            $calculatehash = [System.Convert]::ToBase64String($MyCryptocalc.ComputeHash($myContent))
        } catch {
            $calculatehash = $null
        }
    } Else {
        
        $calculatehash = $null  
    }
         return $calculatehash
}
How to backup Azure blob storage using PowerShell

The above function returns the MD5 hash of the file, we can compare this to the MD5 hash on Azure Portal.

Now, we need to retrieve, the Blob metadata and properties, using the below script.

$myblobname = "myNewBlob"
$containername = "mycontainer"
$myblob = Get-AzureStorageBlob -Blob $myblobname -Container $containername -Context $cntx
$mycloudblob = [Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob]$myblob.ICloudBlob
$mycloudblob.MetaData | Format-Table
$mycloudblob.FetchAttributes()
$mycloudblob.Properties | Format-Table
Write-Host $mycloudblob.Properties.ContentMD5
How to backup Azure blob storage

The above are the properties that can be compared to the values of the local files. If the values differ, then you can overwrite the file on Azure.

Now the next thing is to check for the container. Get-AzureStorageContainer is the cmdlet that helps us to get the container details from the Azure portal. You can use the below script

try {
    $mystoragecontainer = Get-AzureStorageContainer -Name $containername -Context $cntx -ErrorAction SilentlyContinue
} catch {}
 
If ($? -eq $false) {
   
    If ($Error[0] -like "No container found") {
       
        Write-Host -Value " The Container `"$containername`" does not exist, trying to create container" 
        $mystoragecontainer = New-AzureStorageContainer -Name $containername -Context $cntx -ErrorAction SilentlyContinue
 
        If ($mystoragecontainer -eq $null) {
            
            Write-Host -Value "Not able to create container `"$containername`"" 
            return
        } Else {
            Write-Host -Value "Container `"$Container`" successfully created" 
        }
    }
    }
backup azure blob storage using powershell script

Now the time to upload the Blob using the Set-AzureStorageBlobContent cmdlet.

$result = Set-AzureStorageBlobContent -File $myfile.FullName -Blob $myblob -Container $containername -Context $cntx -Metadata @{"Filelastwritetime" = $myfile.LastWriteTimeUTC.Ticks} -Force
azure blob storage backup

Azure blob storage backup

As of now, there is no out of box solution is available to back up the block blobs in Azure blob storage. As a workaround, there is some solution we can create that helps us to take daily incremental backups as well as weekly full backups for the block blobs in Azure Blob storage.

You can use the following technologies as part of the back up solution.

AzCopy: AzCopy is a command-line tool that can be used to back up the block blobs in Azure Blob storage. You can copy data between file systems or file systems to storage accounts as well as from one storage account to another storage account.

If you want to learn more about AzCopy, you can have look at the article on AzCopy, which includes the installation of AzCopy and more details.

EventGrids: This allows us to push the Events to subscribers such as Azure Functions, Azure Storage Queues, Azure Logic Apps, etc.

Docker Container: Docker Container helps to host the listener to read the events from Azure Queue Storage.

Azure Table Storage: This helps to keep the events metadata of incremental back-up and which can be used while performing the re-store activity. 

How to configure incremental backup

Below are the few high level steps you can configure for the incremental backup

  1. You need to create a new storage account in the destination where you want to take the back up.
  2. On the Source, you need to create an event grid subscription for the storage account that helps us to store the events in the Azure Storage queue.
  3. Now you need to create table storage where the .Net Listener will store the events of the event grid.
  4. The next step is to create the .Net Listener that will help to take the incremental backup. There can be multiple listeners based on the need to perform backup activities based on the load on the storage account.

How to configure full backup

Below are the few high level steps to configure the full backup

  1. AzCopy command-line tool is the right choice here, you can use this tool to schedule the back up on the weekends to move the full data from the source storage account to the destination storage account.
  2. We can use the AzCopy to move the data to a logical folder on the source storage account to a folder in the destination storage account.
  3. You can also schedule the backup for a virtual machine(VM) using the AzCopy command-line tool based on your business needs.

How to configure the restore

You can consider below are few high level steps to configure the restore functionality.

  1. The first step is to make sure to create a new storage account in the destination where you want to restore the backup files.
  2. The next step is to move the data from the logical folder which is present on the source to the destination storage account using AzCopy command-line tool.
  3. Now start the incremental restore option by providing the start and the end date for restoring the data.

You may like following Azure tutorials:

Conclusion

Well, here in this article, we discussed how to backup azure blob storage using PowerShell script. Apart from this, we also discussed Azure backup, Azure blob storage backup, and some high-level steps on How to configure incremental backup, how to configure the full backup Azure blob storage account, and finally How to configure the restore Azure blob storage account.

Leave a Comment