0

Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)

As a systems administrator I am constantly having to test ways to accomplish new tasks. Before I go into unknown territory I always like to make sure I have my VMs backed up in the event that my tests crap the bed. Sometimes a simple VM snapshot will do, but other times I might need to revert back a couple of days for when things get too out of hand. In my lab where I do all my tests and Sysadmin videos on YouTube I always have a backup prior to making the videos, simply because I have to do the same task multiple times in order to get a refresher as well as the actual recording. My backup software of choice is Veeam… hands down. Out of all the backup software on the market right now, Veeam is widely regarded as the #1 choice by fellow peers. In this article I am going to share the script that I created to automate backups with Start-VBRZip in Powershell using Veeam Free Backup Edition. In case you didn’t know, Veeam Free Backup Edition doesn’t offer support for creating jobs and running them automatically, nor do they support incremental jobs. This means that you will need to rely on Powershell and Scheduled Tasks to automate the backup process for you. Also, every time you create a backup, it will always be a full backup.

Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)


<#
.Synopsis
    Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)
    For updated help and examples refer to -Online version.
 

.DESCRIPTION
    Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)
    For updated help and examples refer to -Online version.


.NOTES   
    Veeam_BackupAllVMs
    Author: the Sysadmin Channel
    Version: 1.0
    DateCreated: 2018-Apr-25
    DateUpdated: 2018-Apr-25

.LINK
    https://thesysadminchannel.com/automate-backups-start-vbrzip-powershell-veeam-backup-free-edition -


.EXAMPLE
    For updated help and examples refer to -Online version.

#>

Add-PSSnapin VeeamPSSnapIn

$AllVMs = Find-VBRViEntity -Name * | Where-Object {($_.Type -eq "VM") -and ($_.PowerState -eq "PoweredOn")} | Sort-Object Name
$BackupFolder = "\\PAC-VEEAM02\Veeam_Backup"
$StartTime = Get-Date
$Date = (Get-Date).ToString("yyyy")
$LogFile = "$BackupFolder\_Logs\log.csv"
$Retention = "In2Weeks" <# Valid Options:  Never Tonight TomorrowNight In3days In1Week In2Weeks In1Month In3Months In6Months In1Year #>


Foreach ($VM in $AllVMs) {

    $VMName = $VM.Name
    $VMName = $VMName.ToUpper()

    if (!$(Test-Path $BackupFolder$VMName)) {
        mkdir $BackupFolder$VMName
    }

    Start-VBRZip -Folder "$BackupFolder$VMName" -Entity $VM -Compression 5 -AutoDelete $Retention

    $Results =  Get-VBRBackupSession | Where-Object {$_.Name -match $VMName} | Sort-Object EndTime -Descending | select -First 1
    $FileName = Get-ChildItem -Path $BackupFolder$VMName | Sort-Object LastWriteTime -Descending | select -ExpandProperty Name -First 1

    #Outputting results into logfile.
    " " | Select @{Name = "VMName"; Expression = {$VMName}}, @{Name = "Status"; Expression = {$Results.Result}}, @{Name = "StartTime"; Expression= {$Results.CreationTime.ToString('MM/dd/yyyy  hh:mmtt')}}, @{Name = "EndTime"; Expression= {$Results.EndTime.ToString('MM/dd/yyyy  hh:mmtt')}}, @{Name = "TotalTime"; Expression= {$VMTime = ($Results.EndTime - $Results.CreationTime); "Hours: " + $VMTime.ToString('hh') + "   " + "Minutes: " + $VMTime.ToString('mm')}}, @{Name = "Filename"; Expression = {$FileName}}, @{Name = "SizeGB"; Expression= {[math]::Round((Get-ChildItem -Path $BackupFolder$VMName -Filter $FileName | select -ExpandProperty Length) / 1GB,2)}}, @{Name = "AutoDelete"; Expression = {$Retention}} | Export-Csv $LogFile -NoTypeInformation -Append


    #If the result failed, delete the backup file since it is not valid.
    if ($Results.Result -eq "Failed") {
        Remove-Item $BackupFolder$VMName$FileName -Force
    }
}

Setup Scheduled Task to Run VeeamZIP Powershell Script

So we got the first part out of the way, now let’s look at creating a scheduled task to trigger the job and automatically backup your VMs.

  • Open Task Scheduler and create a new task. Be sure to select ‘Run whether user is logged on or not.

Create Scheduled Task Veeam
 

  • Set the schedule to daily (or weekly) and set the time that works best for you. Be sure to leave the option to enabled.

Create Trigger Scheduled Task Veeam
 

  • In the actions tab, set the program to the location of Powershell. (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)
  • Under the arguments, set the location of this script. (E:\_Scripts\Veeam_BackupAllVMs.ps1)
Create Action Scheduled Task Veeam

You will probably need to set your execution policy to RemoteSigned

  • The conditions tab can be set to your preference.
  • The settings tab can be set to your preference.

 

At this point all you do have to now is wait until the schedules have triggered so the backup could run. At the end of each backup, I coded some logging so you can know exactly how long it took to run, how big the size of the VM is, and how long you set the retention on each VM. The output of the log is in CSV format and looks something like this.

Veeam backup log file

If you’re ever interested and decide you want to learn more about Veeam, be sure to check out Learning Veeam Backup and Replication for VMware vSphere

4.9/5 - (16 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

Leave a Reply

Your email address will not be published.