删除Azure恢复服务保险库中的所有备份项目,使用PowerShell。

huangapple go评论105阅读模式
英文:

Delete all backup items in Azure Recovery Service Vault with powershell

问题

我正在尝试使用PowerShell删除恢复服务保管库中的所有备份项。我在一个网站上找到了一个脚本,但是该脚本等待备份项被删除后才开始执行以下一个备份项的删除操作。我想修改这个脚本,让Azure立即删除该项并开始下一个,而不需要等待。

我尝试使用标志“-AsJob”,但PowerShell无法识别它(我已经在代码中进行了注释)。

我正在使用PowerShell的版本5。

我需要删除数百个备份项。你能帮助我吗?

英文:

I am trying to delete all backup items in a recovery service vault using powershell. I have found an script in a website but this script waits until a backup is deleted to begin with the following one.
I would like to modify this script to order Azure delete the item and to begin with the follow one without wait.
I tried to use the flag -AsJob but powershell doesn't recognize it (I have commented it in the code).
I am using the version 5 of powershell.

I have to delete hundreds of backup items.

Could you help me?

  1. ## Variables
  2. $rgBackup = Read-Host -Prompt "Resource group name"
  3. $vaultName = Read-Host -Prompt "Recovery service vault name"
  4. $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
  5. $count = 0
  6. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7. ## Disable soft delete for the Azure Backup Recovery Services vault
  8. Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable
  9. Write-Host ($writeEmptyLine + " # Soft delete disabled for Recovery Service vault " + $vault.Name)`
  10. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  11. ## Check if there are backup items in a soft-deleted state and reverse the delete operation
  12. $containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureWorkload -WorkloadType MSSQL -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}
  13. foreach ($item in $containerSoftDelete) {
  14. Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose
  15. }
  16. Write-Host ($writeEmptyLine + "# Undeleted all backup items in a soft deleted state in Recovery Services vault " + $vault.Name)
  17. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  18. ## Stop protection and delete data for all backup-protected items
  19. $containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}
  20. foreach ($item in $containerBackup) {
  21. Disable-AzRecoveryServicesBackupProtection -Item $item -VaultId $vault.ID -RemoveRecoveryPoints -Force -Verbose #-AsJob
  22. $count++
  23. Write-Host "$count - Item has been deleted"
  24. }
  25. Write-Host ($writeEmptyLine + "# Deleted backup date for all cloud protected items in Recovery Services vault " + $vault.Name)`

答案1

得分: 0

在我这一端复制后,我可以使用 PowerShell 版本 7 并使用 foreach-Object -parallel 来并行执行删除作业,从而获得所需的结果。仅供演示,我使用了 Azure 存储文件作为我的备份项目。以下是对我有效的脚本。

  1. ## 变量
  2. $rgBackup = Read-Host -Prompt "资源组名称"
  3. $vaultName = Read-Host -Prompt "恢复服务保管库名称"
  4. $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
  5. $count = 0
  6. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7. ## 禁用 Azure 备份恢复服务保管库的软删除
  8. Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable
  9. Write-Host ($writeEmptyLine + " # 禁用了 $vault.Name 恢复服务保管库的软删除")
  10. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  11. ## 检查是否存在处于软删除状态的备份项目,并撤消删除操作
  12. $containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}
  13. foreach ($item in $containerSoftDelete) {
  14. Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose
  15. }
  16. Write-Host ($writeEmptyLine + "# 撤消了 $vault.Name 恢复服务保管库中处于软删除状态的所有备份项目的删除操作")
  17. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  18. ## 停止保护并删除所有受保护的备份项目数据
  19. $containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID
  20. $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
  21. Set-AzRecoveryServicesVaultContext -Vault $vault
  22. $containerBackup | foreach-Object -parallel {
  23. Disable-AzRecoveryServicesBackupProtection -Item $_ -RemoveRecoveryPoints -Force -Verbose
  24. }
  25. Write-Host ($writeEmptyLine + "# 删除了 $vault.Name 恢复服务保管库中所有云保护项目的备份数据")

结果:

删除Azure恢复服务保险库中的所有备份项目,使用PowerShell。

然而,如果您想要使用 PowerShell 版本 5 进行此操作,您可以按照 Foreach loop parallelly in PowerShell by tutorialspoint.com 中的说明进行操作。

英文:

After reproducing from my end, I could get the desired results using PowerShell version 7 with the help of foreach-Object -parallel to execute deletion jobs parallelly. Fore demonstration purposes, I have used Azure Storage files as my backup items. The following is the script that worked for me.

  1. ## Variables
  2. $rgBackup = Read-Host -Prompt "Resource group name"
  3. $vaultName = Read-Host -Prompt "Recovery service vault name"
  4. $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
  5. $count = 0
  6. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7. ## Disable soft delete for the Azure Backup Recovery Services vault
  8. Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable
  9. Write-Host ($writeEmptyLine + " # Soft delete disabled for Recovery Service vault " + $vault.Name)`
  10. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  11. ## Check if there are backup items in a soft-deleted state and reverse the delete operation
  12. $containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}
  13. foreach ($item in $containerSoftDelete) {
  14. Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose
  15. }
  16. Write-Host ($writeEmptyLine + "# Undeleted all backup items in a soft deleted state in Recovery Services vault " + $vault.Name)
  17. ## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  18. ## Stop protection and delete data for all backup-protected items
  19. $containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID
  20. $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
  21. Set-AzRecoveryServicesVaultContext -Vault $vault
  22. $containerBackup | foreach-Object -parallel {
  23. Disable-AzRecoveryServicesBackupProtection -Item $_ -RemoveRecoveryPoints -Force -Verbose
  24. }
  25. Write-Host ($writeEmptyLine + "# Deleted backup date for all cloud protected items in Recovery Services vault " + $vault.Name)

Results:

删除Azure恢复服务保险库中的所有备份项目,使用PowerShell。

However, if you want to make this work using PowerShell version 5 you can follow Foreach loop parallelly in PowerShell by tutorialspoint.com

答案2

得分: 0

以下是您要翻译的内容的部分翻译:

  1. # Powershell版本 >= 7.0
  2. # If date format error occurs when deleting, try running it from a VM located in North Europe in English, as some machines do not encounter this error.
  3. # Authenticate to Azure
  4. Connect-AzAccount -Tenant "[租户ID]"
  5. Set-AzContext -SubscriptionId "[订阅ID]"
  6. # 变量
  7. [String]$rgBackup = "[资源组]"
  8. [String]$vaultName = "[恢复服务保险库名称]"
  9. # $vaultId = "[恢复服务保险库ID]"
  10. $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
  11. $containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}
  12. $containerBackup | ForEach-Object -ThrottleLimit 10 -Parallel {Write-Host -Fore Green $_.Name; Disable-AzRecoveryServicesBackupProtection -Item $_ -VaultId "[恢复服务保险库ID,不包括变量]" -RemoveRecoveryPoints -Force -Verbose}
  13. Write-Host "********************************* 完成 *********************************************************"

请注意,我已将代码部分翻译成中文,而不包括注释。

英文:

I could solve the problem with this script. It can be improved:

  1. # Powershell version >= 7.0
  2. # Si al eliminar da error de formato de fecha, probar a lanzarlo desde una VM ubicada en North Europe en inglés ya que en algunas máquinas no da este error.
  3. # Autenticarse en Azure
  4. Connect-AzAccount -Tenant "[TENANT ID]"
  5. Set-AzContext -SubscriptionId "[SUBSCRIPTION ID]"
  6. # VARIABLES
  7. [String]$rgBackup = "[RG]"
  8. [String]$vaultName = "[RECOVERY SERVICE VAULT NAME]"
  9. # $vaultId = "[RECOVERY SERVICE VAULT ID]"
  10. $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
  11. $containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}
  12. $containerbackup| ForEach-Object -ThrottleLimit 10 -Parallel {write-host -fore green $_.name; Disable-AzRecoveryServicesBackupProtection -Item $_ -VaultId "[RECOVERY SERVICE VAULT ID WITHOUT VARIABLES]" -RemoveRecoveryPoints -Force -Verbose}
  13. Write-Host "********************************* FIN *********************************************************"

huangapple
  • 本文由 发表于 2023年3月7日 23:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75664117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定