英文:
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?
## Variables
$rgBackup = Read-Host -Prompt "Resource group name"
$vaultName = Read-Host -Prompt "Recovery service vault name"
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
$count = 0
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Disable soft delete for the Azure Backup Recovery Services vault
Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable
Write-Host ($writeEmptyLine + " # Soft delete disabled for Recovery Service vault " + $vault.Name)`
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Check if there are backup items in a soft-deleted state and reverse the delete operation
$containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureWorkload -WorkloadType MSSQL -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}
foreach ($item in $containerSoftDelete) {
Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose
}
Write-Host ($writeEmptyLine + "# Undeleted all backup items in a soft deleted state in Recovery Services vault " + $vault.Name)
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Stop protection and delete data for all backup-protected items
$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}
foreach ($item in $containerBackup) {
Disable-AzRecoveryServicesBackupProtection -Item $item -VaultId $vault.ID -RemoveRecoveryPoints -Force -Verbose #-AsJob
$count++
Write-Host "$count - Item has been deleted"
}
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 存储文件作为我的备份项目。以下是对我有效的脚本。
## 变量
$rgBackup = Read-Host -Prompt "资源组名称"
$vaultName = Read-Host -Prompt "恢复服务保管库名称"
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
$count = 0
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## 禁用 Azure 备份恢复服务保管库的软删除
Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable
Write-Host ($writeEmptyLine + " # 禁用了 $vault.Name 恢复服务保管库的软删除")
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## 检查是否存在处于软删除状态的备份项目,并撤消删除操作
$containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}
foreach ($item in $containerSoftDelete) {
Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose
}
Write-Host ($writeEmptyLine + "# 撤消了 $vault.Name 恢复服务保管库中处于软删除状态的所有备份项目的删除操作")
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## 停止保护并删除所有受保护的备份项目数据
$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
Set-AzRecoveryServicesVaultContext -Vault $vault
$containerBackup | foreach-Object -parallel {
Disable-AzRecoveryServicesBackupProtection -Item $_ -RemoveRecoveryPoints -Force -Verbose
}
Write-Host ($writeEmptyLine + "# 删除了 $vault.Name 恢复服务保管库中所有云保护项目的备份数据")
结果:
然而,如果您想要使用 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.
## Variables
$rgBackup = Read-Host -Prompt "Resource group name"
$vaultName = Read-Host -Prompt "Recovery service vault name"
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
$count = 0
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Disable soft delete for the Azure Backup Recovery Services vault
Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable
Write-Host ($writeEmptyLine + " # Soft delete disabled for Recovery Service vault " + $vault.Name)`
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Check if there are backup items in a soft-deleted state and reverse the delete operation
$containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}
foreach ($item in $containerSoftDelete) {
Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose
}
Write-Host ($writeEmptyLine + "# Undeleted all backup items in a soft deleted state in Recovery Services vault " + $vault.Name)
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Stop protection and delete data for all backup-protected items
$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
Set-AzRecoveryServicesVaultContext -Vault $vault
$containerBackup | foreach-Object -parallel {
Disable-AzRecoveryServicesBackupProtection -Item $_ -RemoveRecoveryPoints -Force -Verbose
}
Write-Host ($writeEmptyLine + "# Deleted backup date for all cloud protected items in Recovery Services vault " + $vault.Name)
Results:
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
以下是您要翻译的内容的部分翻译:
# Powershell版本 >= 7.0
# 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.
# Authenticate to Azure
Connect-AzAccount -Tenant "[租户ID]"
Set-AzContext -SubscriptionId "[订阅ID]"
# 变量
[String]$rgBackup = "[资源组]"
[String]$vaultName = "[恢复服务保险库名称]"
# $vaultId = "[恢复服务保险库ID]"
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}
$containerBackup | ForEach-Object -ThrottleLimit 10 -Parallel {Write-Host -Fore Green $_.Name; Disable-AzRecoveryServicesBackupProtection -Item $_ -VaultId "[恢复服务保险库ID,不包括变量]" -RemoveRecoveryPoints -Force -Verbose}
Write-Host "********************************* 完成 *********************************************************"
请注意,我已将代码部分翻译成中文,而不包括注释。
英文:
I could solve the problem with this script. It can be improved:
# Powershell version >= 7.0
# 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.
# Autenticarse en Azure
Connect-AzAccount -Tenant "[TENANT ID]"
Set-AzContext -SubscriptionId "[SUBSCRIPTION ID]"
# VARIABLES
[String]$rgBackup = "[RG]"
[String]$vaultName = "[RECOVERY SERVICE VAULT NAME]"
# $vaultId = "[RECOVERY SERVICE VAULT ID]"
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}
$containerbackup| ForEach-Object -ThrottleLimit 10 -Parallel {write-host -fore green $_.name; Disable-AzRecoveryServicesBackupProtection -Item $_ -VaultId "[RECOVERY SERVICE VAULT ID WITHOUT VARIABLES]" -RemoveRecoveryPoints -Force -Verbose}
Write-Host "********************************* FIN *********************************************************"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论