Need the Python process for assigning an AZURE BackupPolicy to a fileshare

44 views Asked by At

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

There are 2 answers

0
Venkatesan On

Need the Python process for assigning an AZURE BackupPolicy to a file share

You can use the below Python code for assigning an Azure BackupPolicy to a file share.

Code:

from azure.identity import DefaultAzureCredential
from azure.mgmt.recoveryservicesbackup import RecoveryServicesBackupClient

def main():
    client = RecoveryServicesBackupClient(
        credential=DefaultAzureCredential(),
        subscription_id="xxxxx",
    )
    response = client.protected_items.create_or_update(
        vault_name="azurefilesvault",
        resource_group_name="xxxxx",
        fabric_name="Azure",
        container_name="StorageContainer;storage;xxxxxx;venkat789",
        protected_item_name="AzureFileShare;xxxxxx",
        parameters={
            "properties": {
                "policyId": "/subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.RecoveryServices/vaults/azurefilesvault/backupPolicies/policy2",
                "protectedItemType": "AzureFileShareProtectedItem",
                "sourceResourceId": "/subscriptions/xxxxf/resourceGroups/xxx/providers/Microsoft.Storage/storageAccounts/venkat789/fileServices/default/shares/share1",
            }
        },
    )
    print(response)

if __name__ == "__main__":
    main()

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 the create_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:

enter image description here

Reference: Protected Items - Create Or Update - REST API (Azure Recovery Services - Backup) | Microsoft Learn

0
Romojo On

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?