Within a Python program, I’m creating storage accounts and fileshares based on some parameters, and need to assign an existing backup policy to this new fileshare.
I’ve found the method to do this in Powershell (I think: below, from the AZURE site.
$monthlyafsPol = Get-AzRecoveryServicesBackupProtectionPolicy -Name "monthlyafs"
$afsContainer = Get-AzRecoveryServicesBackupContainer -FriendlyName "testStorageAcct" -ContainerType AzureStorage
$afsBkpItem = Get-AzRecoveryServicesBackupItem -Container $afsContainer -WorkloadType AzureFiles -Name "testAzureFS"
Enable-AzRecoveryServicesBackupProtection -Item $afsBkpItem -Policy $monthlyafsPol
But haven’t found the Python equivalent yet. I suspect it’s hanging off RecoveryServicesBackupClient
somewhere but no success yet.
2
Answers
Thanks! I'm most of the way there now, but having issues with the sourceResourceId parameter. Error is .. SourceResourceId must be a fully qualified ARM Id of source storage account
My value has been built up from: # - Subscription S1 = "/subscriptions/" + sub_id # - Resource group S2 = "/resourceGroups/" + AZURE_RG # - Provider S3 = "/providers/Microsoft.Storage" # - Storage account S4 = "/storageAccounts/" + STGACCTAPPX # - Fileservices part S5 = "/fileServices/default/shares/" + ShareName # SrcResID = S1 + S2 + S3 + S4 + S5
I'm also using a storage account without a container - so have set parameter to container_name="StorageContainer;storage;" + ResGroup +";$root"
Sound good?
You can use the below Python code for assigning an
Azure BackupPolicy
to a file share.Code:
The above code creates or updates a protected item in an Azure Recovery Services vault using the Azure SDK for Python. It uses the
RecoveryServicesBackupClient
class and thecreate_or_update()
method, passing in the necessary parameters including the vault name, resource group name, fabric name, container name, protected item name, and a dictionary of parameters for the protected item. The response from the create_or_update() method is printed to the console.Output:
Reference:
Protected Items – Create Or Update – REST API (Azure Recovery Services – Backup) | Microsoft Learn