Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. ################################################################################
  2. ## SMB Backup Space Credentials
  3. ################################################################################
  4. $DriveName = "BackupSpace"
  5. $UserName = "u123456"
  6. $SecretPass = ConvertTo-SecureString "My Super Secret Password" -AsPlainText -Force
  7. # Creating Credential Object to use with PSDrive
  8. $Creds = New-Object System.Management.Automation.PSCredential($UserName, $SecretPass)
  9. # Using PSDrive to create the drive
  10. New-PSDrive -Name $DriveName -Credential $Creds -Root "\\backup.space\backup\Veeam" -PSProvider FileSystem
  11. $RemoteRepo = "${DriveName}:\"
  12. # Enable Purging the backup space (Optional)
  13. $PurgeEnabled = $True
  14. # Define days of retention for Purge (I keep 2 days of backups)
  15. $PurgeRetention = "-2"
  16.  
  17. ################################################################################
  18. ## Veeam Settings
  19. ################################################################################
  20. # Names of VMs to backup separated by comma (Mandatory). For instance, $VMNames = “VM1”,”VM2”
  21. $VMNames = "VM1","VM2"
  22. # Name of vCenter or standalone host VMs to backup reside on (Mandatory)
  23. $HostName = "my.vcenter.com"
  24. # Directory that VM backups should go to (Mandatory; for instance, C:\Backup)
  25. $Directory = "C:\TempBackup"
  26. # Desired compression level (Optional; Possible values: 0 - None, 4 - Dedupe-friendly, 5 - Optimal, 6 - High, 9 - Extreme)
  27. $CompressionLevel = "5"
  28. # Quiesce VM when taking snapshot (Optional; VMware Tools are required; Possible values: $True/$False)
  29. $EnableQuiescence = $True
  30. # Protect resulting backup with encryption key (Optional; $True/$False)
  31. $EnableEncryption = $False
  32. # Encryption Key (Optional; path to a secure string)
  33. $EncryptionKey = ""
  34. # Retention settings (Optional; By default, VeeamZIP files are not removed and kept in the specified location for an indefinite period of time.
  35. # Possible values: Never , Tonight, TomorrowNight, In3days, In1Week, In2Weeks, In1Month)
  36. $Retention = "Never"
  37.  
  38. ################################################################################
  39. ## Notification Settings
  40. ################################################################################
  41. # Enable notification (Optional)
  42. $EnableNotification = $True
  43. # Email SMTP server settings
  44. $SMTPServer = "smtp-relay.gmail.com"
  45. $SMTPServerPort = "587"
  46. $SMTPEnableSSL = $True
  47. #$SMTPUser = "mysmtpuser" # SMTP User authentication (optional)
  48. #$SMTPPass = "mysmtppasswd"
  49. # Email "envelope" settings
  50. $EmailFrom = "ops@foobar.com"
  51. $EmailTo = "mail@example.com"
  52. $EmailSubject = "Backup Job: Completed"
  53. ## Email Formatting
  54. $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
  55. $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
  56. $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
  57. $style = $style + "TD{border: 1px solid black; padding: 5px; }"
  58. $style = $style + "</style>"
  59.  
  60. ################################################################################
  61. ## Main
  62. ################################################################################
  63. Asnp VeeamPSSnapin
  64. $Server = Get-VBRServer -name $HostName
  65. $MesssagyBody = @()
  66.  
  67. foreach ($VMName in $VMNames)
  68. {
  69. $VM = Find-VBRViEntity -Name $VMName -Server $Server
  70.  
  71. If ($EnableEncryption)
  72. {
  73. $EncryptionKey = Add-VBREncryptionKey -Password (cat $EncryptionKey | ConvertTo-SecureString)
  74. $ZIPSession = Start-VBRZip -Entity $VM -Folder $Directory -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention -EncryptionKey $EncryptionKey
  75. Move-Item -Path $Directory\$VMName*.vbk -Destination $RemoteRepo
  76. }
  77.  
  78. Else
  79. {
  80. $ZIPSession = Start-VBRZip -Entity $VM -Folder $Directory -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention
  81. Move-Item -Path $Directory\$VMName*.vbk -Destination $RemoteRepo
  82. }
  83.  
  84. If ($EnableNotification)
  85. {
  86. $TaskSessions = $ZIPSession.GetTaskSessions().logger.getlog().updatedrecords
  87. $FailedSessions = $TaskSessions | where {$_.status -eq "EWarning" -or $_.Status -eq "EFailed"}
  88.  
  89. if ($FailedSessions -ne $Null)
  90. {
  91. $MesssagyBody = $MesssagyBody + ($ZIPSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e={$_.EndTime}},Result,@{n="Details";e={$FailedSessions.Title}})
  92. }
  93.  
  94. Else
  95. {
  96. $MesssagyBody = $MesssagyBody + ($ZIPSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e={$_.EndTime}},Result,@{n="Details";e={($TaskSessions | sort creationtime -Descending | select -first 1).Title}})
  97. }
  98.  
  99. }
  100. }
  101. If($EnableNotification)
  102. {
  103. $Message = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
  104. $Message.Subject = $EmailSubject
  105. $Message.IsBodyHTML = $True
  106. $Message.Body = $MesssagyBody | ConvertTo-Html -head $style | Out-String
  107.  
  108. $SMTP = New-Object Net.Mail.SmtpClient($SMTPServer)
  109. $SMTP = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPServerPort)
  110. $SMTP.EnableSsl = $SMTPEnableSSL
  111. # Enable if you mail server requires authentication
  112. #$SMTP.Credentials = New-Object System.Net.NetworkCredential($SMTPUser, $SMTPPass);
  113. $SMTP.Send($Message)
  114. }
  115.  
  116. If($PurgeEnabled)
  117. {
  118. # Delete all Files in the remote Repo that are older than specified above
  119. $CurrentDate = Get-Date
  120. $DatetoDelete = $CurrentDate.AddDays($PurgeRetention)
  121. Get-ChildItem ${RemoteRepo}\*.vbk | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
  122. }
  123.  
  124. # Remove the mapped drive
  125. Remove-PSDrive -Name $DriveName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement