Matt That IT Guy

Racking And Stacking In A Cloud-Based World

PowerShellVeeamVMware

Automated Tagging of VMware VMs & the Veeam Jobs that Use Them

vSphere TagsI tend to rebuild my home lab a few times a year; sometimes it is to try out a new process (e.g. scripted installs, upgrade paths, etc.), but more often than not it is because I have broken something so bad that it is just easier to restart. As part of that, I usually try to restore some key VMs from backup. However, the bigger struggle there is keeping my backup jobs up to date.

I’ve been a fan of using vSphere tagging as a means to create backup jobs within Veeam for years now; that being said, I’m not always great at actually applying tags to my VMs (I know, shame on me). Recently, when I finally got around to performing some lab hygiene, I opted to get my vSphere tags in order. Long story short, I spent about 20 minutes trying to figure out what tags wasn’t an option when creating a new Veeam backup job, only to realize I am hitting the long-standing DPI issue.

After some “sympathy” from Joe Houghes, I opted to just create the jobs via PowerShell, which led me down the road of “well if I can do this, maybe I can do this other step in PowerShell”. Which brings me to this script.

WHATS IT DO & WHERE DO I RUN IT

In short, this script works in 2 parts. The first part is vSphere focused: it will ingest a spreadsheet consisting of VM names along with a tag (currently set with Daily, Weekly, and Monthly). It will then create those tags in vSphere and tag the VMs based off of the spreadsheet.

The second step creates the jobs in Veeam, which use vSphere tags as their criteria. Each of these jobs can be customized as far as day(s) of the week, time to run at, and retention points. Additionally, I’ve tried to toss most of that stuff into variables at the beginning of the script, just to make it easier. I’m sure I’ve missed a bunch of stuff that could added as variables, but I’m satisfied with this for a v1.

Lastly, since Veeam does not currently have a PowerShell module, you’ll need to run this on a machine that has a) PowerCLI installed (and configured – basically make sure it is run once) and b) it has the Veeam snap-in available (e.g. the VBR server).

Also of note, the “Get-VM” line towards the top of the script (which is commented out) can be used to generate a simple CSV file to run this. Just run that, open the file in Excel and make sure it has two columns with the headers of Name and Tag.

THE SCRIPT

I’ll also host this on my (rarely used) GitHub page:

#############################
## vCenter Tagging & Veeam Backup & Replication job builder
## Matt Crape / @MattThatITGuy / 42u.ca
############################

#CSV That contains two columns (Name and Tag)
$csvFile = "c:\temp\VMs.csv"

#vSphere info
$vCenterServerName = "vcenter.42u.local"
$vCenterUserName = "administrator@vsphere.local"
$vCenterPassword = "VMware123!"

#Tag names (used in spreadsheet & vCenter) and Veeam Job Name
$dailyTagName = "Daily"
$weeklyTagName = "Weekly"
$monthlyTagName = "Monthly"

#Retention points for jobs
$dailyRetention = 14
$weeklyRetention = 8
$monthlyRetention = 2

#Veeam Job Settings
$repositoryName = "V drive"
$dailyRunTime = "23:30"
$weeklyRunTime = "0:15"
$monthlyRunTime = "3:00"

#Connect to vCenter
Connect-VIServer -Server $vCenterServerName -User $vCenterUserName -Password $vCenterPassword

#Dump list of VMs - Do this to create spreadsheet for tags
#Get-VM | Select-Object Name | Export-csv $csvFile

#Import based on tag
$listOfDailyVMs = Import-Csv -LiteralPath $csvFile |  Where-Object -FilterScript {$_.Tag -eq $dailyTagName}
$listOfWeeklyVMs = Import-Csv -LiteralPath $csvFile | Where-Object -FilterScript {$_.Tag -eq $weeklyTagName}
$listOfMonthlyVMs = Import-Csv -LiteralPath $csvFile | Where-Object -FilterScript {$_.Tag -eq $monthlyTagName}

#Create Backup Tag Category & Tags
New-TagCategory -Name Backups -Description "Used to define backup jobs"
New-Tag -Category Backups -Name $dailyTagName
New-Tag -Category Backups -Name $weeklyTagName
New-Tag -Category Backups -Name "Monthly"


foreach ($vmDaily in $listOfDailyVMs){
    
    New-TagAssignment -Tag $dailyTagName -Entity $vmDaily.Name
}

foreach ($vmWeekly in $listOfWeeklyVMs){
    
    New-TagAssignment -Tag $weeklyTagName -Entity $vmWeekly.Name
}

foreach ($vmMonthly in $listOfMonthlyVMs){
    
    New-TagAssignment -Tag $monthlyTagName -Entity $vmMonthly.Name
}

#Begin Veeam Job Creation

Add-PSSnapin VeeamPSSnapin


#Create Daily Job

$JobName = $dailyTagName
$VMwareTag = $dailyTagName

$VeeamRepository = Get-VBRBackupRepository -Name $repositoryName
$vCenterServer = Get-VBRServer -Name $vCenterServerName

$VeeamTag = Find-VBRViEntity -Name $VMwareTag -Tags -Server $vCenterServerName

$VeeamBackupJob = Add-VBRViBackupJob -Name $JobName -Description 'Daily backup Job' -BackupRepository $VeeamRepository -Entity $VeeamTag
$retentionDaily = New-VBRJobOptions -ForBackupJob
$retentionDaily.BackupStorageOptions.RetainCycles = $dailyRetention
Set-VBRJobOptions -Job $JobName -Options $retentionDaily

Get-VBRJob -Name $JobName | Enable-VBRJobSchedule
Get-VBRJob -Name $JobName | Set-VBRJobSchedule -At $dailyRunTime -DailyKind Everyday

#Create Weekly Job

$JobName = $weeklyTagName
$VMwareTag = $weeklyTagName

$VeeamRepository = Get-VBRBackupRepository -Name $repositoryName
$vCenterServer = Get-VBRServer -Name $repositoryName

$VeeamTag = Find-VBRViEntity -Name $VMwareTag -Tags -Server $vCenterServerName

$VeeamBackupJob = Add-VBRViBackupJob -Name $JobName -Description 'Weekly backup Job' -BackupRepository $VeeamRepository -Entity $VeeamTag
$retentionWeekly = New-VBRJobOptions -ForBackupJob
$retentionWeekly.BackupStorageOptions.RetainCycles = $weeklyRetention
Set-VBRJobOptions -Job $JobName -Options $retentionWeekly

Enable-VBRJobSchedule -Job $JobName 
Set-VBRJobSchedule -Job $JobName  -At $weeklyRunTime -DailyKind SelectedDays -Days Saturday

#Create Monthly Job

$JobName = $monthlyTagName
$VMwareTag = $monthlyTagName

$VeeamRepository = Get-VBRBackupRepository -Name $repositoryName
$vCenterServer = Get-VBRServer -Name $repositoryName

$VeeamTag = Find-VBRViEntity -Name $VMwareTag -Tags -Server $vCenterServerName

$VeeamBackupJob = Add-VBRViBackupJob -Name $JobName -Description 'Monthly backup Job' -BackupRepository $VeeamRepository -Entity $VeeamTag
$retentionMonthly = New-VBRJobOptions -ForBackupJob
$retentionMonthly.BackupStorageOptions.RetainCycles = $monthlyRetention
Set-VBRJobOptions -Job $JobName -Options $retentionMonthly

Get-VBRJob -Name $JobName | Enable-VBRJobSchedule
Get-VBRJob -Name $JobName | Set-VBRJobSchedule -At $monthlyRunTime -Monthly -NumberInMonth Last -Days Sunday

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.