Veeam v8 – Backing up a single VM with PowerShell

One of the nice additions to VBR v8 is the ability to back up a single VM within a job using Quick Backup. I especially like the fact that they extended this ability to the vSphere Web Client, making it as simple as a right click. You can read more about this functionality on Veeam’s blog:
8 gems in Veeam Availability Suite v8. Part 3: Quick Backup

After reading this post and using the feature a few times, I figured there would be no need to update the Single VM Backup script. It turns out there is still use for a script such as this which was pointed out to me in the comments (Thanks Phil!). Essentially the Quick Backup feature only works on VMs that have previously been backed up, it does not work for new VMs.

Follow this scenario – you create a new VM, add it to a backup job and want to create a nice, clean full backup of this VM. Quick Backup will not work in this case as it does not already have an existing backup of this VM. PowerShell to the rescue!

Just a few tweaks were needed in order to ‘fix’ the script for v8. I also added a version check to both the old and new scripts so that you do not inadvertently run the wrong script and mess up your jobs.

Warning – If you run the original script against v8 it will remove the ‘excluded’ VMs from the job permanently (doh!). There is nothing wrong with the script itself per se, Veeam just happened to make a tweak in v8 that causes the script to fail when re-adding the VMs to the job. This highlights the need to test all scripts running against production systems, including when changes are made like a version upgrade.

# Load Veeam snapin
Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
Clear-Host
Write-Host "Veeam Single VM Backup Script`n"
Write-Host "Notes:"
Write-Host "VM must be explicitly defined in the job"
Write-Host "VM can not be inherited by a folder, datastore or other object"
Write-Host "else it will get excluded as well and job will be empty."
Write-Host "Always confirm job completion via Veeam BR Console!"
# Check Veeam Version (Must be v8)
If ((Get-PSSnapin VeeamPSSnapin).Version.Major -ne 8) {
Write-Host "You must be running VBR v8 to run this script...Exiting"
Exit
}
# User Input
$jobName = Read-Host "Enter Job Name"
$vmName = Read-Host "Enter VM Name"
# Find the job that has our VM
$job = Get-VBRJob | ?{$_.Name -eq $jobName}
# Get all objects in job apart from our target VM
$execObjs = $job.GetObjectsInJob() | ?{$_.Name -ne $vmName}
# Exclude the objects from the job
Remove-VBRJobObject -Objects $execObjs
# Start the job only backing up the target VM
$runJob = Start-VBRJob -Job $job
# Find the exclude job objects
$incObjs = $job.GetObjectsInJob() | ?{$_.Type -eq "Exclude"}
# Delete the exclude objects and re-add to job
foreach ($obj in $incObjs) {
$Excitem = Find-VBRViEntity -Name $obj.Name
Add-VBRViJobObject -job $Job -Entities $Excitem | Out-Null
$obj.Delete()
}
Write-Host "`nBackup Complete"

3 thoughts on “Veeam v8 – Backing up a single VM with PowerShell

  1. Kenneth

    I am getting this error in Veeam 8.x

    Add-VBRViJobObject : Cannot validate argument on parameter ‘Entities’. The argument is null. Supply a non-null argument and try the command again.
    At C:\Install\Backup_1_VM_in_a_job.ps1:41 char:44
    + Add-VBRViJobObject -job $Job -Entities $Excitem | Out-Null
    + ~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Add-VBRViJobObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Backup.PowerShell.Command.AddVBRViJobObject

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *