Create new backup jobs in Veeam via powershell

0 Flares Twitter 0 Facebook 0 LinkedIn 0 Email -- 0 Flares ×

When you have to deal with a large environment and several jobs in Veeam, automation via powershell is the only possible solution.

The interface is nice…

.. yet is an interface. Which means that for any new job you have to create, you need to go through the job creation wizard, fill in any required field, select the different options, and wait for the job to be reigstered before creating the next one. At least the advanced options for the storage can be stored as default in Veeam Backup & Replication, but this is only a part of the different settings available in a job. And if one of the jobs requires a different set of options, you need again to open the advanced options and change them.

This scenario is not a problem for users with few jobs, or jobs that do not change frequently because the protected environment doesn’t change often. But if you have like in the case I was following 50 different jobs, each mapped to a vSphere datastore, things starts to become a little bit more complicated

Create jobs using Powershell

The two major limits in creating jobs using the interface is the total bore of repeating the same process multiple times, but even more the risk to commit some errors in the wizard, exactly because the bore makes it all appear even more, well, boring…

To solve this issue, and guarantee a consistent configuration across all the 50 jobs I had to create, I developed two powershell scripts. They are both to be executed in the powershell console of the Veeam server.

The first one retrieves all the datastores as Veeam sees them when browsing the connected vCenter:

asnp “VeeamPSSnapIn” -ErrorAction SilentlyContinue
Find-VBRViEntity -DatastoresAndVMs | where {$_.Type -eq “Datastore”} | select {$_.Name} -Unique | Sort-Object Name

The list can then be used to create the different jobs. Because there are several jobs moving a lot of data, you want to optimise the day when the full backup is executed during the week, instead of having all of them running in the same day. This would cause issues in the environment and the schedule will last too long. Then, you also want to send different backup jobs into different repositories to balance the storage consumption. Because of these requirements, the script asks 4 parameters as input: the name of the datastore to be used as a backup entity, the repository where we want to store the backup chain, the day the active full will be used, the time of the day to schedule the backup. The datastore name will also be the job name.

This script is also a good opportunity to learn how the different job options can be configured via Powershell in Veeam.

asnp “VeeamPSSnapIn” -ErrorAction SilentlyContinue

# Ask for parameters and build the job

$datastorename = Read-Host ‘Which datastore you want to use?’
$repositoryname = Read-Host ‘Which repository you want to use?’
$fullday = Read-Host ‘Which day you want to run the active full? Options are Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday’
$time = Read-Host “At what time of the day you want to run the job? Format is hh:mm”

$VBRName = “This server”
$VBR = Get-VBRServer -Name $VBRName
$VC = Get-VBRServer -Type “VC”

$datastore = Find-VBRViDatastore -Server $VC -Name $datastorename
$repository = Get-VBRBackupRepository -Name $repositoryname

# Create the job
Add-VBRViBackupJob -Name $datastorename -Entity $datastore -BackupRepository $repository

# Job is created with default parameters
# After creation, edit job options to have the final result
#
# Set retention points 

$job = Get-VBRJob -Name $datastorename
$options = $job.GetOptions()
$options.BackupStorageOptions.RetainCycles = 7
$job.SetOptions($options)

# for compression levels and block size = http://helpcenter.veeam.com/backup/80/powershell/set-vbrjobadvancedstorageoptions.html

Get-VBRJob -Name $datastorename | Set-VBRJobAdvancedStorageOptions -CompressionLevel 4 -EnableDeduplication $True -StorageBlockSize 6

# Disable default synthetic and set active full on the desired day

Get-VBRJob -Name $datastorename | Set-VBRJobAdvancedBackupOptions -Algorithm Incremental -TransformFullToSyntethic $False -EnableFullBackup $True -FullBackupDays $fullday

# Set the schedule, daily execution with date as input, and enable it

Get-VBRJob -Name $datastorename | Set-VBRJobSchedule -Daily -At $time
Get-VBRJob -Name $datastorename | Enable-VBRJobSchedule