Skip to content

Instantly share code, notes, and snippets.

@JoelAzaria
Forked from jbfriedrich/VeeamBackup.ps1
Created September 4, 2018 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelAzaria/0585cfcb4206874534f7b689f8b6a6a4 to your computer and use it in GitHub Desktop.
Save JoelAzaria/0585cfcb4206874534f7b689f8b6a6a4 to your computer and use it in GitHub Desktop.
Create Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)
################################################################################
## SMB Backup Space Credentials
################################################################################
$DriveName = "BackupSpace"
$UserName = "u123456"
$SecretPass = ConvertTo-SecureString "My Super Secret Password" -AsPlainText -Force
# Creating Credential Object to use with PSDrive
$Creds = New-Object System.Management.Automation.PSCredential($UserName, $SecretPass)
# Using PSDrive to create the drive
New-PSDrive -Name $DriveName -Credential $Creds -Root "\\backup.space\backup\Veeam" -PSProvider FileSystem
$RemoteRepo = "${DriveName}:\"
# Enable Purging the backup space (Optional)
$PurgeEnabled = $True
# Define days of retention for Purge (I keep 2 days of backups)
$PurgeRetention = "-2"
################################################################################
## Veeam Settings
################################################################################
# Names of VMs to backup separated by comma (Mandatory). For instance, $VMNames = “VM1”,”VM2”
$VMNames = "VM1","VM2"
# Name of vCenter or standalone host VMs to backup reside on (Mandatory)
$HostName = "my.vcenter.com"
# Directory that VM backups should go to (Mandatory; for instance, C:\Backup)
$Directory = "C:\TempBackup"
# Desired compression level (Optional; Possible values: 0 - None, 4 - Dedupe-friendly, 5 - Optimal, 6 - High, 9 - Extreme)
$CompressionLevel = "5"
# Quiesce VM when taking snapshot (Optional; VMware Tools are required; Possible values: $True/$False)
$EnableQuiescence = $True
# Protect resulting backup with encryption key (Optional; $True/$False)
$EnableEncryption = $False
# Encryption Key (Optional; path to a secure string)
$EncryptionKey = ""
# Retention settings (Optional; By default, VeeamZIP files are not removed and kept in the specified location for an indefinite period of time.
# Possible values: Never , Tonight, TomorrowNight, In3days, In1Week, In2Weeks, In1Month)
$Retention = "Never"
################################################################################
## Notification Settings
################################################################################
# Enable notification (Optional)
$EnableNotification = $True
# Email SMTP server settings
$SMTPServer = "smtp-relay.gmail.com"
$SMTPServerPort = "587"
$SMTPEnableSSL = $True
#$SMTPUser = "mysmtpuser" # SMTP User authentication (optional)
#$SMTPPass = "mysmtppasswd"
# Email "envelope" settings
$EmailFrom = "ops@foobar.com"
$EmailTo = "mail@example.com"
$EmailSubject = "Backup Job: Completed"
## Email Formatting
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
################################################################################
## Main
################################################################################
Asnp VeeamPSSnapin
$Server = Get-VBRServer -name $HostName
$MesssagyBody = @()
foreach ($VMName in $VMNames)
{
$VM = Find-VBRViEntity -Name $VMName -Server $Server
If ($EnableEncryption)
{
$EncryptionKey = Add-VBREncryptionKey -Password (cat $EncryptionKey | ConvertTo-SecureString)
$ZIPSession = Start-VBRZip -Entity $VM -Folder $Directory -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention -EncryptionKey $EncryptionKey
Move-Item -Path $Directory\$VMName*.vbk -Destination $RemoteRepo
}
Else
{
$ZIPSession = Start-VBRZip -Entity $VM -Folder $Directory -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention
Move-Item -Path $Directory\$VMName*.vbk -Destination $RemoteRepo
}
If ($EnableNotification)
{
$TaskSessions = $ZIPSession.GetTaskSessions().logger.getlog().updatedrecords
$FailedSessions = $TaskSessions | where {$_.status -eq "EWarning" -or $_.Status -eq "EFailed"}
if ($FailedSessions -ne $Null)
{
$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}})
}
Else
{
$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}})
}
}
}
If($EnableNotification)
{
$Message = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
$Message.Subject = $EmailSubject
$Message.IsBodyHTML = $True
$Message.Body = $MesssagyBody | ConvertTo-Html -head $style | Out-String
$SMTP = New-Object Net.Mail.SmtpClient($SMTPServer)
$SMTP = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPServerPort)
$SMTP.EnableSsl = $SMTPEnableSSL
# Enable if you mail server requires authentication
#$SMTP.Credentials = New-Object System.Net.NetworkCredential($SMTPUser, $SMTPPass);
$SMTP.Send($Message)
}
If($PurgeEnabled)
{
# Delete all Files in the remote Repo that are older than specified above
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($PurgeRetention)
Get-ChildItem ${RemoteRepo}\*.vbk | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
}
# Remove the mapped drive
Remove-PSDrive -Name $DriveName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment