异步调用 Powershell 脚本?(Robocopy)

异步调用 Powershell 脚本?(Robocopy)

我们的备份脚本每天都会运行。月底,每个文件都应移至“暂存”位置,以便复制到“磁带”...

因此,我编写了一个 PS 脚本,移动所有文件。到目前为止,这一切都是“同步”进行的,运行良好……但两天的备份丢失了,因为移动文件大约需要 2 天时间,阻止了每天调用同一脚本进行每日备份……

现在我想通过异步方式移动文件(通过 robocopy)来改进这一点,以便接下来几天的备份可以在需要时开始......

我将现有脚本修改为以下内容 - 但是,在启动“所有作业”后它会引发异常(如果是月末一天!):

在此处输入图片描述

脚本如下:

#definitions
$backupJobName = "MyCompany Komplett"
$backupCopyJobName = "CopyToTape"
$backupFileLocation = "V:\MyCompany Komplett"
$backupFileToTapeLocation = "T:\"

##############################################################
# Modify content bellow only if aware of what you are doing! #
##############################################################
. C:\shared.ps1

Start-Transcript -Path "C:\daily_backup.txt" -Append

# imports
Add-PSSnapin VeeamPSSnapIn

#script
$dateTime = Get-Date;
$firstDayOfMonth = getFirstDayOfMonth
$lastDayOfMonth = getLastDayOfMonth

if (isSameDay $dateTime $lastDayOfMonth){
  $dateTime.ToString() + ": Last day of Month:"
  $dateTime.ToString() + ": 1.) Run final incremental Backup of this month."
  #$job = Get-VBRJob | where {$_.Name –eq $backupJobName} | Start-VBRJob

  #output for logs
  #$job

  #Copy Files to staging folder.
  $dateTime = Get-Date;
  $dateTime.ToString() + ": 2.) Copy Files to Tape-Staging-Location"
  $elements = Get-ChildItem -Path $backupFileLocation -filter "*.vrb"| ? { $_.lastwritetime.month -eq $dateTime.month -and $_.lastwritetime.year -eq $dateTime.year}
  $elements_vbk = Get-ChildItem -Path $backupFileLocation -filter "*.vbk" | ? { $_.lastwritetime.day -eq $dateTime.day -and $_.lastwritetime.month -eq $dateTime.month -and $_.lastwritetime.year -eq $dateTime.year}
  $elements_vbm = Get-ChildItem -Path $backupFileLocation -filter "*.vbm" 

  "List of relevant vrb files"
  $elements | Select Name | ForEach-Object { write-host $_.Name } #output for logs

  "List of relevant vbk files"
  $elements_vbk | Select Name | ForEach-Object { write-host $_.Name } #output for logs

  # copy elements to Staging location (Async!)
  # copy: vrb from current month
  # copy: vbk from today.
  "Copy Job started for:"
  $elements_vbm | Select Name | ForEach-Object { Start-Job -ScriptBlock { robocopy $backupFileLocation $backupFileToTapeLocation $_.name } }
  $elements_vbk | Select Name | ForEach-Object { Start-Job -ScriptBlock { robocopy $backupFileLocation $backupFileToTapeLocation $_.name } }
  $elements | Select Name | ForEach-Object { Start-Job -ScriptBlock { robocopy $backupFileLocation $backupFileToTapeLocation $_.name } } 

  ###
  ###
  ###
  ###  Here I get the exception. Running robocopy without "Start-Job" works, but takes tooo long...
  ###
  ###
  ###
} elseif (isSameDay $dateTime $firstDayOfMonth){

  #do full Backup.
  $dateTime.ToString() + ": First day of month: Running Full Backup this time."
  $job = Get-VBRJob | where {$_.Name –eq $backupJobName} | Start-VBRJob -FullBackup
  #Output for logs
  $job
}else{
  #do Reverse Incremental Backup.
  $dateTime.ToString() + ": Regular Day of Month: Running Reverse Incremental Backup."

  $job = Get-VBRJob | where {$_.Name –eq $backupJobName} | Start-VBRJob

  #Output for logs
  $job
}

Stop-Transcript

相关内容