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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<#
.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
    <a class="vglnk" href="/https://thesysadminchannel.com/automate-backups-start-vbrzip-powershell-veeam-backup-free-edition" rel="nofollow"><span>https</span><span>://</span><span>thesysadminchannel</span><span>.</span><span>com</span><span>/</span><span>automate</span><span>-</span><span>backups</span><span>-</span><span>start</span><span>-</span><span>vbrzip</span><span>-</span><span>powershell</span><span>-</span><span>veeam</span><span>-</span><span>backup</span><span>-</span><span>free</span><span>-</span><span>edition</span></a> -
 
 
.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
    }
}

This:

$VM = Find-VBRViEntity -Name $VMName -Server $Server

Is for VMware

and this:

$VM = Find-VBRHvEntity -Name $VMName -Server $Server

is for Hyper-V

 

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