Tracking Azure Backup Job Status with CLI

Martin Thong 20 Reputation points
2023-07-10T23:39:43.5133333+00:00

What is the process for creating a CLI script to initiate an Azure virtual machine backup, monitor the backup status every five minutes, and trigger an email notification upon job completion?

Azure Backup
Azure Backup
An Azure backup service that provides built-in management at scale.
1,076 questions
{count} votes

Accepted answer
  1. Luke Murray 10,276 Reputation points MVP
    2023-07-11T00:01:21.16+00:00

    You could do something similar to this with PowerShell."

    # Authenticate to Azure
    Connect-AzAccount
    
    # Set Variables
    $resourceGroupName = "resourceGroup"
    $vaultName = "vaultName"
    $vmName = "pstestv2vm1"
    $emailFrom = "backup@contoso.com"
    $emailTo = "admin@contoso.com"
    $emailSubject = "VM Backup Job Completed"
    $emailBody = "The VM backup job for $vmName is complete."
    
    # Get Recovery Services Vault and Backup Item
    $vault = Get-AzRecoveryServicesVault -ResourceGroupName $resourceGroupName -Name $vaultName
    $NamedContainer = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -FriendlyName $vmName -VaultId $vault.ID
    $Item = Get-AzRecoveryServicesBackupItem -Container $NamedContainer -WorkloadType AzureVM -VaultId $vault.ID
    
    # Initiate Backup
    $Job = Backup-AzRecoveryServicesBackupItem -Item $Item -VaultId $vault.ID -ExpiryDateTimeUTC (Get-Date).ToUniversalTime().AddDays(60)
    $Job
    
    # Monitor Backup Status
    do {
        $jobStatus = Get-AzRecoveryServicesBackupJob -VaultId $vault.ID -Job $Job
        Start-Sleep -Seconds 300
    } until ($jobStatus.Status -eq "Completed")
    
    # Trigger Email Notification
    Send-MailMessage -From $emailFrom -To $emailTo -Subject $emailSubject -Body $emailBody
    
    
    

    Reference: Example 1: Start a backup for a Backup item

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful