DEV Community

Anthony Buhnerkemper
Anthony Buhnerkemper

Posted on

Azure Backup and Site Recovery with PowerShell 7: Find the Gaps Before You Need a Restore

Backups are easy to set up. They are also easy to forget about.

Then someone needs a restore, and you find out a VM was never protected. Or the last good backup was two weeks ago.

So I cleaned up the checks I use and shared them on GitHub. This toolkit uses PowerShell 7 and the Az.RecoveryServices module.

Repo: azure-backup-dr-powershell-toolkit

The problem it solves

A new VM goes live. Nobody turns on backup.

A backup job fails every night. The alert goes to a mailbox nobody reads.

Soft delete is off. One bad click and the backups are gone.

Site Recovery is set up, but nobody has ever run a test failover.

These scripts make those checks quick and repeatable.

Highlight scripts

Get-AzUnprotectedVmReport.ps1

Compares every VM in the subscription with every vault. It lists the VMs with no backup. VMs tagged Backup = Exempt are listed as exempt, so you can document why they are skipped.

./Get-AzUnprotectedVmReport.ps1
Enter fullscreen mode Exit fullscreen mode

Get-AzBackupProtectedItemReport.ps1

Shows every protected VM with its last backup result and how many hours ago it ran. It flags failed and stale backups.

./Get-AzBackupProtectedItemReport.ps1 -StaleHours 26
Enter fullscreen mode Exit fullscreen mode

Get-AzBackupVaultReport.ps1

Checks each vault for soft delete, storage redundancy and immutability. These settings are your best defense against ransomware and mistakes.

./Get-AzBackupVaultReport.ps1
Enter fullscreen mode Exit fullscreen mode

Get-AzSiteRecoveryHealthReport.ps1

Shows replication health, RPO and test failover state for every replicated item. It flags anything that has never had a test failover.

./Get-AzSiteRecoveryHealthReport.ps1 -MaxRpoMinutes 15
Enter fullscreen mode Exit fullscreen mode

Other scripts cover backup policies, failed jobs, restore points, turning on backup from a CSV and on-demand backups before a change.

How the scripts are built

  • PowerShell 7.4 or later.
  • Comment-based help on every script. Run Get-Help ./script.ps1 -Full.
  • Reports export to CSV.
  • Change scripts support -WhatIf and -Confirm.
  • Certificate (app-only) or interactive sign-in for Azure.
  • No secrets in the repo. Examples use contoso.com and placeholder GUIDs.

Please test in a test subscription first. Read each script before you run it.

My usual workflow

I run the job report and the protected item report every morning. It takes a minute.

Once a month, I run the gap report. Every VM should be protected or tagged as exempt.

Before a risky change, I take an on-demand backup. It costs little and saves a lot.

One tip. Do a real test restore at least once a quarter. A backup you have never restored is a hope, not a plan.

Getting started

Clone the repo:

git clone https://github.com/anthonybuhnerkemper/azure-backup-dr-powershell-toolkit.git
cd azure-backup-dr-powershell-toolkit
Enter fullscreen mode Exit fullscreen mode

Every toolkit ships the same Connect-M365Admin script. Dot-source it, then connect.

# Interactive sign-in
. ./scripts/Connect/Connect-M365Admin.ps1
Connect-M365Admin -Service Azure -Interactive -SubscriptionId 00000000-0000-0000-0000-000000000000

# Or app-only with a certificate
$env:M365_TENANT_ID       = '00000000-0000-0000-0000-000000000000'
$env:M365_CLIENT_ID       = '00000000-0000-0000-0000-000000000000'
$env:M365_CERT_THUMBPRINT = '0000000000000000000000000000000000000000'
$env:AZ_SUBSCRIPTION_ID   = '00000000-0000-0000-0000-000000000000'
Connect-M365Admin -Service Azure
Enter fullscreen mode Exit fullscreen mode

Run a report, check the CSV, then call Disconnect-M365Admin when you are done. The README lists the modules and roles each script needs.

More toolkits

I have more free toolkits that use the same pattern, including graph-powershell7-admin-toolkit, intune-powershell-toolkit and entra-id-powershell-toolkit.

You can find them all on my GitHub profile.

Feedback welcome

Issues, ideas and pull requests are welcome. What would you add? Tell me in the comments.

Top comments (1)

Collapse
 
mariosfong profile image
Mario Fang •

“新虚拟机上线了,没人给它开备份”这句我太有共鸣了,我这边也出过一模一样的事故:机器跑了三个月,出事才发现根本没进备份策略。你那个 Backup = Exempt 的标签做法很好,把“故意不备份”也变成有记录的决定,而不是默认漏掉。想问一句:Get-AzUnprotectedVmReport 遇到那种曾经受保护、后来策略被删掉的虚拟机,也能发现吗?我这边最怕的就是这种悄悄掉出保护的状态。