Last active
April 5, 2020 09:51
-
-
Save jbfriedrich/c7ccd123f23d8d0d5b309dd6b3983a81 to your computer and use it in GitHub Desktop.
Create Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
## 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 = "[email protected]" | |
$EmailTo = "[email protected]" | |
$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 |
Hey 👋! If nothing has changed in the last years since I wrote the script, it should be possible to do that. As far as I remember, there is no connection to a Veeam DB or something similar. If you want to be 100% sure, I would recommend asking in the Veeam forums. I have stopped using Veeam for quite a while, so my knowledge might not be up to date.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello. right now i wrote a script similar yours. i dont want use the autodelete and remove the old backups on a way like you.
it is dangerless possible to remove the older backups with ps remove-item? there is no connectivity to the veeam backup db?