Taking backups from Azure disks attached to AKS via Azure Automation

When you followed the Azure docs on how to dynamically create Azure disks via a Kubernetes PVC, you saw the instructions to create a backup of the disk. Snapshots are leveraged for this. But it is a manual task you must accomplish.

-> https://docs.microsoft.com/en-us/azure/aks/azure-disks-dynamic-pv#back-up-a-persistent-volume

It would be nice to have an automated solution which also take care of a specified retention time. Have a look at the following PowerShell script that can be scheduled and executed via Azure Automation.

-> https://github.com/neumanndaniel/kubernetes/blob/master/backup/createManagedDiskSnapshot.ps1

The PowerShell script needs five parameters as input. The resource group to store the snapshots, the resource id of the managed disk, the retention time for the snapshot, the Azure Storage account resource id and table name to store the backup information.

The script does the following steps:

1. Azure login

2. Initializing the resource group to store the snapshots

3. Snapshot creation and tagging with date and retention time


4. Writing backup information to the Azure Storage table including information like Azure region, retention time and snapshot resource id


5. Removing old snapshots and entries in the Azure Storage table

Storing the snapshot resource id is important to be able to do a clean up after the retention time is reached. Otherwise we will not be able to easily identify the snapshot that has to be deleted then.

Now let us have a look on how to configure the script to backup a specific Azure disk attached to Azure Kubernetes Service.

First, as prerequisites you need an existing Azure Automation and Azure Storage account. In the Azure Storage account create a new table to store the backup information.

Before we can import the PowerShell script as a runbook, we must import the PowerShell module AzureRmStorageTable from the Azure Automation module gallery.


Import the runbook and publish it, so we can configure the schedule next.

-> https://docs.microsoft.com/en-us/azure/automation/automation-creating-importing-runbook#importing-a-runbook-from-a-file-into-azure-automation

In the runbook context klick on Schedules and then Add a schedule to create a new one.


Finally configure the runbook parameters to target the Azure disk you want to backup and you are good to go.


Now, Azure Automation executes every four hours the runbook to take a snapshot as backup of the Azure disk attached to AKS and retains the snapshot for seven days.

If you want to backup more Azure disks, just add a new schedule with new runbook parameters to the runbook itself.

PS. When you have two or fewer entries in the Azure Storage table the runbook part “Removing old backups…” throws an error, but the runbook execution is successfully. I had not the time yet to fix it.