禁用备份后使用 powershell 脚本恢复 Azure VM 备份
Resume Azure VM backup using powershell script after disabling backup
我想在禁用备份后恢复我的备份。
使用以下命令禁用和启用。
禁用备份:Disable-AzRecoveryServicesBackupProtection -Item $backupItems -Force
启用备份:Enable-AzRecoveryServicesBackupProtection
现在,如果我使用 运行 Enable-AzRecoveryServicesBackupProtection 命令,则会出现错误。我的用例是我想检查 RSV 是否存在,如果 RSV 不存在,那么我应该创建新的 RSV,新策略,启用策略和 运行 备份并使用上述命令禁用备份。现在,如果我再次 运行 相同的脚本,并且由于 RSV 已经与策略一起创建,现在我只想恢复我的备份(当我第一次 运行 我的脚本时,我已经禁用了我的备份)。仅为此我无法找到任何 powershell command.If 我 运行 命令 Enable-AzRecoveryServicesBackupProtection 然后它失败并显示以下 error.So 请帮助我了解如何使用 powershell 脚本处理此问题。整个步骤已通过 powershell 脚本完成。
错误:
##[错误]项目未受保护。请检查项目是否与任何政策相关联。
##[错误]PowerShell 退出,代码为“1”。
此外,我的 powershell 脚本应该可以处理以下情况。
生产环境的要求:我的 powershell 脚本应该只 运行 使用已经存在的策略进行备份,这可能与我在 powershell 脚本中声明的策略不同并且已经备份 运行ning .所以在这里我应该只 运行 在 prod 环境中备份,不管它 using.How 处理这种情况的策略是什么?
VMBACKUP.ps1
####################### Parameters for the VM Backup Process #######################
param (
[Parameter(Mandatory = $false)][String]$ResourceGroupName,
[Parameter(Mandatory = $false)][String]$SourceVMname,
[Parameter(Mandatory = $false)][String]$RSVaultname,
[Parameter(Mandatory = $false)][String]$PolicyName,
[Parameter(Mandatory = $false)][String]$Location,
[Parameter(Mandatory = $false)][String]$Storagetype,
[Parameter(Mandatory = $false)][String]$WorkLoadType
)
####################### Connecting to Azure via Service principal and install the AZ modules #######################
Write-Host "Check for AZ modules and install the AZ modules if not available"
if (-not(Get-Module -Name Az.Accounts -ListAvailable)) {
Write-Warning "Module 'Az.Accounts' is missing or out of date. Installing module now."
Install-Module -Name Az.Accounts, Az.Resources, Az.Automation, Az.RecoveryServices -Scope CurrentUser -Force -AllowClobber
}
####################### Registering the Azure Recovery Service provider in the subscription #######################
Write-Host "Registering the Azure Recovery Service provider in the subscription"
try {
Register-AzResourceProvider -ProviderNamespace "Microsoft.RecoveryServices" -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Check if RSV is exist,create if not exist already and setup vault context #######################
Write-Host "Check if RSV is exist,create if not exist already and setup vault context"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
Set-AzRecoveryServicesVaultContext -Vault $targetVault -ErrorVariable notPresent -ErrorAction SilentlyContinue
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Fetching the right container and backup items #######################
Write-Host "Fetching the right container and backup items"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Check if RSV is exist already,if exist then go to else block ###############
Write-Host "Checking if RSV already exist"
if ($notPresent) {
New-AzRecoveryServicesVault -Name $RSVaultname -ResourceGroupName $ResourceGroupName -Location $Location
####################### Fetching vault resource values #######################
Write-Host "Fetching vault resource values"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
Set-AzRecoveryServicesVaultContext -Vault $targetVault
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype
####################### Setting up new backup policy #######################
Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent) {
Write-Host "Setting up a new valid Backup Policy"
$schPol = Get-AzRecoveryServicesBackupSchedulePolicyObject -WorkloadType $WorkLoadType
$UtcTime = Get-Date -Date 0001-01-01T00:00:00 #This is the time that you want to start the backup
$UtcTime = $UtcTime.ToUniversalTime()
$schpol.ScheduleRunTimes[0] = $UtcTime
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType $WorkLoadType
New-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -WorkloadType $WorkLoadType -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $targetVault.ID
}
$pol = Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID
$pol
####################### Enable Backup protection in the new Policy #######################
Write-host "Enable Backup protection in the $PolicyName"
try {
Enable-AzRecoveryServicesBackupProtection -ResourceGroupName $ResourceGroupName -Name $SourceVMname -Policy $pol -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Fetching the right container and backup items #######################
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
###################### Calling backup script #######################
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
####################### Disable backup protection in the RSV #######################
Write-Host "Disable backup protection in the RSV"
try {
$containers = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname
$backupItems = Get-AzRecoveryServicesBackupItem -Container $containers[0] -WorkloadType $WorkLoadType
# Disble the backup protection in RSV to delete backup items
Disable-AzRecoveryServicesBackupProtection -Item $backupItems -Force
Write-Host "Backup is successfully disabled"
}
catch {
$message = $_
Write-Warning "An error occured! $message"
}
}
###################### RSV is already exist,run backup script ##################### }
else {
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
}
创建备份。ps1
####################### Backup process starts now ##########################
####################### Checking the status of backup jobs and wait until its configuring the backup #######################
Write-Host "Checking the status of Configure backup jobs and wait until its configuring the backup"
$ConfigureBackup = Get-AzRecoveryservicesBackupJob
while ($ConfigureBackup.Status -eq "In progress") {
start-sleep -s 30
write-host "Configure Backup is still in progress"
}
####################### Backup process begins #######################
Write-Host "Backup process begins"
$backupjob = Backup-AzRecoveryServicesBackupItem -Item $item
$backupjob
start-sleep -s 60
####################### Fail the pipeline if backup is failed #######################
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
####################### Get the Backup Status #######################
Write-Host "Current Backup Status"
Get-AzRecoveryservicesBackupJob
####################### Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete #######################
Write-Host "Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete"
$Jobs = Get-AzRecoveryServicesBackupJob -Status InProgress -VaultId $targetVault.ID
$JobId = $Jobs[0].JobId
DO {
$subtasks = Get-AzRecoveryServicesBackupJobDetail -JobId $JobId -VaultId $targetVault.ID
$subtask = $subtasks.SubTasks | Where-Object { $_.Name -eq "Take Snapshot" }
$subtask.Status
Start-Sleep -Seconds 20
}UNTIL ($subtask.Status -eq "Completed")
####################### Fail the pipeline if backup is failed #######################
try {
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
start-sleep -s 30
####################### Here is the final backup status #######################
Write-Host "Here is the final backup status"
Get-AzRecoveryservicesBackupJob
Write-Output "Backup process is completed successfully"
我能够解决我的问题并能够成功备份多个场景。
**vmbackup.ps1:**
####################### Parameters for the VM Backup Process #######################
param (
[Parameter(Mandatory = $false)][String]$ResourceGroupName,
[Parameter(Mandatory = $false)][String]$SourceVMname,
[Parameter(Mandatory = $false)][String]$RSVaultname,
[Parameter(Mandatory = $false)][String]$PolicyName,
[Parameter(Mandatory = $false)][String]$Location,
[Parameter(Mandatory = $false)][String]$Storagetype,
[Parameter(Mandatory = $false)][String]$WorkLoadType,
[Parameter(Mandatory = $false)][String]$PolicyName2
)
####################### Connecting to Azure via Service principal and install the AZ modules #######################
Write-Host "Check for AZ modules and install the AZ modules if not available"
if (-not(Get-Module -Name Az.Accounts -ListAvailable)) {
Write-Warning "Module 'Az.Accounts' is missing or out of date. Installing module now."
Install-Module -Name Az.Accounts, Az.Resources, Az.Automation, Az.RecoveryServices -Scope CurrentUser -Force -AllowClobber
}
####################### Registering the Azure Recovery Service provider in the subscription #######################
Write-Host "Registering the Azure Recovery Service provider in the subscription"
try {
Register-AzResourceProvider -ProviderNamespace "Microsoft.RecoveryServices" -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Check if RSV is exist,create if not exist already and setup vault context #######################
Write-Host "Check if RSV is exist,create if not exist already and setup vault context"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
Set-AzRecoveryServicesVaultContext -Vault $targetVault -ErrorVariable notPresent -ErrorAction SilentlyContinue
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Fetching the right container and backup items #######################
Write-Host "Fetching the right container and backup items"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Check if RSV is exist already,if exist then go to else block ###############
Write-Host "Checking if RSV already exist"
if ($notPresent) {
New-AzRecoveryServicesVault -Name $RSVaultname -ResourceGroupName $ResourceGroupName -Location $Location
####################### Fetching vault resource values #######################
Write-Host "Fetching vault resource values"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
Set-AzRecoveryServicesVaultContext -Vault $targetVault
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype
####################### Setting up new backup policy #######################
Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID -ErrorVariable notPresent -ErrorAction SilentlyContinue
Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName2 -VaultId $targetVault.ID -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent) {
Write-Host "Setting up a new valid Backup Policy"
$schPol = Get-AzRecoveryServicesBackupSchedulePolicyObject -WorkloadType $WorkLoadType
$UtcTime = Get-Date -Date 0001-01-01T00:00:00 #This is the time that you want to start the backup
$UtcTime = $UtcTime.ToUniversalTime()
$schpol.ScheduleRunTimes[0] = $UtcTime
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType $WorkLoadType
New-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -WorkloadType $WorkLoadType -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $targetVault.ID
}
$pol = Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID
$pol
####################### Enable Backup protection in the new Policy #######################
Write-host "Enable Backup protection in the $PolicyName"
try {
Enable-AzRecoveryServicesBackupProtection -ResourceGroupName $ResourceGroupName -Name $SourceVMname -Policy $pol -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Fetching the right container and backup items #######################
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
###################### Calling backup script #######################
#$present=0
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
####################### Disable backup protection in the RSV #######################
Write-Host "Disable backup protection in the RSV"
try {
$containers = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname
$backupItems = Get-AzRecoveryServicesBackupItem -Container $containers[0] -WorkloadType $WorkLoadType
# Disble the backup protection in RSV to delete backup items
Disable-AzRecoveryServicesBackupProtection -Item $backupItems -Force
Write-Host "Backup is successfully disabled"
}
catch {
$message = $_
Write-Warning "An error occured! $message"
}
}
###################### RSV is already exist,run backup script ##################### }
else {
#$present=1
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
}
创建备份。ps1:
####################### Changing the backup policy to enable backup again if disabled ##########################
Write-Host "Check VM backup status"
$status = Get-AzRecoveryServicesBackupStatus -Name $SourceVMname -ResourceGroupName $ResourceGroupName -Type "AzureVM"
If ($status.VaultId) {
$rsv = $status.VaultId.Split('/')[-1]
Write-Output "The VM < $SourceVMname > is member of RSV < $rsv >"
$Disablestatus1 = Get-AzRecoveryservicesBackupJob
Write-Host "Overall jobs status"
$Disablestatus1
Write-Host "Initial job status"
$Disablestatus1[0]
if (($Disablestatus1[0].Operation -eq "Disablebackup") -and ($Disablestatus1[0].Status -eq "Completed")) {
$Disablestatus2 = Get-AzRecoveryServicesBackupJob -VaultId $targetVault.ID -Operation Disablebackup
Write-Host "Overall disable jobs backup status"
$Disablestatus2
Write-Host "disable initial backup status"
$Disablestatus2[0]
If ($Disablestatus2.Status -eq "Completed") {
Write-Output "The VM < $SourceVMname > is not protected with Azure Backup. You should consider enabling it!"
Write-Host "Changing the backup policy to resume backup again since its disabled in the last run"
$schPol = Get-AzRecoveryServicesBackupSchedulePolicyObject -WorkloadType $WorkLoadType
$UtcTime = Get-Date -Date 0001-01-02T00:00:00 #This is the time that you want to start the backup
$UtcTime = $UtcTime.ToUniversalTime()
$schpol.ScheduleRunTimes[0] = $UtcTime
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType $WorkLoadType
New-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName2 -WorkloadType $WorkLoadType -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $targetVault.ID
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname
$TargetPol1 = Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName2 -VaultId $targetVault.ID
$anotherBkpItem = Get-AzRecoveryServicesBackupItem -WorkloadType $WorkLoadType -BackupManagementType $WorkLoadType -Name $SourceVMname -VaultId $targetVault.ID
Enable-AzRecoveryServicesBackupProtection -Item $anotherBkpItem -Policy $TargetPol1 -VaultId $targetVault.ID
}
}}
####################### Actual Backup process starts now ##########################
####################### Checking the status of backup jobs and wait until its configuring the backup #######################
Write-Host "Checking the status of Configure backup jobs and wait until its configuring the backup"
$ConfigureBackup = Get-AzRecoveryservicesBackupJob
while ($ConfigureBackup.Status -eq "In progress") {
start-sleep -s 30
write-host "Configure Backup is still in progress"
}
####################### Backup process begins #######################
Write-Host "Actual Backup process begins"
$backupjob = Backup-AzRecoveryServicesBackupItem -Item $item
$backupjob
start-sleep -s 60
####################### Fail the pipeline if backup is failed #######################
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
####################### Get the Backup Status #######################
Write-Host "Current Backup Status"
Get-AzRecoveryservicesBackupJob
####################### Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete #######################
Write-Host "Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete"
$Jobs = Get-AzRecoveryServicesBackupJob -Status InProgress -VaultId $targetVault.ID
$JobId = $Jobs[0].JobId
DO {
$subtasks = Get-AzRecoveryServicesBackupJobDetail -JobId $JobId -VaultId $targetVault.ID
$subtask = $subtasks.SubTasks | Where-Object { $_.Name -eq "Take Snapshot" }
$subtask.Status
Start-Sleep -Seconds 20
}UNTIL ($subtask.Status -eq "Completed")
####################### Fail the pipeline if backup is failed #######################
try {
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
start-sleep -s 30
####################### Here is the final backup status #######################
Write-Host "Here is the final backup status"
Get-AzRecoveryservicesBackupJob
Write-Output "Backup process is completed successfully"
我想在禁用备份后恢复我的备份。 使用以下命令禁用和启用。 禁用备份:Disable-AzRecoveryServicesBackupProtection -Item $backupItems -Force 启用备份:Enable-AzRecoveryServicesBackupProtection
现在,如果我使用 运行 Enable-AzRecoveryServicesBackupProtection 命令,则会出现错误。我的用例是我想检查 RSV 是否存在,如果 RSV 不存在,那么我应该创建新的 RSV,新策略,启用策略和 运行 备份并使用上述命令禁用备份。现在,如果我再次 运行 相同的脚本,并且由于 RSV 已经与策略一起创建,现在我只想恢复我的备份(当我第一次 运行 我的脚本时,我已经禁用了我的备份)。仅为此我无法找到任何 powershell command.If 我 运行 命令 Enable-AzRecoveryServicesBackupProtection 然后它失败并显示以下 error.So 请帮助我了解如何使用 powershell 脚本处理此问题。整个步骤已通过 powershell 脚本完成。
错误:
##[错误]项目未受保护。请检查项目是否与任何政策相关联。 ##[错误]PowerShell 退出,代码为“1”。
此外,我的 powershell 脚本应该可以处理以下情况。
生产环境的要求:我的 powershell 脚本应该只 运行 使用已经存在的策略进行备份,这可能与我在 powershell 脚本中声明的策略不同并且已经备份 运行ning .所以在这里我应该只 运行 在 prod 环境中备份,不管它 using.How 处理这种情况的策略是什么?
VMBACKUP.ps1
####################### Parameters for the VM Backup Process #######################
param (
[Parameter(Mandatory = $false)][String]$ResourceGroupName,
[Parameter(Mandatory = $false)][String]$SourceVMname,
[Parameter(Mandatory = $false)][String]$RSVaultname,
[Parameter(Mandatory = $false)][String]$PolicyName,
[Parameter(Mandatory = $false)][String]$Location,
[Parameter(Mandatory = $false)][String]$Storagetype,
[Parameter(Mandatory = $false)][String]$WorkLoadType
)
####################### Connecting to Azure via Service principal and install the AZ modules #######################
Write-Host "Check for AZ modules and install the AZ modules if not available"
if (-not(Get-Module -Name Az.Accounts -ListAvailable)) {
Write-Warning "Module 'Az.Accounts' is missing or out of date. Installing module now."
Install-Module -Name Az.Accounts, Az.Resources, Az.Automation, Az.RecoveryServices -Scope CurrentUser -Force -AllowClobber
}
####################### Registering the Azure Recovery Service provider in the subscription #######################
Write-Host "Registering the Azure Recovery Service provider in the subscription"
try {
Register-AzResourceProvider -ProviderNamespace "Microsoft.RecoveryServices" -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Check if RSV is exist,create if not exist already and setup vault context #######################
Write-Host "Check if RSV is exist,create if not exist already and setup vault context"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
Set-AzRecoveryServicesVaultContext -Vault $targetVault -ErrorVariable notPresent -ErrorAction SilentlyContinue
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Fetching the right container and backup items #######################
Write-Host "Fetching the right container and backup items"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Check if RSV is exist already,if exist then go to else block ###############
Write-Host "Checking if RSV already exist"
if ($notPresent) {
New-AzRecoveryServicesVault -Name $RSVaultname -ResourceGroupName $ResourceGroupName -Location $Location
####################### Fetching vault resource values #######################
Write-Host "Fetching vault resource values"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
Set-AzRecoveryServicesVaultContext -Vault $targetVault
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype
####################### Setting up new backup policy #######################
Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent) {
Write-Host "Setting up a new valid Backup Policy"
$schPol = Get-AzRecoveryServicesBackupSchedulePolicyObject -WorkloadType $WorkLoadType
$UtcTime = Get-Date -Date 0001-01-01T00:00:00 #This is the time that you want to start the backup
$UtcTime = $UtcTime.ToUniversalTime()
$schpol.ScheduleRunTimes[0] = $UtcTime
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType $WorkLoadType
New-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -WorkloadType $WorkLoadType -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $targetVault.ID
}
$pol = Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID
$pol
####################### Enable Backup protection in the new Policy #######################
Write-host "Enable Backup protection in the $PolicyName"
try {
Enable-AzRecoveryServicesBackupProtection -ResourceGroupName $ResourceGroupName -Name $SourceVMname -Policy $pol -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Fetching the right container and backup items #######################
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
###################### Calling backup script #######################
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
####################### Disable backup protection in the RSV #######################
Write-Host "Disable backup protection in the RSV"
try {
$containers = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname
$backupItems = Get-AzRecoveryServicesBackupItem -Container $containers[0] -WorkloadType $WorkLoadType
# Disble the backup protection in RSV to delete backup items
Disable-AzRecoveryServicesBackupProtection -Item $backupItems -Force
Write-Host "Backup is successfully disabled"
}
catch {
$message = $_
Write-Warning "An error occured! $message"
}
}
###################### RSV is already exist,run backup script ##################### }
else {
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
}
创建备份。ps1
####################### Backup process starts now ##########################
####################### Checking the status of backup jobs and wait until its configuring the backup #######################
Write-Host "Checking the status of Configure backup jobs and wait until its configuring the backup"
$ConfigureBackup = Get-AzRecoveryservicesBackupJob
while ($ConfigureBackup.Status -eq "In progress") {
start-sleep -s 30
write-host "Configure Backup is still in progress"
}
####################### Backup process begins #######################
Write-Host "Backup process begins"
$backupjob = Backup-AzRecoveryServicesBackupItem -Item $item
$backupjob
start-sleep -s 60
####################### Fail the pipeline if backup is failed #######################
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
####################### Get the Backup Status #######################
Write-Host "Current Backup Status"
Get-AzRecoveryservicesBackupJob
####################### Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete #######################
Write-Host "Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete"
$Jobs = Get-AzRecoveryServicesBackupJob -Status InProgress -VaultId $targetVault.ID
$JobId = $Jobs[0].JobId
DO {
$subtasks = Get-AzRecoveryServicesBackupJobDetail -JobId $JobId -VaultId $targetVault.ID
$subtask = $subtasks.SubTasks | Where-Object { $_.Name -eq "Take Snapshot" }
$subtask.Status
Start-Sleep -Seconds 20
}UNTIL ($subtask.Status -eq "Completed")
####################### Fail the pipeline if backup is failed #######################
try {
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
start-sleep -s 30
####################### Here is the final backup status #######################
Write-Host "Here is the final backup status"
Get-AzRecoveryservicesBackupJob
Write-Output "Backup process is completed successfully"
我能够解决我的问题并能够成功备份多个场景。
**vmbackup.ps1:**
####################### Parameters for the VM Backup Process #######################
param (
[Parameter(Mandatory = $false)][String]$ResourceGroupName,
[Parameter(Mandatory = $false)][String]$SourceVMname,
[Parameter(Mandatory = $false)][String]$RSVaultname,
[Parameter(Mandatory = $false)][String]$PolicyName,
[Parameter(Mandatory = $false)][String]$Location,
[Parameter(Mandatory = $false)][String]$Storagetype,
[Parameter(Mandatory = $false)][String]$WorkLoadType,
[Parameter(Mandatory = $false)][String]$PolicyName2
)
####################### Connecting to Azure via Service principal and install the AZ modules #######################
Write-Host "Check for AZ modules and install the AZ modules if not available"
if (-not(Get-Module -Name Az.Accounts -ListAvailable)) {
Write-Warning "Module 'Az.Accounts' is missing or out of date. Installing module now."
Install-Module -Name Az.Accounts, Az.Resources, Az.Automation, Az.RecoveryServices -Scope CurrentUser -Force -AllowClobber
}
####################### Registering the Azure Recovery Service provider in the subscription #######################
Write-Host "Registering the Azure Recovery Service provider in the subscription"
try {
Register-AzResourceProvider -ProviderNamespace "Microsoft.RecoveryServices" -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Check if RSV is exist,create if not exist already and setup vault context #######################
Write-Host "Check if RSV is exist,create if not exist already and setup vault context"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
Set-AzRecoveryServicesVaultContext -Vault $targetVault -ErrorVariable notPresent -ErrorAction SilentlyContinue
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Fetching the right container and backup items #######################
Write-Host "Fetching the right container and backup items"
$e = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
}
catch {
$ErrorActionPreference = $e
}
$ErrorActionPreference = $e;
####################### Check if RSV is exist already,if exist then go to else block ###############
Write-Host "Checking if RSV already exist"
if ($notPresent) {
New-AzRecoveryServicesVault -Name $RSVaultname -ResourceGroupName $ResourceGroupName -Location $Location
####################### Fetching vault resource values #######################
Write-Host "Fetching vault resource values"
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname
$targetVault.ID
####################### Setting up VaultContext using Vault and Setting up Storage redundancy #######################
Write-Host "Setting up VaultContext using Vault and Setting up Storage redundancy"
Set-AzRecoveryServicesVaultContext -Vault $targetVault
Set-AzRecoveryServicesBackupProperty -Vault $targetVault -BackupStorageRedundancy $Storagetype
####################### Setting up new backup policy #######################
Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID -ErrorVariable notPresent -ErrorAction SilentlyContinue
Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName2 -VaultId $targetVault.ID -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent) {
Write-Host "Setting up a new valid Backup Policy"
$schPol = Get-AzRecoveryServicesBackupSchedulePolicyObject -WorkloadType $WorkLoadType
$UtcTime = Get-Date -Date 0001-01-01T00:00:00 #This is the time that you want to start the backup
$UtcTime = $UtcTime.ToUniversalTime()
$schpol.ScheduleRunTimes[0] = $UtcTime
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType $WorkLoadType
New-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -WorkloadType $WorkLoadType -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $targetVault.ID
}
$pol = Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName -VaultId $targetVault.ID
$pol
####################### Enable Backup protection in the new Policy #######################
Write-host "Enable Backup protection in the $PolicyName"
try {
Enable-AzRecoveryServicesBackupProtection -ResourceGroupName $ResourceGroupName -Name $SourceVMname -Policy $pol -ErrorAction SilentlyContinue
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
####################### Fetching the right container and backup items #######################
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname -ErrorVariable notPresent -ErrorAction SilentlyContinue
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType $WorkLoadType -ErrorVariable notPresent -ErrorAction SilentlyContinue
###################### Calling backup script #######################
#$present=0
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
####################### Disable backup protection in the RSV #######################
Write-Host "Disable backup protection in the RSV"
try {
$containers = Get-AzRecoveryServicesBackupContainer -ContainerType $WorkLoadType -FriendlyName $SourceVMname
$backupItems = Get-AzRecoveryServicesBackupItem -Container $containers[0] -WorkloadType $WorkLoadType
# Disble the backup protection in RSV to delete backup items
Disable-AzRecoveryServicesBackupProtection -Item $backupItems -Force
Write-Host "Backup is successfully disabled"
}
catch {
$message = $_
Write-Warning "An error occured! $message"
}
}
###################### RSV is already exist,run backup script ##################### }
else {
#$present=1
Invoke-Expression "$PSScriptRoot\createbackup.ps1"
}
创建备份。ps1:
####################### Changing the backup policy to enable backup again if disabled ##########################
Write-Host "Check VM backup status"
$status = Get-AzRecoveryServicesBackupStatus -Name $SourceVMname -ResourceGroupName $ResourceGroupName -Type "AzureVM"
If ($status.VaultId) {
$rsv = $status.VaultId.Split('/')[-1]
Write-Output "The VM < $SourceVMname > is member of RSV < $rsv >"
$Disablestatus1 = Get-AzRecoveryservicesBackupJob
Write-Host "Overall jobs status"
$Disablestatus1
Write-Host "Initial job status"
$Disablestatus1[0]
if (($Disablestatus1[0].Operation -eq "Disablebackup") -and ($Disablestatus1[0].Status -eq "Completed")) {
$Disablestatus2 = Get-AzRecoveryServicesBackupJob -VaultId $targetVault.ID -Operation Disablebackup
Write-Host "Overall disable jobs backup status"
$Disablestatus2
Write-Host "disable initial backup status"
$Disablestatus2[0]
If ($Disablestatus2.Status -eq "Completed") {
Write-Output "The VM < $SourceVMname > is not protected with Azure Backup. You should consider enabling it!"
Write-Host "Changing the backup policy to resume backup again since its disabled in the last run"
$schPol = Get-AzRecoveryServicesBackupSchedulePolicyObject -WorkloadType $WorkLoadType
$UtcTime = Get-Date -Date 0001-01-02T00:00:00 #This is the time that you want to start the backup
$UtcTime = $UtcTime.ToUniversalTime()
$schpol.ScheduleRunTimes[0] = $UtcTime
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType $WorkLoadType
New-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName2 -WorkloadType $WorkLoadType -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $targetVault.ID
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $RSVaultname
$TargetPol1 = Get-AzRecoveryServicesBackupProtectionPolicy -Name $PolicyName2 -VaultId $targetVault.ID
$anotherBkpItem = Get-AzRecoveryServicesBackupItem -WorkloadType $WorkLoadType -BackupManagementType $WorkLoadType -Name $SourceVMname -VaultId $targetVault.ID
Enable-AzRecoveryServicesBackupProtection -Item $anotherBkpItem -Policy $TargetPol1 -VaultId $targetVault.ID
}
}}
####################### Actual Backup process starts now ##########################
####################### Checking the status of backup jobs and wait until its configuring the backup #######################
Write-Host "Checking the status of Configure backup jobs and wait until its configuring the backup"
$ConfigureBackup = Get-AzRecoveryservicesBackupJob
while ($ConfigureBackup.Status -eq "In progress") {
start-sleep -s 30
write-host "Configure Backup is still in progress"
}
####################### Backup process begins #######################
Write-Host "Actual Backup process begins"
$backupjob = Backup-AzRecoveryServicesBackupItem -Item $item
$backupjob
start-sleep -s 60
####################### Fail the pipeline if backup is failed #######################
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
####################### Get the Backup Status #######################
Write-Host "Current Backup Status"
Get-AzRecoveryservicesBackupJob
####################### Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete #######################
Write-Host "Look for the backup subtask: Take Snapshot in RSV for Backup job and wait for it to complete"
$Jobs = Get-AzRecoveryServicesBackupJob -Status InProgress -VaultId $targetVault.ID
$JobId = $Jobs[0].JobId
DO {
$subtasks = Get-AzRecoveryServicesBackupJobDetail -JobId $JobId -VaultId $targetVault.ID
$subtask = $subtasks.SubTasks | Where-Object { $_.Name -eq "Take Snapshot" }
$subtask.Status
Start-Sleep -Seconds 20
}UNTIL ($subtask.Status -eq "Completed")
####################### Fail the pipeline if backup is failed #######################
try {
if ($backupjob.Status -eq "Failed") {
throw "Backup is failed"
}
}
catch {
$message = $_
Write-Warning "An error occurred! $message"
}
start-sleep -s 30
####################### Here is the final backup status #######################
Write-Host "Here is the final backup status"
Get-AzRecoveryservicesBackupJob
Write-Output "Backup process is completed successfully"