How To Automate File Restore From Azure File Share Snapshot

4 min read

Introduction

Azure Files offers fully managed file shares in the cloud that are accessible via the industry-standard Server Messaging Block (SMB) Protocol. Azure file shares can be mounted concurrently by cloud or on-premises deployments of Windows, Linux, and macOS. Additionally, Azure file shares can be cached on Windows Servers with Azure File Sync for fast access near where the data is being used. The benefits of Azure File Sync are the following:

  • Simplify cloud development
  • Replace file server functionality
  • SMB Authentication with Azure AD
  • Scalability
  • Integration with Azure Backup service

For more information about Azure File Sync and how to get started, please check the following Step-by-Step guide.

As you know, if you need to restore a file in the Azure portal, the process is very simple as follows:

  • You navigate to the share in the storage account > File shares > click on the share name in question and then click on Manage backups.
  • The Backup Item blade in the Recovery Services vault will open that is managing the backups.
  • Then you can restore either the entire file share by clicking on (Restore Share), or select (File Recovery) if you want to restore an individual file.

  • In my case, I want to restore an individual file, you select File Recovery, then select the desired Restore Point, click OK.
  • In the Restore Location blade, you can either restore the file to its Original Location or Alternate Location.
  • Then you click on Select File, and choose the file(s) in question, click Select, and then click OK.
  • When you’re ready, click Restore and an Azure Backup restore job will be triggered and restore your file.

It’s worth mentioning that at the time of this writing, the Snapshot (Backup) for Azure Files is stored within the same Azure Storage account, the snapshot is NOT transferred to the Recovery Services Vaults as we have with Azure VM backup. You can read more about it here.

I recently came across a scenario where I need to automate the restore of multiple sets of files to a well-known state to a different storage account/file share. As you can see, there are multiple steps you want to go through to restore a file using the Azure Portal.

In this article, I will share with you how to automate the restore process of a file from an Azure file share snapshot using PowerShell and Azure CLI.

Restore file from a share snapshot using PowerShell

For this example, I want to restore a single file to its original location from a specific snapshot.

Jump into Azure Cloud Shell session or using Windows Terminal with PowerShell Core V7.0.0 and run the commands below:

First, I need to specify the storage account name and the file share name and put them in the following 2 variables:

$azstorageacct = "afsstoragesync"
$azsfilesharesrc = "afs-doc-share"

Then, I will query the storage account name:

$azStorage = Get-AzStorageAccount | Where-Object { $_.StorageAccountName -eq $azstorageacct }

Next, I will get the file share details and the snapshot.

$share = Get-AzStorageShare -Context $azStorage.Context -Name $azsfilesharesrc
$snapshot = $share.Snapshot()

Next, I will list all available share snapshots:

$snapshots = Get-AzStorageShare -Context $azstorage.Context | Where-Object { $_.Name -eq $snapshot.Name -and $_.IsSnapshot -eq $true }

Then, I need to select which snapshot (Time) I want to restore from.

For Windows Terminal with PowerShell Core V7, you can use the following command:

$snapshot = $snapshots | Out-GridView -PassThru

If you are using the Cloud Shell, then you need to specify the desired Snapshot Time by running the following command:

$snaphosts
$snapshot = $snapshots | Where {$_.SnapshotTime -eq "12/12/19 9:34:02 PM +00:00"}

Last, you can restore the desired file from the snapshot to the desired destination (storage account and file share name). This can be the same share as well.

# Restore Sample file from the share snapshot
Start-AzStorageFileCopy `
    -SrcShare $snapshot `
    -SrcFilePath "2018-04-20_AzureIaaS_PowerBI_Reporting.mp4" `
    -DestContext $azstorage.Context `
    -DestShareName $azsfilesharesrc `
    -DestFilePath "2018-04-20_AzureIaaS_PowerBI_Reporting.mp4"

Here is an example of the output when you run it in action.

Restore file from a share snapshot using Azure CLI

You can also restore any file from a share snapshot using the Azure CLI.

Open an Azure Cloud Shell session and run the following commands to automate the restore process. If you are using Azure CLI locally on your machine, then you need to sign in to Azure first using az login command.

First, I need to specify the storage account name and the file share name and put them in 2 variables:

$azstorageacct = "afsstoragesync"
$azsfilesharesrc = "afs-doc-share"

Next, I will query the storage account name to get the resource group name and put it in a variable:

$azstoragerg=(az storage account show --name $azstorageacct --query "resourceGroup")

Then, I need to get the storage account key and put it in another variable:

$azstoragekey = (az storage account keys list --resource-group $azstoragerg --account-name $azstorageacct --query "[0].value")

Next, I will list all available share snapshots:

az storage share list --account-name $azstorageacct --account-key $azstoragekey --include-snapshot --query "[? name=='$azsfilesharesrc' && snapshot!=null]" --output table

Then, I need to select which snapshot I want to restore from. In this example, I want to restore from the following snapshot date/time: “2019-12-13T21:31:08.0000000Z“. I will put all the snapshots in one variable and then I filter

$snapshot = az storage share list --account-name $azstorageacct --account-key $azstoragekey --include-snapshot --query "[5].snapshot"

Finally, I will restore my file using the ‘az storage file copy start‘ command using the parameter –file-snapshot to restore from the $snapshot variable above.

az storage file copy start --account-name $azstorageacct --account-key $azstoragekey --destination-share $azsfilesharesrc --destination-path "2018-04-20_AzureIaaS_PowerBI_Reporting.mp4" --source-share $azsfilesharesrc --source-path "2018-04-20_AzureIaaS_PowerBI_Reporting.mp4" --file-snapshot $snapshot

Here is an example of the output when you run it in the Cloud Shell.

That’s it there you have it!

Summary

In this article, I showed you how to automate the file restore process from a share snapshot using Azure PowerShell and Azure CLI. You can further automate it and restore any set of files to a different or to the same storage account/file share.

__
Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

About Charbel Nemnom 550 Articles
Charbel Nemnom is a Cloud Architect, ICT Security Expert, Microsoft Most Valuable Professional (MVP), and Microsoft Certified Trainer (MCT), totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 17 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems. Excellent communicator is adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design, business continuity, and cloud security.

Be the first to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.