You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Restore-AzRecoveryServicesBackupItem cmdlet实现还原为文件自动化?

自动化实现Recovery Services备份的文件级还原及跨订阅转移

步骤1:初始化Recovery Services保管库上下文

先完成Azure身份验证并绑定目标保管库上下文(自动化场景推荐用服务主体):

# 服务主体方式登录Azure
Connect-AzAccount -ServicePrincipal -ApplicationId "你的应用ID" -TenantId "租户ID" -CertificateThumbprint "证书指纹"

# 定位并设置保管库上下文
$vault = Get-AzRecoveryServicesVault -Name "你的保管库名称" -ResourceGroupName "保管库所属资源组"
Set-AzRecoveryServicesVaultContext -Vault $vault

步骤2:获取最新完整备份恢复点

定位目标备份项,筛选出最新的完整备份恢复点:

# 获取备份容器(以Azure VM为例,根据实际备份类型调整ContainerType)
$container = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -FriendlyName "目标VM名称"

# 获取对应备份项
$backupItem = Get-AzRecoveryServicesBackupItem -Container $container -WorkloadType AzureVM

# 筛选最新的完整备份恢复点
$recoveryPoints = Get-AzRecoveryServicesBackupRecoveryPoint -Item $backupItem -BackupManagementType AzureVM -RecoveryPointType Full
$latestFullRecoveryPoint = $recoveryPoints | Sort-Object RecoveryPointTime -Descending | Select-Object -First 1

步骤3:执行“还原为文件”操作

核心是指定-RestoreFileType File参数,配置还原目标存储路径:

# 配置还原参数(示例:还原到临时存储账户的文件共享)
$restoreConfig = @{
    Item = $backupItem
    RecoveryPoint = $latestFullRecoveryPoint
    RestoreFileType = "File"
    TargetResourceGroupName = "临时存储资源组名"
    TargetStorageAccountName = "临时存储账户名"
    TargetFileShareName = "目标文件共享名"
    RestoreDirectory = "还原子目录" # 可选,指定文件共享内的存放路径
}

# 启动还原作业
$restoreJob = Restore-AzRecoveryServicesBackupItem @restoreConfig

# 等待还原完成(自动化场景循环检查状态)
do {
    $jobStatus = Get-AzRecoveryServicesBackupJob -Job $restoreJob
    Write-Host "当前还原状态:$($jobStatus.Status)"
    Start-Sleep -Seconds 30
} while ($jobStatus.Status -ne "Completed")

步骤4:跨订阅转移还原文件

还原完成后,将文件复制到目标订阅的存储资源:

# 生成源文件共享的SAS令牌(有效期2小时)
$sourceStorage = Get-AzStorageAccount -ResourceGroupName "临时存储资源组名" -Name "临时存储账户名"
$sasToken = New-AzStorageShareSASToken -Context $sourceStorage.Context -ShareName "目标文件共享名" -Permission rdl -ExpiryTime (Get-Date).AddHours(2)

# 切换到目标订阅
Set-AzContext -SubscriptionId "目标订阅ID"

# 获取目标存储账户
$targetStorage = Get-AzStorageAccount -ResourceGroupName "目标资源组名" -Name "目标存储账户名"

# 批量复制文件到目标订阅(大文件推荐用AzCopy)
Get-AzStorageFile -Context $sourceStorage.Context -ShareName "目标文件共享名" | ForEach-Object {
    if ($_.GetType().Name -eq "CloudFile") {
        $sourceUri = $_.Uri.AbsoluteUri + $sasToken
        $targetFile = New-AzStorageFile -Context $targetStorage.Context -ShareName "目标文件共享名" -Path $_.Name -Force
        Start-AzStorageFileCopy -AbsoluteUri $sourceUri -DestFile $targetFile
    }
}

# AzCopy高效复制命令(可选)
# azcopy copy "https://源存储账户.file.core.windows.net/文件共享名?$sasToken" "https://目标存储账户.file.core.windows.net/文件共享名" --recursive=true

关键注意事项

  • 确保服务主体拥有Recovery Services保管库的Backup Operator权限,以及源/目标存储账户的Storage Contributor权限
  • 若备份的是其他 workload(如SQL Server文件),需调整-WorkloadType和-ContainerType参数
  • 临时存储账户需与保管库同区域,避免跨区域传输延迟

内容的提问来源于stack exchange,提问作者Marcel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 02:46:23