Automatically create Veeam backups using vCenter Moref IDs

0 Flares Twitter 0 Facebook 0 LinkedIn 0 Email -- 0 Flares ×
For a project I’m working on these weeks, I’ve been asked to demonstrate how an external system (a Cloud Management Platform, an Automation tool, else) can automatically create backups for some specific virtual machines without interacting with the Veeam console. This blog post will show you how, using vSphere Moref IDs.

The ingredients

vCenter and Veeam use the same common field to uniquely identify a virtual machine, and this is the MorefID. This value is assigned by vCenter to every object that is created in its hierarchy, and it’s unique and unchanged for as long as the object itself is registered in vCenter. Veeam uses the same ID to identify which virtual machine needs to be backed up. So, definitely, this ID is our starting point.
Second, both platforms can use Powershell to automate many operations; in our case, we want to retrieve the MorefID of a given VM and pass this value as a parameter to Veeam Backup & Replication. If we want to use a dedicated windows machine to run our scripts (since Powershell runs on Windows), this machine, first of all, needs to have both Powershell modules installed.
Powershell 5.0 has an option to install additional modules using the Gallery, a public repository accessible over the Internet where we can retrieve and install supported modules directly from the shell itself, as long as the machine has Internet connectivity. My machine is running Windows 2012 R2, so it has Powershell 4.0 by default, not 5.0; however, we can easily upgrade it to the latest version, and there are many blog posts that show you how to do it. Once Powershell is upgraded to 5.0, we can just run:
Install-Module -Name VMware.PowerCLI

The command will require additional steps like to install dependencies and so on, but the final result is that the PowerCLI module is installed in our machine and ready to be used.

Veeam Backup & Replication Powershell is available as a snap-in, not as a module, so we need to install it locally; this can be done by installing the Standalone Console, as the PowerShell snap-in is part of its installation. We will then load it inside the script when we will run it, using as always this line:
asnp VeeamPssnapin

We are ready to build our proof of concept.

The script

Premise: I’m not a developer, and I only needed this small test to demonstrate that all this automation was doable, so forgive my low-level PowerShell skills; my code works, but probably many can make it better.
First of all, we need to retrieve the Virtual Machine MorefID. This can be done using this command:
$vmname = Read-Host "What is the name of the VM?"
$tempvm = Get-VM -Name $vmname
$morefid = $tempvm.ExtensionData.MoRef.Value

where we ask the user to tell us which VM he wants to protect. In my example, I’m going to protect one of my domain controllers, and if I input “dc1” in the dialog, the result is this:

So, now I have the MorefID stored in a variable. It’s now time to pass this information to Veeam; this can be done in a few lines of codes, and the final script would be this one:
# Connect to vCenter
$vcenter = "vcsa.cloudconnect.local"
Connect-VIServer $vcenter
# Ask for the VM to protect
$vmname = Read-Host "What is the name of the VM?"
Write-Host "We are creating a new backup for virtual machine $vmname"
# Find the MorefID of the VM
$tempvm = Get-VM -Name $vmname
$morefid = $tempvm.ExtensionData.MoRef.Value
# Load Veeam snap-in
asnp VeeamPssnapin
# In Veeam, connect to vCenter and identify the same VM
$vc = Get-VBRServer -name $vcenter
$VM = Find-VBRViEntity -Server $vc | where {$_.Reference -eq $morefid}
# Create a new backup for the VM
Add-VBRViBackupJob -Name $morefid -Entity $VM

and this is the script in action:

I’ve cut the output, as there are many more information in regards to the new backup job that has just been created; all the options like scheduling, guest processing and so on can be configured using more parameters of Add-VBRViBackupJob or other Veeam Powershell commands. My goal here was to show that a new backup job has been created in Veeam (and you can notice that in the description it says “Created by Powershell”):
Also, the MorefID is used as the name of the job. Then, if we look at the list of Virtual Machines, we can see that dc1 is the only virtual machine that is going to be protected by the new job:
People can further customize the script in order to have automatic configuration of schedule, datastore selection, and many other options. But the main goal of automatically creating a backup from just the MorefID has been accomplished.

One thought on “Automatically create Veeam backups using vCenter Moref IDs

Comments are closed.