Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #
  2. # ROBOCOPY Script
  3. #
  4. # This script calls robocopy over a few folders, and then emails you when they're done
  5.  
  6. Param(
  7. [Parameter(Mandatory=$true)]
  8. [string]$basesrc, #"G:\",
  9. [Parameter(Mandatory=$true)]
  10. [string]$basedst, #
  11. [Parameter(Mandatory=$false)]
  12. [string[]]$dirs= $(),
  13. [string]$backup_log_dir = "backuplogs"
  14. )
  15.  
  16. Set-Location -Path $PSScriptRoot
  17. Write-Host "###### Starting backup script ######"
  18.  
  19. $robocopy_block = {
  20. param($src,$dst,$log)
  21. New-Item -Force $log | Out-Null
  22. # Execute a command
  23. robocopy $src $dst /MIR /ZB /MT /XJ /XD Temp /XA:SH /R:3 /LOG:$log /NP
  24. # /MIR # mirroring the folders, removing non-existant files. We don't want lots of old files cluttering up the backup and the File Shares should keep a history on their own
  25. # /ZB # copies files such that if they are interrupted part-way, they can be restarted (should be default IMHO)
  26. # /MT # copies using multiple cores, good for many small files
  27. # /XJ # Ignoring junctions, they can cause infinite loops when recursing through folders
  28. # /XD Temp # Temp directories shouldn't have important data files
  29. # /XA:SH # Exclude hidden and system files when copying
  30. # /R:3 # Retrying a file only 3 times, we don't want bad permissions or other dumb stuff to halt the entire backup
  31. # /LOG:$log # Logging to a file internally, the best way to log with the /MT flag
  32. # /NP # Removing percentages from the log, they don't format well
  33. "Backup directory is complete. [$src -> $dst] ($log)"
  34. }
  35.  
  36. if ((test-path variable:\dirs) -and ($dirs.Length -ne 0)) {
  37. foreach ($dir in $dirs)
  38. {
  39. $src = Join-Path $basesrc $dir
  40. $dst = Join-Path $basedst $dir
  41. $log = Join-Path $backup_log_dir "backup_$($dir)_$(get-date -f yyyy-MM-dd_hh-mm-ss).log"
  42.  
  43. # Show the loop variable here is correct
  44. Write-Host "Backing up $dir..."
  45.  
  46. # pass the loop variable across the job-context barrier
  47. Start-Job $robocopy_block -ArgumentList $src,$dst,$log -Name "backup-$dir" | Out-Null
  48. }
  49. }
  50. else {
  51. Write-Host "Backing up $basesrc -> $basedst..."
  52. $log = Join-Path $backup_log_dir "backup_$(get-date -f yyyy-MM-dd_hh-mm-ss).log"
  53. Invoke-Command $robocopy_block -ArgumentList $basesrc,$basedst,$log | Out-Null
  54. }
  55.  
  56. # Wait for all to complete
  57. While (Get-Job -State "Running") { Start-Sleep 2 }
  58. # Display output from all jobs
  59. Get-Job | Receive-Job
  60. # Cleanup
  61. Remove-Job *
  62.  
  63. Send-MailMessage -to "admin <admin@foo.com>" `
  64. -from "backup <backup@foo.com>" `
  65. -subject "Backup Finished" `
  66. -smtpServer smtp.foo.com
  67. Write-Host "###### Backup is complete ######"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement