skip to Main Content

I am trying to enable autoprotect for a SQL Server instance on an Azure virtual machine. I want to do this using a REST call to the Azure API.

AUTOPROTECT is disabled

I’m using the AzureWorkloadSQLAutoProtectionIntent object and have followed Microsoft Azure’s documentation regarding this object.

https://learn.microsoft.com/en-us/rest/api/backup/protected-items/create-or-update?view=rest-backup-2024-04-01&tabs=HTTP#azurevmworkloadsqldatabaseprotecteditem

I have written the following PowerShell script, but it is not working:

$url = "https://management.azure.com/Subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.RecoveryServices/vaults/$vaultName/backupFabrics/Azure/backupProtectionIntent/$intentObjectName`?api-version=$apiVersion"

# Create Headers
$headers = @{
  "Authorization" = "Bearer $accessToken"
  "Content-Type" = "application/json"
}

# Request Body
$parentID = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.SqlVirtualMachine/SqlVirtualMachines/$serverName"

$body = @{
  "properties" = @{
      "backupManagementType" = "AzureWorkload";
      "itemId" = $itemId;
      "policyId" = $policyId;    
      "protectionIntentItemType" = "AzureWorkloadSQLAutoProtectionIntent";
      "sourceResourceId" = $parentID; 
      "workloadItemType" = "SQLDataBase";
  }
} | ConvertTo-Json

# Invoke Rest Method
Invoke-RestMethod -Uri $url -Method PUT -Headers $headers -Body $body

When I try to run this script, I get the following error:

Error:
"code": "BMSUserErrorInvalidInput",
"message": "Input provided for the call is invalid. Please check the required inputs"

I’ve double-checked all my resource IDs and am using the latest version of the API. Can you help me understand what I’m doing wrong in this script? What are the correct steps to enable AzureWorkloadSQLAutoProtectionIntent for a SQL Server instance using a REST call to the Azure API?

Thank you in advance.

2

Answers


  1. Chosen as BEST ANSWER

    I was able to make it work. My backup container name was missing the 'VMAppContainer' value:

    • Wrong: Compute;<YOUR_RESOURCE_GROUP_NAME>;<YOUR_VM_NAME>
    • Right: VMAppContainer;Compute;<YOUR_RESOURCE_GROUP_NAME>;<YOUR_VM_NAME>

    You can find the container using the following documentation:

    https://learn.microsoft.com/en-us/rest/api/backup/protection-containers/get?view=rest-backup-2024-04-01&tabs=HTTP

    Here is my final code:

    # Constants
    $subscriptionId = "<YOUR_SUBSCRIPTION_ID>"
    $resourceGroupName = "<YOUR_RESOURCE_GROUP_NAME>"
    $vaultName = "<YOUR_VAULT_NAME>"
    $policyId = "/subscriptions/<YOUR_SUBSCRIPTION_ID>/resourceGroups/<YOUR_RESOURCE_GROUP_NAME>/providers/Microsoft.RecoveryServices/vaults/<YOUR_VAULT_NAME>/backupPolicies/<YOUR_POLICY_NAME>"
    $itemId = "/subscriptions/<YOUR_SUBSCRIPTION_ID>/resourceGroups/<YOUR_RESOURCE_GROUP_NAME>/providers/Microsoft.RecoveryServices/vaults/<YOUR_VAULT_NAME>/backupFabrics/Azure/protectionContainers/<YOUR_CONTAINER_INFO>/protectableItems/<YOUR_PROTECTABLE_ITEM>"
    $parentName = "/subscriptions/<YOUR_SUBSCRIPTION_ID>/resourceGroups/<YOUR_RESOURCE_GROUP_NAME>/providers/Microsoft.RecoveryServices/vaults/<YOUR_VAULT_NAME>/backupFabrics/Azure/protectionContainers/<YOUR_CONTAINER_INFO>"
    $intentObjectName = "<YOUR_INTENT_OBJECT_NAME>"
    $apiVersion = "2022-01-01"
    $url = "https://management.azure.com/Subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.RecoveryServices/vaults/$vaultName/backupFabrics/Azure/backupProtectionIntent/$intentObjectName?api-version=$apiVersion"
    
    # Create Headers
    $headers = @{
      "Authorization" = "Bearer <YOUR_ACCESS_TOKEN>"
      "Content-Type" = "application/json"
    }
    
    # Request Body
    $body = @{
      "properties" = @{
        "protectionIntentItemType" = "RecoveryServiceVaultItem"
        "backupManagementType" = "AzureWorkload"
        "parentName" = $parentName
        "itemId" = $itemId
        "policyId" = $policyId
      }
    } | ConvertTo-Json 
    
    # Invoke REST Method
    Invoke-WebRequest -Uri $url -Method PUT -Headers $headers -Body $body
    

  2. ERROR:

    Error: "code": "BMSUserErrorInvalidInput", "message": "Input provided for the call is invalid. Please check the required inputs"

    The error BMSUserErrorInvalidInput indicates that one of the parameters is incorrect or missing.
    Check if all resource IDs, like subscriptionId, resourceGroupName, vaultName, serverName, itemId, and policyId, are correct.

    The below is the code to call the REST to enable automatic protection:

    PUT https://management.azure.com/Subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/Azure/backupProtectionIntent/{intentObjectName}?api-version=2024-04-01
    Content-Type: application/json
    Authorization: Bearer {accessToken}
    
    {
      "properties": {
        "backupManagementType": "AzureWorkload",
        "itemId": "{itemId}",
        "policyId": "{policyId}",
        "protectionIntentItemType": "AzureWorkloadSQLAutoProtectionIntent",
        "sourceResourceId": "{parentID}",
        "workloadItemType": "SQLDataBase"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search