Host-based backup of VMware vSphere VMs.
Post Reply
dcolpitts
Veeam ProPartner
Posts: 119
Liked: 24 times
Joined: Apr 01, 2011 10:36 am
Full Name: Dean Colpitts
Location: Atlantic coast of Canada
Contact:

Job Scripts - run only after successful backup?

Post by dcolpitts »

I looking at the Advanced Settings of the Storage settings for a backup job. Would it be possible to request an option to set that the "after job" script only runs upon successful completion of the job? Specifically I'm thinking of the ability to write a script to eject an RDX cartridge if the job completes successfully. Obviously if we have automatic retries enabled, then we wouldn't want to eject the cartridge...

Unless there is some sort or API we can call from Powershell to check the status of the job in our script before running the RDX eject utility.

dcc
chjones
Expert
Posts: 117
Liked: 31 times
Joined: Oct 30, 2012 7:53 pm
Full Name: Chris Jones
Contact:

Re: Job Scripts - run only after successful backup?

Post by chjones »

We do this exact thing with Powershell since there isn't a way to have Veeam only run the post-job script if the job was successful. We use the following powershell code in our scripts.

#Get the status of the last job run, this sets the $VBRLastBackupSuccess as $true if the Backup result was "Success" or "Warning", and $false if the job failed
$VBRLastBackupSuccess = (Get-VBRJob | where {$_.Name -eq $VBRBackupJobName}).GetLastResult() -eq "Success" -OR (Get-VBRJob | where {$_.Name -eq $VBRBackupJobName}).GetLastResult() -eq "Warning"

If the value of the $VBRLastBackupSuccess is true, we can then carry on and complete other tasks. To do this we use a simple IF statement:

If(!$VBRLastBackupSuccess)
{ Quit script }
else
{ Carry on and do other steps }

This seems to work quite well for us, had success with this for a few years.

The tricky bit we found is that since Veeam v8, Veeam will wait for your script to finish running before it actually marks the job as a success or failure. If you check the status of the job immediately with a script the job will still be in progress so you wont get an accurate result.

What we do is specify Veeam to called a simple Windows Batch file (in the Storage > Advanced Settings of the job) and that batch file calls Powershell.exe and our own script. Once the batch file calls the powershell the batch file is considered complete so Veeam carries on and completes the last couple of steps and ends the job in Veeam within a few seconds.

One of the first lines in our Powershell script is:

Start-Sleep -S 60

We do this so the powershell is launched but then waits 60 seconds so that Veeam can complete and mark the job as Success, Warning of Failure, and then when we check the job status using the above code we get the accurate result.

It's just something we've worked around since v8.
dcolpitts
Veeam ProPartner
Posts: 119
Liked: 24 times
Joined: Apr 01, 2011 10:36 am
Full Name: Dean Colpitts
Location: Atlantic coast of Canada
Contact:

Re: Job Scripts - run only after successful backup?

Post by dcolpitts » 1 person likes this post

Dude - this was almost exactly what I was looking for. Thank you very much for sharing so I didn't have to spend time going out and re-inventing the wheel so to speak!

Thanks!

dcc
dcolpitts
Veeam ProPartner
Posts: 119
Liked: 24 times
Joined: Apr 01, 2011 10:36 am
Full Name: Dean Colpitts
Location: Atlantic coast of Canada
Contact:

Re: Job Scripts - run only after successful backup?

Post by dcolpitts » 1 person likes this post

So initial testing of the eject script shows it works when manually invoked from the command prompt. We'll see what tomorrow morning brings I guess. Here is my current code - it should work on both VBR8 & VBR9.

Code: Select all

###  begin cut & paste in notepad C:\Windows\Eject_RDX_if_Veeam_Successful.ps1
###  wget http://www.FreeEject.com/FreeEject.zip

Start-Sleep -S 60
Add-PSSnapin VeeamPSSnapin
if (get-command | Where-Object {$_.Name -eq "Connect-VBRServer"}) {Connect-VBRServer -Server $env:computername}
$FreeEjectPath = "C:\Windows\FreeEject.exe"
$RDX_Drive_Letter = "E:"
$VBRBackupJobName = "All VMs"
$VBRLastBackupSuccess = (Get-VBRJob | where {$_.Name -eq $VBRBackupJobName}).GetLastResult() -eq "Success" -OR (Get-VBRJob | where {$_.Name -eq $VBRBackupJobName}).GetLastResult() -eq "Warning"
If(!$VBRLastBackupSuccess)
{ Quit script }
else
{& $FreeEjectPath $RDX_Drive_Letter}

###  end cut & paste of C:\Windows\Eject_RDX_if_Veeam_Successful.ps1

And here is the the batch file to call as the actual script in the job...

Code: Select all

rem --- begin cut & paste in notepad C:\Windows\Eject_RDX_if_Veeam_Successful.cmd
@echo off
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -noprofile -File C:\Windows\Eject_RDX_if_Veeam_Successful.ps1
exit /b
rem --- end cut & paste of Eject_RDX_if_Veeam_Successful.cmd ---
chjones
Expert
Posts: 117
Liked: 31 times
Joined: Oct 30, 2012 7:53 pm
Full Name: Chris Jones
Contact:

Re: Job Scripts - run only after successful backup?

Post by chjones »

Glad to hear that helped and appreciate the feedback. Good work!
FSC_DS
Lurker
Posts: 1
Liked: never
Joined: Sep 07, 2017 3:20 pm
Full Name: Deniss S
Contact:

Re: Job Scripts - run only after successful backup?

Post by FSC_DS »

chjones and dcolpitts, Thank you for your script.

But can you help me.
I used your code and stil receiving $VBRLastBackupSuccess as FALSE and executes quit command. But backup job is Success.
I try to Start-Sleep -S 120, but this didn't help me.

Can you help me?

script_zabbix_veeam_backup_test.ps1

Code: Select all

Start-Sleep -S 120
Add-PSSnapin VeeamPSSnapin
if (get-command | Where-Object {$_.Name -eq "Connect-VBRServer"}) {Connect-VBRServer -Server $env:computername}
$ZabbixValueTrue = 'C:\Zabbix\scripts\script_zabbix_veeam_backup_test_OK.bat'
$ZabbixValueFalse = 'C:\Zabbix\scripts\script_zabbix_veeam_backup_test_False.bat'
$VBRLastBackupSuccess = (Get-VBRJob | where {$_.Name -eq $VBRBackupJobName}).GetLastResult() -eq "Success" -OR (Get-VBRJob | where {$_.Name -eq $VBRBackupJobName}).GetLastResult() -eq "Warning"
If(!$VBRLastBackupSuccess)
{& $ZabbixValueFalse}
else
{& $ZabbixValueTrue}
After the job script

Code: Select all

@echo off
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -noprofile -File C:\Zabbix\scripts\script_zabbix_veeam_backup_test.ps1
exit /b
script_zabbix_veeam_backup_test_OK.bat'

Code: Select all

@echo off
TIMEOUT 10
C:\Zabbix\zabbix_sender.exe -z 192.168.10.10 -s "HOST" -k "TEST" -o "1"
exit /b
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 34 guests