Azure NFS Snapshot Automation - Deletion

Introduction

The purpose of this document is to describe the process to take automated on-demand snapshots for Azure File Share.

Snapshot for Azure File Share is now in General Availability. Previously, Azure File Share did not have any backup option. In case of the File Share getting deleted, there was no mechanism to recover the File Share and the data was lost. The File Share Snapshot helps organizations take on-demand snapshot backups of the existing File Share for restoring it later as and when required. This document describes automating this process.

Pre-requisites

  • Subscription contributor
  • Implementation Steps

Step 1. Search for Automation Account in the Resource Blade and open the Automation Account Service.

Step 2. Click on + Create to create a new Automation Account.

Automation Account

Step 3. Fill in the below details and click on Next.

Details

Step 4. Select the System Assigned checkbox which will assign a System-assigned Managed Identity to this Automation account. Click on Next.

 System Assigned

Step 5. Select Private Access which will enable us to create a Private Endpoint to this NFS Storage.

Private Access

Step 6. Provide the Tags and click Review + Create to validate and create the Automation Account.

Step 7. Once the resource has been created, go to the resource, click on Runbooks à + Create a Runbook. This runbook is for creating new snapshots.

Runbooks

Step 8. Provide the Runbook Name, Type, Version, and Description, and click on Review + Create to create the Runbook.

Description

Step 9. Following the above steps, create a new Runbook for deleting old snapshots and copy the below code. The below code will check for older snapshots and delete them.

Connect-AzAccount -Identity

$resourceGroupName = "snapshottest"
$accountName = "snapshotnfstest"
$shareName = "testfileshare"

#$snapshotLifetimeInDays = "15"
$snapshotLifetimeInMinutes = "5"
$whatIf = $false

$CurrentDateTime = (Get-Date).ToUniversalTime()
#$DateTimeThreshold = $CurrentDateTime.AddDays(-$snapshotLifetimeInDays)
$DateTimeThreshold = $CurrentDateTime.AddMinutes(-$snapshotLifetimeInMinutes)

Write-Host "Querying all snapshots for share '$shareName'"

$snapshotList = (Get-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -IncludeSnapshot) | Where-Object { $_.Name -eq $shareName -and $_.snapshotTime -ne $null }

Write-Output $snapshotList

Write-Output "Current date/time is $CurrentDateTime. Removing share snapshots older than '$DateTimeThreshold'"

foreach ($snapshot in $snapshotList) {
    if ($snapshot.SnapshotTime -lt $DateTimeThreshold) {
        Write-Output "Removing snapshot '$($snapshot.snapshotTime)' of share '$($snapshot.Name)'"
        if ($whatIf -ne $true) {
            Remove-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -Name $snapshot.Name -snapshotTime $snapshot.snapshotTime -Force
        }
        else {
            Write-Output "Retaining recent snapshot '$($snapshot.snapshotTime)' of share '$($snapshot.Name)'"
        }
    }    
}

Step 10. Click on Schedule click on + Add a schedule.

Add a schedule

Step 11. Click on Schedule as shown in the snapshot below.

Schedule

Step 12. Click on + Add a Schedule.

Schedule runbook

Step 13. Provide the schedule for the Automation account to execute the Runbook and click on Create to create a schedule.

Execute the Runbook

Set expiration


Similar Articles