question

SaurabhGada-0698 avatar image
0 Votes"
SaurabhGada-0698 asked SwathiDhanwada-MSFT commented

Automate On-demand backup for Azure Virtual Machines using PowerShell

Hi,
We need to prepare a script for on demand or ad-hoc backup request,
if a user wants to trigger an adhoc backup before any activity, with x days of retention
can we schedule it through Automation Account?

azure-automation
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SaurabhGada-0698 Did you get chance to look into my previous comment ? Do revert if you have further questions.

0 Votes 0 ·

1 Answer

SwathiDhanwada-MSFT avatar image
0 Votes"
SwathiDhanwada-MSFT answered

@SaurabhGada-0698 Welcome to Microsoft Q & A Community Forum. I understand you are trying to trigger on demand back up of azure vm using PowerShell. You can use below commands to trigger the on-demand backup for a virtual machine.

Here is a sample script to trigger an on-demand backup. This script assumes that you have enabled system assigned identity for automation account. Also, enable appropriate RBAC permissions to the system identity of this automation account. Otherwise, the runbook may fail.

 try
 {
     "Logging in to Azure..."
     Connect-AzAccount -Identity
 }
 catch {
     Write-Error -Message $_.Exception
     throw $_.Exception
 }
    
    
 Get-AzRecoveryServicesVault -Name "<recoveryvault name>" | Set-AzRecoveryServicesVaultContext
    
 $namedContainer = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -Status Registered -FriendlyName "<azurevmname>"
    
 $item = Get-AzRecoveryServicesBackupItem -Container $namedContainer -WorkloadType AzureVM
    
 $job = Backup-AzRecoveryServicesBackupItem -Item $item 


References:

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.