Loading...

How to automate On-Demand Azure Backup for Azure Virtual Machines using PowerShell

How to automate On-Demand Azure Backup for Azure Virtual Machines using PowerShell

This guest post is authored by Swathi Dhanwada, Customer Engineer 2, who is frequently seen answering Azure Backup community queries.

 

Azure Backup supports multiple backups of Azure Virtual Machines per day using Enhanced Policy.  For hourly backup, the minimum RPO is 4 hours and the maximum is 24 hours. You can set the backup schedule to 4, 6, 8, 12, and 24 hours respectively. Learn how to back up an Azure VM using Enhanced policy.   This feature is only available to unprotected VMs that are new to Azure Backup. VMs that are already protected with existing policy can't be moved to Enhanced policy.   To overcome this, you can run an multiple on-demand backup of a VM per day after you set up its protection. Keep these details in mind:

  • If the initial backup is pending, on-demand backup creates a full copy of the VM in the Recovery Services vault.
  • If the initial backup is complete, an on-demand backup will only send changes from the previous snapshot to the Recovery Services vault. That is, later backups are always incremental.
  • You can’t trigger on-demand backups by disabling scheduled backup.

Below are the steps to automate the on-demand backup for azure virtual machines using Azure Automation.

Step1

Create an Automation resource with system managed identity. To assign permissions to managed identities, complete the following steps:

  • Sign in to Azure interactively using the Connect-AzAccount cmdlet and follow the instructions:

 

 

Sign in to your Azure subscription `$sub = Get-AzSubscription -ErrorAction SilentlyContinue if(-not($sub)) { Connect-AzAccount }` If you have multiple subscriptions, set the one to use Select-AzSubscription -SubscriptionId <SUBSCRIPTIONID>

 

 

  • Provide an appropriate value for the following variables and then run the script:

 

 

$resourceGroup = "resourceGroupName" $automationAccountId = "xAutomationAccount"

 

 

  • Use the PowerShell cmdlet New-AzRoleAssignment to assign a role to the system-assigned managed identity:

 

 

$role1 = "Backup Operator" $SAMI = (Get-AzAutomationAccount -ResourceGroupName $resourceGroup -Name $automationAccount).Identity.PrincipalId New-AzRoleAssignment -ObjectId $SAMI -ResourceGroupName $resourceGroup -RoleDefinitionName $role1

 

 

Step2

Verify if required modules are imported to the Automation resource. If not, import the following modules from the Modules gallery in the order given below:

Step3

Create PowerShell Runbooks in the Automation Resource. You can create multiple Runbooks based on which set of virtual machines you want to protect.

Step4

Edit the Runbook and write script to choose which virtual machines to take a backup. You can create scripts that suit your requirements.

 

Script to trigger on demand backup for single vm

 

 

Get-AzRecoveryServicesVault -Name "<recoveryvault name>" | Set-AzRecoveryServicesVaultContext $namedContainer = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -FriendlyName "<azurevmname>" $item = Get-AzRecoveryServicesBackupItem -Container $namedContainer -WorkloadType AzureVM $job = Backup-AzRecoveryServicesBackupItem -Item $item

 

 

Step5
  • Schedule the Runbook. While scheduling the Runbook, you can pass on the parameters required for the PowerShell Script.
  • The sample script takes the retention as an input. So, if you need to schedule a weekly snapshot and retain for 8 weeks, create a weekly schedule as mentioned below and specify the retention as 56 days (8 weeks). You can create monthly and yearly schedules (run every 12 months) in a similar manner. You can monitor the success/failure of these backups using the “Jobs” tab of Runbooks.

Reference Documents:

Published on:

Learn more
Azure Storage Blog articles
Azure Storage Blog articles

Azure Storage Blog articles

Share post:

Related posts

Announcing latest Azure Cosmos DB Python SDK: Powering the Future of AI with OpenAI

We’re thrilled to announce the stable release of Azure Cosmos DB Python SDK version 4.14.0! This release brings together months of innov...

18 hours ago

How Azure CLI handles your tokens and what you might be ignoring

Running az login feels like magic. A browser pops up, you pick an account, and from then on, everything just works. No more passwords, no more...

1 day ago

Boost your Azure Cosmos DB Efficiency with Azure Advisor Insights

Azure Cosmos DB is Microsoft’s globally distributed, multi-model database service, trusted for mission-critical workloads that demand high ava...

3 days ago

Microsoft Azure Fundamentals #5: Complex Error Handling Patterns for High-Volume Microsoft Dataverse Integrations in Azure

🚀 1. Problem Context When integrating Microsoft Dataverse with Azure services (e.g., Azure Service Bus, Azure Functions, Logic Apps, Azure SQ...

3 days ago

Using the Secret Management PowerShell Module with Azure Key Vault and Azure Automation

Automation account credential resources are the easiest way to manage credentials for Azure Automation runbooks. The Secret Management module ...

4 days ago

Microsoft Azure Fundamentals #4: Azure Service Bus Topics and Subscriptions for multi-system CRM workflows in Microsoft Dataverse / Dynamics 365

🚀 1. Scenario Overview In modern enterprise environments, a single business event in Microsoft Dataverse (CRM) can trigger workflows across m...

5 days ago

Easily connect AI workloads to Azure Blob Storage with adlfs

Microsoft works with the fsspec open-source community to enhance adlfs. This update delivers faster file operations and improved reliability f...

5 days ago

Microsoft Azure Fundamentals #3: Maximizing Event-Driven Architecture in Microsoft Power Platform

🧩 1. Overview Event-driven architecture (EDA) transforms how systems communicate.Instead of traditional request–response or batch integration...

5 days ago

Azure Developer CLI (azd) – October 2025

This post announces the October release of the Azure Developer CLI (`azd`). The post Azure Developer CLI (azd) – October 2025 appeared f...

6 days ago

Microsoft Azure Fundamentals #2: Designing Real-Time Bi-Directional Sync Between Dataverse and Azure SQL for Multi-Region Deployments

Here’s a detailed technical breakdown of designing a real-time bi-directional sync between Dataverse and Azure SQL for multi-region deployment...

6 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy