Move Recovery Services Vault Items to new Vault

Intro

This script moves all items with type AzureVM to a new Recovery Services Vault.

For one of my clients I had to set up a new backup strategy. In the old situation they had 200+ Virtual Machines spread over multiple Recovery Services Vaults used only for 2-day snapshots making it safe to delete backup data, since no retention was build up.
I created this script to quickly disable backup jobs, delete the backup data and to move all items over to a new Recovery Services Vault with new backup and retention policies.

Prerequisites

  • The script assumes that you already set up your new Recovery Services Vault and Backup Policy.
  • The script will fail if Soft Delete is turned on. Make sure to turn it off.
  • The script will fail if Immutable Vault is turned on. Make sure to turn it off
  • The script uses the Azure PowerShell Module. Contributor Access on the old and new Recovery Services Vaults are required.

Script

Please not that the script will permanently delete your backup data from the old vault.

## IMPORTANT: DISABLE SOFT DELETE FIRST
## IMPORTANT: DISABLE IMMUTABLE VAULT FIRST
## IMPORTANT: BACKUP DATA IN CURRENT/OLD VAULT WILL BE DESTROYED. THIS IS IRREVERSIBLE!!
## IMPORTANT: USE AT YOUR OWN RISK

$oldvaultname = '' ## Name of the old/current Recovery Services Vault
$newvaultname = '' ## Name of the new Recovery Services Vault
$newvaultpolicyname = '' ## Name of the new Recovery Services Vault Backup Policy

$oldvault = Get-AzRecoveryServicesVault -Name $oldvaultname
$container = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -VaultId $oldvault.ID
$newvault = Get-AzRecoveryServicesVault -Name $newvaultname 
$newvaultpolicy = Get-AzRecoveryServicesBackupProtectionPolicy -Name $newvaultpolicyname -VaultId $newvault.ID

foreach ($vm in $container) {
    $backupitemcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -FriendlyName $vm.FriendlyName -VaultId $oldvault.ID 
    $backupitem = Get-AzRecoveryServicesBackupItem -Container $backupitemcontainer -WorkloadType AzureVM -VaultID $oldvault.ID
    Disable-AzRecoveryServicesBackupProtection -Item $backupitem -VaultId $oldvault.ID -RemoveRecoveryPoints -Force -Verbose
    Enable-AzRecoveryServicesBackupProtection -Policy $newvaultpolicy -Name $vm.FriendlyName -ResourceGroupName $vm.ResourceGroupName -VaultId $newvault.ID -Verbose
}
1

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *