PowerShell script exchange
Post Reply
daniel_rogers
Influencer
Posts: 12
Liked: never
Joined: Feb 03, 2015 6:53 am
Full Name: Daniel Rogers
Contact:

D/W/M/Y schedule scripts (sharing and feedback)

Post by daniel_rogers »

Hi,

I've been working on something that I thought I'd put up for sharing and feedback. I initially had some trouble with the Start-VBRJob, (it doesn't accept some parameters such as -FullBackup / -RetryJob via a variable??) and also detecting when a job had failed. These scripts mimic the 3 retry of the GUI jobs if it detects a failure.

I like having the tape jobs continuously monitor the output directories. I had an issue where if the backup job failed, the file would become available (unlocked) and would start writing to tape. I didn't want this to happen until I could confirm the backup had been successful (or at least given it a few retries). Thus the tape jobs are disabled by default, enabled in the script and have a post script to disable them again.

Hope it helps someone or maybe you can help me with optimisations or ideas.

Environment: Veeam 8 Patch 2, VMWare

Schedule: No scheduling configured through Veeam GUI - Powershell via task scheduler = Daily INCR, Weekly/Monthly/Yearly FULL

Disk Jobs (Retention): DAILY_ (8), WEEKLY_ (1), MONTHLY_ (1), YEARLY_ (1)
Tape Jobs (Continuous & disabled): Offsite Tape Weekly, Offsite Tape Monthly, Offsite Tape Yearly

DAILY_ Script:

Code: Select all

# Daily INCR and Weekly FULL
# Script scheduled for 10PM Monday - Friday and 6:00AM Sunday

# Imports Veeam PowerShell module
Import-Module "C:\Program Files\Veeam\Backup and Replication\Backup\Veeam.Backup.PowerShell.dll"

# Assigns date variable for day checks
$date = Get-Date
$i = 0

function Start-Backup {
	param($name, [bool]$full, [bool]$retry)
	
	if ($full -eq $false -AND $retry -eq $false) { Start-VBRJob -Job $name }
	if ($full -eq $true -AND $retry -eq $false) { Start-VBRJob -Job $name -FullBackup }
	if ($retry -eq $true) { Start-VBRJob -Job $name -RetryBackup }
	
	$job = Get-VBRJob -Name $name
	$script:outcome = $job.GetLastResult()
}

# If weekday - start INCR backup (Disk only)
if ($date.DayOfWeek -eq "Monday" -or $date.DayOfWeek -eq "Tuesday" -or $date.DayOfWeek -eq "Wednesday" -or $date.DayOfWeek -eq "Thursday" -or $date.DayOfWeek -eq "Friday") {
	
	$jobname = "DAILY_"
	Start-Backup $jobname $false $false
	while ($script:outcome -eq "Failed" -AND $i -le 3) {
		Start-Backup $jobname $false $true; $i++ }
}

# If Sunday - start FULL backup	(Disk only)
if ($date.DayOfWeek -eq "Sunday") {
	
	$jobname = "DAILY_"
	Start-Backup $jobname $true $false
	while ($script:outcome -eq "Failed" -AND $i -le 3) {
		Start-Backup $jobname $false $true; $i++ }
}

W/M/Y Script

Code: Select all

# Checks Saturday @ 6:00AM for:
	# First Saturday of the year
	# First Saturday of the month
	# Saturday
# Starts appropriate job
# Tape jobs continually monitor output directories for new files
# Fresh tapes will need to be managed manually

# Imports Veeam PowerShell module
Import-Module "C:\Program Files\Veeam\Backup and Replication\Backup\Veeam.Backup.PowerShell.dll"

# Assigns date variable for day checks
$date = Get-Date
$i = 0

#Starts backup. Accepts [string]Job Name, [bool]FullBackup, [bool]RetryBackup
function Start-Backup {
	param($name, [bool]$full, [bool]$retry)
	
	if ($full -eq $false -AND $retry -eq $false) { Start-VBRJob -Job $name }
	if ($full -eq $true -AND $retry -eq $false) { Start-VBRJob -Job $name -FullBackup }
	if ($retry -eq $true) { Start-VBRJob -Job $name -RetryBackup }
	
	$job = Get-VBRJob -Name $name
	$script:outcome = $job.GetLastResult()
}

#If yearly (Disk -> Tape)
if ($date.month -eq 1 -and $date.Day -lt 7 -and $date.DayOfWeek -eq "Saturday") {
		
	$jobname = "YEARLY_"
	Start-Backup $jobname $true $false
	while ($script:outcome -eq "Failed" -AND $i -le 3) {
		Start-Backup $jobname $false $true; $i++ }
	
	Enable-VBRJob -job "Offsite Tape Yearly"
}

#If monthly (Disk -> Tape)	
elseif ($date.Day -lt 7 -and $date.DayOfWeek -eq "Saturday") {
	$jobname = "MONTHLY_"
	Start-Backup $jobname $true $false
	while ($script:outcome -eq "Failed" -AND $i -le 3) {
		Start-Backup $jobname $false $true; $i++ }
		
	Enable-VBRJob -job "Offsite Tape Monthly"
}

#If weekly (Disk -> Tape)
elseif ($date.DayOfWeek -eq "Saturday") {
	$jobname = "WEEKLY_"
	Start-Backup $jobname $true $false
	while ($script:outcome -eq "Failed" -AND $i -le 3) {
		Start-Backup $jobname $false $true; $i++ }
		
	Enable-VBRJob -job "Offsite Tape Weekly"
}
Post Reply

Who is online

Users browsing this forum: vizzzor and 25 guests