Start PowerShell’ing your Backups

VeeamOn was such a great and educational show. For my small part of it, I wanted to share how you can automate the deployment/management of your infrastructure. Why would you want to automate? Seriously, did I really just ask that… Speed and  standardization/predictability are the primary drivers for scripting via PowerShell. Or awesomeness. Yup, we’ll stick with the fact that PowerShell is full of awesomeness.

I started my VeeamOn presentation with an example of just how awesome you can become with your PowerShell scripts. In the first part of the video you see how long it takes a person to manually configure a job. Keep in mind this is someone who pretends to know what they are doing, but still errors happen. The second example shows just how long it takes to implement the exact same job via script. So let’s take a moment to parse the Why’s of PowerShell

  • Speed. When I made this video, I had a practiced each click. It still took 2 minutes and 45 seconds to create a backup job. Conversely, the script took 30 seconds to complete. This is for one machine. Now think about if you’re setting this up for 100 machines… manually you’re looking at 4.5 and half hours. Via script, 50 minutes. Yes this is dirty math as there are many factors that go into the equation, but you can’t argue with the fact that scripting is more efficient, even factoring in the ~20 minutes I spent writing the script.
  • Standardization. I’ve worked with Veeam B&R for a number of years now and as I mentioned above, I practiced the workflow to try and take out any bias from the equation. Still I made an oops. We’re human after all. As you’ll see in the code below, by standardizing you can remove much of the human variable (bad pun intended) and produce a more consistent output.

 

Without further ado, here’s the code and desciptions about what’s going on.

lines 2-3: If the PsSnapIn for Veeam isn’t loaded, let’s go ahead and load it. You’re using ISE right, so you probably want it in your editor session as I talked about here
lines 6-27: When you stop and think about it, backup jobs are complex and have lots of options. This is an uber simple example and still you’ve got 16 lines of configurations. By moving this off into a function you make your code both more readable and repeatable. Thankfully the developers have used very intuitive naming conventions for the job options!
line 32: connect to the Backup and Recovery server. Full disclosure, I set the $cred variable in a previous script and got lazy. Sorry!
lines 6-27: Finally, we are getting to the action. It’s exciting! But it’s also not. We set most of the options already. At this point all we need to do is execute the various VBR cmdlets.

  • Add-VBRViBackupJob
    Short and simple, create the backup job… Or is it????  We create a job, but we also leverage:
    Find-VBRViEntity: Find the VMware entity to backup that we specified in our variables above.
    Get-VBRBackupRepository: Fairly self-explanatory, find the backup repository that the job will use.
  • Set-VBRJobOptions
    There is a flow to objects created via the VBR PowerShell cmdlets. Create the object. Set options on the object. This is the later. These are the options configured in our SetVariables function in lines 19-26.
  • Set-VBRJobAdvancedBackupOptions
    Do I need to explain what this does?
  • Set-VBRJobSchedule
    Again, pretty self-explanatory right?
  • Add-VBRViBackupCopyJob
    Remember when I screwed up in the video above? Why do you have to bring up those painful memories??? Anyway, moving on this one creates a new copy job on the previously set variables.

Each of the above cmdlets have a significant number of options to fit your environment. I’d encourage you to peruse the Veeam PowerShell Reference guide for additional options.

Here’s the code. We’ll cover more advanced workflows in our next post in this series. Stay tuned!

#VeeamOn Simple Backup Job Demo
if( ! $(get-pssnapin -Name VeeamPSSnapIn -ea SilentlyContinue)) {
Add-PSSnapin VeeamPSSnapIn
}

function SetVariables{
$Global:PWord= ConvertTo-SecureString -String "VMware1!" -AsPlainText -Force
$Global:cred=New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList "lab\Administrator", $PWord
$Global:VBRserver="VBR.lab.local"

$Global:VBRBackupName="Gold_Tier_Backup"
$Global:VBRRepositoryName="VBR_Local_Repository"
$Global:VBRBackupEntity="GoldTier"

$Global:VBRBackupCopyName="Gold_Copy"
$Global:VBRReplRepositoryName="VBR_Replication_Repository"

#set JobOptions
$Global:VBRJobOptions=Get-VBRJobOptions -Job $VBRBackupName
$VBRJobOptions.JobOptions.RunManually = $false
$VBRJobOptions.BackupStorageOptions.RetainCycles = 3
$VBRJobOptions.BackupStorageOptions.RetainDays = 7
$VBRJobOptions.BackupStorageOptions.EnableDeletedVmDataRetention = $true
$VBRJobOptions.BackupStorageOptions.CompressionLevel = 6
$VBRJobOptions.NotificationOptions.SendEmailNotification2AdditionalAddresses = $true
$VBRJobOptions.NotificationOptions.EmailNotificationAdditionalAddresses = "test@test.com"
}

#Call SetVariables function
SetVariables

if ( ! $(get-vbrserver -Name $VBRserver) ) {Connect-VBRServer -Credential $cred -Server $VBRserver}
sleep 5

$null=Add-VBRViBackupJob -Name $VBRBackupName -Entity $(find-vbrvientity -Tags -Name $VBRBackupEntity) -BackupRepository $(Get-VBRBackupRepository -Name $VBRRepositoryName)

$null=Set-VBRJobOptions -Job $VBRBackupName -Options $VBRJobOptions

$null=Set-VBRJobAdvancedBackupOptions -Job $VBRBackupName -EnableFullBackup $true -FullBackupDays Friday -FullBackupScheduleKind Daily

$null=Set-VBRJobSchedule -Job $VBRBackupName -DailyKind WeekDays -At 01:00

$null=Add-VBRViBackupCopyJob -DirectOperation -Name $VBRBackupCopyName -Repository $(Get-VBRBackupRepository -Name $VBRReplRepositoryName) -Entity $(find-vbrvientity -Tags -Name $VBRBackupEntity)

 

Leave a comment