PowerShell script exchange
Post Reply
baatch
Enthusiast
Posts: 30
Liked: 4 times
Joined: May 16, 2013 12:52 am
Full Name: Alexander
Contact:

Post job activity, run only last saturday of the month?

Post by baatch »

Hi

I'am trying to utilize the Post job activity to create a hardlink of the latest .vbk to another folder so that Backup Exec 2012 can back it up on tape.

I have the powershell script working but I want it to run as a post job activity of the veeam job, and only on the last saturday of the month.

Is this possible?
frejak73
Novice
Posts: 8
Liked: never
Joined: Jun 10, 2013 6:03 am
Full Name: Fredrik Jakobsson
Contact:

Re: Post job activity, run only last saturday of the month?

Post by frejak73 »

I should create a small job that triggers the script, and put the job last in line,i see no other plan.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Post job activity, run only last saturday of the month?

Post by veremin »

Is this possible?
Yep, it’s. In order to do it you need to add “auditorial” part to the existing post job script that will check the current date and execute required commands only if it’s last Saturday of the month.

If you need any assistance with scripting, don’t hesitate to let me know.

Thanks.
baatch
Enthusiast
Posts: 30
Liked: 4 times
Joined: May 16, 2013 12:52 am
Full Name: Alexander
Contact:

Re: Post job activity, run only last saturday of the month?

Post by baatch »

Cool if you help me out with the auditorial part of the script :) Below is what I have currently:

Code: Select all

$SourcePath = "E:\Veeamdata\Backup"
$TargetPath = "E:\Tape\Monthly"
$LastVBK = Get-ChildItem $SourcePath\*.vbk| Sort-Object CreationTime -Descending | Select-Object -First 1| Get-ChildItem -name 
fsutil hardlink create $TargetPath\$LastVBK $SourcePath\$LastVBK
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Post job activity, run only last saturday of the month?

Post by veremin »

There is an existing custom function called Get-LastxOfMonth that is responsible for finding out last Saturday of a month, so you can easily use it:

Code: Select all

Function Get-LastxOfMonth {            
[CmdletBinding()]            
param(            
    [parameter(Mandatory)]            
    [String]$Day,            
            
    [parameter(ParameterSetName='ByDate',Mandatory,ValueFromPipeline)]            
    [System.DateTime]$Date,            
            
    [parameter(ParameterSetName='ByString',Mandatory,ValueFromPipelineByPropertyName)]            
    [ValidateRange(1,12)]            
    [int]$Month,             
    [parameter(ParameterSetName='ByString',Mandatory,ValueFromPipelineByPropertyName)]            
    [ValidatePattern('^\d{4}$')]            
    [int]$Year,            
            
    [switch]$asDate=$false            
)            
Begin {            
    $alldays = @()            
}            
Process {            
    # Validate the Day string passed as parameter by casting it into            
    if (-not([System.DayOfWeek]::$Day -in 0..6)) {            
        Write-Warning -Message 'Invalid string submitted as Day parameter'            
        return            
    }            
            
    Switch ($PSCmdlet.ParameterSetName)            
    {            
        ByString {            
            # Do nothing, variables are already defined and validated            
        }            
        ByDate   {            
            $Month = $Date.Month            
            $Year = $Date.Year            
        }            
    }            
    # There aren't 32 days in any month so we make sure we iterate through all days in a month            
    0..31 | ForEach-Object -Process {            
        $evaldate = (Get-Date -Year $Year -Month $Month -Day 1).AddDays($_)            
        if ($evaldate.Month -eq $Month)            
        {            
            if ($evaldate.DayOfWeek -eq $Day) {            
                $alldays += $evaldate.Day            
            }            
        }            
    }            
    # Output            
    if ($asDate) {            
        Get-Date -Year $Year -Month $Month -Day $alldays[-1]            
    } else {            
        $alldays[-1]            
    }            
}            
End {}            
}
$Date = Get-Date 
$LastSaturday = Get-LastxOfMonth -Day Saturday -Month $Date.Month -Year $Date.Year
if ($Date.Day -eq $LastSaturday)
{
$SourcePath = "E:\Veeamdata\Backup"
$TargetPath = "E:\Tape\Monthly"
$LastVBK = Get-ChildItem $SourcePath\*.vbk| Sort-Object CreationTime -Descending | Select-Object -First 1| Get-ChildItem -name 
fsutil hardlink create $TargetPath\$LastVBK $SourcePath\$LastVBK
}
Else {Write-OutPut "The current day isn’t last Saturday; no post job activity will be performed"} 
Otherwise, you can just schedule your PS script through Windows Scheduler to run on the last Sunday of the month; might be easier. 

Hope this helps.
Thanks.
baatch
Enthusiast
Posts: 30
Liked: 4 times
Joined: May 16, 2013 12:52 am
Full Name: Alexander
Contact:

Re: Post job activity, run only last saturday of the month?

Post by baatch »

Thanks alot v.Eremin, I'm going to test out the script.

I always thought that the disadvantage of using the task scheduler would be that if veeam fails that day the hardlink would be created for the wrong .vbk.

But then again if I incorporate a auditoral part to the script, and the veeam job doesn't complete at the right date it would not create a hardlink at all.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Post job activity, run only last saturday of the month?

Post by veremin »

I always thought that the disadvantage of using the task scheduler would be that if veeam fails that day the hardlink would be created for the wrong .vbk.
In fact, this problem persists even in case of post job activity - if a given job fails, the post job activity will still be performed and a wrong .vbk file will be copied.

In order to avoid it, the following part needs to be added to the script:

Code: Select all

asnp VeeamPSSnapin
$Job = Get-VBRJob -name "Name of your job"
If ($job.GetLastresult() -eq "Warning" -or $job.GetLastresult() -eq "Success")
{
Place your commands here that you want to start
} Else {} 
But then again if I incorporate a auditoral part to the script, and the veeam job doesn't complete at the right date it would not create a hardlink at all.
In this case, it would be more advisable to catch not a current day, but, instead, a day when a given backup session was started and check whether it was last Saturday or not:

Code: Select all

asnp VeeamPSSnapin
Function Get-LastxOfMonth {            
[CmdletBinding()]            
param(            
    [parameter(Mandatory)]            
    [String]$Day,            
            
    [parameter(ParameterSetName='ByDate',Mandatory,ValueFromPipeline)]            
    [System.DateTime]$Date,            
            
    [parameter(ParameterSetName='ByString',Mandatory,ValueFromPipelineByPropertyName)]            
    [ValidateRange(1,12)]            
    [int]$Month,             
    [parameter(ParameterSetName='ByString',Mandatory,ValueFromPipelineByPropertyName)]            
    [ValidatePattern('^\d{4}$')]            
    [int]$Year,            
            
    [switch]$asDate=$false            
)            
Begin {            
    $alldays = @()            
}            
Process {            
    # Validate the Day string passed as parameter by casting it into            
    if (-not([System.DayOfWeek]::$Day -in 0..6)) {            
        Write-Warning -Message 'Invalid string submitted as Day parameter'            
        return            
    }            
            
    Switch ($PSCmdlet.ParameterSetName)            
    {            
        ByString {            
            # Do nothing, variables are already defined and validated            
        }            
        ByDate   {            
            $Month = $Date.Month            
            $Year = $Date.Year            
        }            
    }            
    # There aren't 32 days in any month so we make sure we iterate through all days in a month            
    0..31 | ForEach-Object -Process {            
        $evaldate = (Get-Date -Year $Year -Month $Month -Day 1).AddDays($_)            
        if ($evaldate.Month -eq $Month)            
        {            
            if ($evaldate.DayOfWeek -eq $Day) {            
                $alldays += $evaldate.Day            
            }            
        }            
    }            
    # Output            
    if ($asDate) {            
        Get-Date -Year $Year -Month $Month -Day $alldays[-1]            
    } else {            
        $alldays[-1]            
    }            
}            
End {}            
}
$Job = Get-VBRJob -name "Name of your Backup Job"
$StartTime = $Job.FindLastSession().CreationTime 
$LastSaturday = Get-LastxOfMonth -Day Saturday -Month $Date.Month -Year $Date.Year
if ($StartTime.Day -eq $LastSaturday)
{
$SourcePath = "E:\Veeamdata\Backup"
$TargetPath = "E:\Tape\Monthly"
$LastVBK = Get-ChildItem $SourcePath\*.vbk| Sort-Object CreationTime -Descending | Select-Object -First 1| Get-ChildItem -name 
fsutil hardlink create $TargetPath\$LastVBK $SourcePath\$LastVBK
}
Else {Write-OutPut "The last session wasn't started on last Saturday; no post job activity will be performed"} 
Hope this helps.
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests