Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions AlertSender.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
param(
[String]$JobId,
[String]$SessionId,
[String]$JobType,
$Config,
Expand Down Expand Up @@ -72,7 +73,7 @@ try {
# Job info preparation

## Get the backup session.
$session = (Get-VBRSessionInfo -SessionId $SessionId -JobType $JobType).Session
$session = (Get-VBRSessionInfo -SessionId $SessionId -JobId $JobId -JobType $JobType).Session

## Initiate logger variable
$vbrSessionLogger = $session.Logger
Expand All @@ -84,7 +85,7 @@ try {
do {
Write-LogMessage -Tag 'INFO' -Message 'Session not completed. Sleeping...'
Start-Sleep -Seconds 10
$session = (Get-VBRSessionInfo -SessionId $SessionId -JobType $JobType).Session
$session = (Get-VBRSessionInfo -SessionId $SessionId -JobId $JobId -JobType $JobType).Session
}
while ($false -eq $session.Info.IsCompleted -and $stopwatch.Elapsed -lt $timeout)
$stopwatch.Stop()
Expand Down
73 changes: 51 additions & 22 deletions Bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ param (
[string]$SessionId = $null
)

function FinishBootstrap {
param(
[switch]$Failed
)

if ($config.logging.enabled) {
Stop-Logging

if ($newLogfile) {
# Rename log file to include the job name.
try {
Rename-Item -Path $logFile -NewName "$(Split-Path $newLogfile -Leaf)" -ErrorAction Stop
}
catch {
Write-Output "ERROR: Failed to rename log file: $_" | Out-File $logFile -Append
}
}
}
if ($Failed) {
exit 1
}
else {
exit 0
}
}

# Import modules
Import-Module Veeam.Backup.PowerShell -DisableNameChecking
Import-Module "$PSScriptRoot\resources\Logger.psm1"
Expand All @@ -19,15 +45,15 @@ if ($JobId -and -not $SessionId) {
$SessionId = (Get-VBRBackupSession | Where-Object {$_.JobId -eq $JobId} | Sort-Object EndTimeUTC -Descending | Select-Object -First 1).Id.ToString()
if ($null -eq $SessionId) {
Write-Output 'ERROR: Failed to retrieve the session ID for the provided job ID. Please check the job ID.'
exit 1
FinishBootstrap -Failed
}
}
elseif ($SessionId -and -not $JobId) {
Write-Output 'INFO: SessionId is provided but not JobId. Attempting to retrieve the last job ID for the session.'
$JobId = (Get-VBRBackupSession -Id $SessionId | Sort-Object EndTimeUTC -Descending | Select-Object -First 1).JobId.ToString()
if ($null -eq $JobId) {
Write-Output 'ERROR: Failed to retrieve the job ID for the provided session ID. Please check the session ID.'
exit 1
FinishBootstrap -Failed
}
}
if ($JobId -and $SessionId) {
Expand Down Expand Up @@ -91,19 +117,32 @@ else {
# At time of writing, there is no alternative way to discover the job time.
Write-LogMessage -Tag 'INFO' -Message 'Getting VBR job details'
$job = Get-VBRJob -WarningAction SilentlyContinue | Where-Object {$_.Id.ToString() -eq $JobId}
if (!$job) {
# Can't locate non tape job so check if it's a tape job
$job = Get-VBRTapeJob -WarningAction SilentlyContinue | Where-Object {$_.Id.ToString() -eq $JobId}
$JobType = $job.Type
if ($job) {
$JobType = $job.JobType
}
else {
$JobType = $job.JobType
# Can't locate non-tape job so check if it's a tape job
Write-LogMessage -Tag 'DEBUG' -Message "Job with ID $JobId not found in VBR jobs. Checking for tape jobs."
$job = Get-VBRTapeJob -WarningAction SilentlyContinue | Where-Object {$_.Id.ToString() -eq $JobId}
if ($job) {
$JobType = $job.Type
}
else {
Write-LogMessage -Tag 'ERROR' -Message "Job with ID $JobId not found in tape jobs. Exiting."
FinishBootstrap -Failed
}
}


# Get the session information and name.
Write-LogMessage -Tag 'INFO' -Message 'Getting VBR session information'
$sessionInfo = Get-VBRSessionInfo -SessionId $SessionId -JobType $JobType
try {
$sessionInfo = Get-VBRSessionInfo -SessionId $SessionId -JobId $JobId -JobType $JobType
}
catch {
Write-LogMessage -Tag 'ERROR' -Message "Failed to retrieve session information: $_"
FinishBootstrap -Failed
}
$jobName = $sessionInfo.JobName
$vbrSessionLogger = $sessionInfo.Session.Logger

Expand All @@ -112,7 +151,7 @@ $vbrLogEntry = $vbrSessionLogger.AddLog('[VeeamNotify] Parsing job & session inf
# Quit if job type is not supported.
if ($JobType -notin $supportedTypes) {
Write-LogMessage -Tag 'ERROR' -Message "Job type '$($JobType)' is not supported."
exit 1
FinishBootstrap -Failed
}

Write-LogMessage -Tag 'INFO' -Message "Bootstrap script for Veeam job '$jobName' (job $JobId session $SessionId) - Session & job detection complete."
Expand All @@ -129,6 +168,7 @@ $newLogfile = "$PSScriptRoot\log\$($date)-$($logJobName).log"

# Build argument string for the alert sender script.
$powershellArguments = "-NoProfile -File $PSScriptRoot\AlertSender.ps1", `
"-JobId `"$JobId`"", `
"-SessionId `"$SessionId`"", `
"-JobType `"$JobType`"", `
"-Config `"$configRaw`"", `
Expand All @@ -149,18 +189,7 @@ try {
catch {
Write-LogMessage -Tag 'ERROR' -Message "Failed to launch AlertSender.ps1: $_"
$vbrSessionLogger.UpdateErr($vbrLogEntry, '[VeeamNotify] Failed to launch Alert Sender.', "Please check the log: $newLogfile") | Out-Null
exit 1
FinishBootstrap -Failed
}

# Stop logging.
if ($config.logging.enabled) {
Stop-Logging

# Rename log file to include the job name.
try {
Rename-Item -Path $logFile -NewName "$(Split-Path $newLogfile -Leaf)" -ErrorAction Stop
}
catch {
Write-Output "ERROR: Failed to rename log file: $_" | Out-File $logFile -Append
}
}
FinishBootstrap
18 changes: 10 additions & 8 deletions resources/VBRSessionInfo.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ function Get-VBRSessionInfo {
[Parameter(Mandatory)][ValidateNotNullOrEmpty()]
[string]$SessionId,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()]
[string]$JobType
[string]$JobType,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()]
[string]$JobId
)

# Import VBR module
Expand All @@ -33,16 +35,16 @@ function Get-VBRSessionInfo {
# to load in whatever's required to utilise the GetByOriginalSessionId method.
# See https://forums.veeam.com/powershell-f26/want-to-capture-running-jobs-by-session-type-i-e-sobr-tiering-t75583.html#p486295
Get-VBRSession -Id $SessionId | Out-Null

# Get the session details.
$session = [Veeam.Backup.Core.CBackupSession]::GetByOriginalSessionId($SessionId)
$session = [Veeam.Backup.Core.CBackupSession]::GetByJob($JobId) | Select-Object -Last 1

# Copy the job's name to it's own variable.
if ($JobType -eq 'EpAgentBackup') {
$jobName = $job.Info.Name
}
elseif ($JobType -in 'BackupToTape', 'FileToTape') {
$jobName = $job.Name
if ($null -eq $session) {
throw "$JobType job session with ID '$SessionId' could not be found."
}

# Extract the job name from the session
$jobName = $session.Name
}
}

Expand Down
Loading