Job Scripts - run only after successful backup?

VMware specific discussions

Job Scripts - run only after successful backup?

Veeam Logoby dcolpitts » Sun Jan 17, 2016 8:15 pm

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
dcolpitts
Veeam ProPartner
 
Posts: 50
Liked: 9 times
Joined: Fri Apr 01, 2011 10:36 am
Location: Atlantic coast of Canada
Full Name: Dean Colpitts

Re: Job Scripts - run only after successful backup?

Veeam Logoby chjones » Mon Jan 18, 2016 3:12 am

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.
chjones
Enthusiast
 
Posts: 83
Liked: 25 times
Joined: Tue Oct 30, 2012 7:53 pm
Full Name: Chris Jones

Re: Job Scripts - run only after successful backup?

Veeam Logoby dcolpitts » Mon Jan 18, 2016 11:12 am 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: 50
Liked: 9 times
Joined: Fri Apr 01, 2011 10:36 am
Location: Atlantic coast of Canada
Full Name: Dean Colpitts

Re: Job Scripts - run only after successful backup?

Veeam Logoby dcolpitts » Mon Jan 18, 2016 3:54 pm 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 ---
dcolpitts
Veeam ProPartner
 
Posts: 50
Liked: 9 times
Joined: Fri Apr 01, 2011 10:36 am
Location: Atlantic coast of Canada
Full Name: Dean Colpitts

Re: Job Scripts - run only after successful backup?

Veeam Logoby chjones » Mon Jan 18, 2016 7:31 pm

Glad to hear that helped and appreciate the feedback. Good work!
chjones
Enthusiast
 
Posts: 83
Liked: 25 times
Joined: Tue Oct 30, 2012 7:53 pm
Full Name: Chris Jones


Return to VMware vSphere



Who is online

Users browsing this forum: No registered users and 23 guests