How To Automate File Restore From Azure File Share Snapshot

| ,

Published on | Updated on May 6, 2021

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.

How To Automate File Restore From Azure File Share Snapshot 1

  • 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.

How To Automate File Restore From Azure File Share Snapshot 2

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.CloudFileShare

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.CloudFileShare `
    -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.

How To Automate File Restore From Azure File Share Snapshot 3

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.

How To Automate File Restore From Azure File Share Snapshot 4

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-

Related Posts

Previous

How To Set Dynamic MAC Address On a Hyper-V VM With PowerShell

How To Disable Automatic Checkpoints on Windows 10 Hyper-V For New VMs

Next

2 thoughts on “How To Automate File Restore From Azure File Share Snapshot”

Leave a comment...

  1. The command above $snapshot = $share.Snapshot() does not return any data. However, using $snapshot = $share.CloudFileShare.Snapshot() seems to work. However, when I get to the last command to restore the snapshot, I get the following error because the snapshot is now of type cloudfileshare.

    Start-AzStorageFileCopy : Cannot bind parameter ‘SrcShare’. Cannot convert the
    “Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare” value of type
    “Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare” to type
    “Microsoft.Azure.Storage.File.CloudFileShare”.

    I’m trying this as your other post https://charbelnemnom.com/sync-between-two-azure-file-shares-for-disaster-recovery/ does not mention how to restore the snapshot for DR. Is it possible to restore all the contents of the snapshot without specifying each file for DR purposes?

  2. Hello Raymond, thanks for your comment and feedback.
    It looks like Microsoft has changed the method type. I have updated the article to reflect the latest method changes (CloudFileShare).
    Please give it a try and let me know if it works for you.
    As for your second question, yes, you can restore all the contents of the snapshot without specifying each file for DR purposes.
    Here is how to do it:
    # Restore all files from the share snapshot for DR purposes
    $srcFiles = Get-AzStorageFile $snapshot.CloudFileShare
    $srcFiles | % {Start-AzStorageFileCopy -SrcShare $snapshot.CloudFileShare -SrcFilePath $_.Name -DestContext $azstorage.Context -DestShareName $azsfilesharesrc -DestFilePath $_.Name -Force}

    However, this command will fail if you have directories in the file share that you want to restore which is a common use case.
    If you are using Azure Backup, then it would be easier to restore the entire share and use Restore-AzRecoveryServicesBackupItem cmdlet as described here.
    Hope this helps!

Let me know what you think, or ask a question...

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

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

The content of this website is copyrighted from being plagiarized! You can copy from the 'Code Blocks' in Black.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!