PowerShell script exchange
Post Reply
shaun2011
Influencer
Posts: 19
Liked: 1 time
Joined: Nov 12, 2011 11:43 am
Contact:

Backup Copy Job Script Help

Post by shaun2011 »

Hello

Below script is from “buwprof”, is there a way to modify so script would wait for one successful backup copy (if fail retry) and then disables the job?

Reason is to stop the job running more than once during a backup cycle when tape job spans more than 24 hrs. By default job stays disabled and this script will trigger the backup.

Thanks.

Code: Select all

------------------------------------------------------------------------------------------------------------------------------------------------
# Get Parameters
param( $job )
# Import Veeam Snapin and we do not want Errors
asnp -Name VeeamPSSnapin -ErrorAction SilentlyContinue
# If we have a JobName then we go further
if ( $job ) {
    # JobName here
    $jobName          = $job
    # Get Backup Specs
    $backup           = Get-VBRBackup -Name $jobName
    if ( $backup.JobName ) {
        # Get FilePath
        $backupfilepath   = ( $backup.GetStorages() | ?{$_.GetStorageType() -eq "vbk"} ).FilePath
        # Get Directory for deleting
        $backupdirpath    = ( $backup.GetStorages() | ?{$_.GetStorageType() -eq "vbk"} ).DirPath
        # If current active VBK is found on media then media has not been rotated
        if ( -not( Test-Path "$backupfilepath" ) ) {
            # Current active VBK was not found indicating media has been changed
            # First DisableScheduler, If not then the Copy Job will Create a new Directory like $Jobname_1    
            # Get-Job Object
            $CopyJob      = Get-VBRJob -Name $jobName    
            # Disbale Scheduler
            $CopyJob.DisableScheduler()    
            # then Wait until Job is Stopped (and is not running anymore)
            while ( -not( $CopyJob.IsStopped() ) ) { 
            # Do Nothing 
            }    
            # If stopped then Delete from VeeamGUI
            $backup.Delete()    
            # Remove Backup Files from Media
            if ( Test-Path "$backupdirpath" ) {
                # Remove whole Directory
                Remove-Item -Path "$backupdirpath" -Recurse
            }
            # Enable Scheduler
            $CopyJob.EnableScheduler()
        }
    } # UPPS NOT CORRECT JOBNAME - BUT WE ARE SILENT
} # NO JOBNAME given - WE ARE SILENT
veremin
Product Manager
Posts: 20284
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Backup Copy Job Script Help

Post by veremin » 1 person likes this post

Hmm, what about checking the time and status of last backup copy job session? If the session occurs during the current day and has “success” as a status, then, the backup copy job can be simply disabled:

Code: Select all

asnp VeeamPSSNapin
$Job = Get-VBRJob -name "Name of backup copy Job"
$LastSession = $Job.FindLastSession()
If (($LastSession.endtime.DayOfYear -eq (Get-date).DayOfYear) -and ($LastSession.State -eq  "Success"))
{$Job.DisableScheduler()}
Actually, you can add some more lines in order to check that backup session ran for, at least, 10 minutes (in other words, it did process something):

Code: Select all

asnp VeeamPSSNapin
$Job = Get-VBRJob -name "Name of backup copy Job"
If (($LastSession.endtime.DayOfYear -eq (Get-date).DayOfYear) –and ($LastSession.State -eq  "Success") -and ((($LastSession.endtime) - ($LastSession.creationtime)).Minutes -ge "10"))
{$Job.DisableScheduler()}
Hope this helps.
Thanks.
shaun2011
Influencer
Posts: 19
Liked: 1 time
Joined: Nov 12, 2011 11:43 am
Contact:

Re: Backup Copy Job Script Help

Post by shaun2011 »

thanks Vladimir.
had trouble testing the if logic, it won't get get picked up properly cos copy jobs include more activities than just straight backups, so needs to do some more testing. also i've noticed you cannot run scripts against disabled jobs ..
isn't it safe to say copy jobs are for GFS to a storage device and not USB/tape friendly? there's lots of forum posts, vbk reg key does not work (copy job minimum retention is 2 not 1!), would have been nice to control continuous behavior in copy jobs when required, so turn off for tape jobs.
scripts/work arounds make me think third party backup app is still the easiest way to go.
unless someone can tell me a better way to get copy jobs working for tape/usb?
cheers,
veremin
Product Manager
Posts: 20284
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Backup Copy Job Script Help

Post by veremin »

had trouble testing the if logic, it won't get get picked up properly cos copy jobs include more activities than just straight backups, so needs to do some more testing.
In this case, the best option will be to catch different sessions of backup copy job, using .FindLastSession() parameter and see how they differ from one another. That being said, you can take a look at what parameters/properties variable $LastSession has and use some of them in order to distinguish sessions of backup copy job.
also i've noticed you cannot run scripts against disabled jobs.
Could elaborate on it a little bit? You can use PS scripts with disabled jobs. Furthermore,some scripts even require that you disable a given job first.
isn't it safe to say copy jobs are for GFS to a storage device and not USB/tape friendly?
Currently, if you want to use backup copy jobs in conjunction with rotated drives or tapes, you have to use certain workarounds including manual scripts and “dummy” jobs. However, we’re already considering how this functionality can be improved in one of the next releases. For instance, usage of backup copy jobs as a source for backup to tape jobs is in our feature list.

Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests