-
Notifications
You must be signed in to change notification settings - Fork 5
/
vbr-job-scanner-post-script.ps1
99 lines (83 loc) · 3.91 KB
/
vbr-job-scanner-post-script.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<#
.NAME
Veeam Backup & Replication - Backup Checker
.DESCRIPTION
This script checks for suspicious incremental backup sizes and analyzes the last
x (-Depth) incremental backup jobs and determines if the incremental size has grown above y% (-Growth).
It also determines the duration of a job session that is above the specified percentage (-Duration), based on the amount of
last backup sessions (-Depth).
This script is for use as a post-job script within a backup job. A warning message is generated in the corresponding backup job
if the threshold value was exceeded.
Details can be found on https://github.com/yetanothermightytool/powershell/blob/master/vbr/vbr-job-scanner/README.md
.NOTES
File Name : vbr-job-scanner-post-script.ps1
Author : Stephan "Steve" Herzig
Requires : PowerShell, Veeam Backup & Replication v12
.VERSION
1.1
#>
Param(
[Parameter(Mandatory=$true)]
[string]$Depth,
[Parameter(Mandatory=$false)]
[string]$Growth,
[Parameter(Mandatory=$false)]
[string]$Duration
)
# Funciton to get Process ID - Credits to Tom Sightler
function Get-VbrJobSessionFromPID {
$parentpid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
$parentcmd = (Get-WmiObject Win32_Process -Filter "processid='$parentpid'").CommandLine
$job = Get-VBRJob -WarningAction SilentlyContinue | ?{$parentcmd -like "*"+$_.Id.ToString()+"*"}
$session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $job.Name) -and ($parentcmd -like "*"+$_.Id.ToString()+"*")}
return $session
}
# Function to get JobName from running process
function Get-VbrJobNameFromPID {
$parentPid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
$parentCmd = (Get-WmiObject Win32_Process -Filter "processid='$parentPid'").CommandLine
$cmdArgs = $parentCmd.Replace('" "','","').Replace('"','').Split(',')
$jobName = (Get-VBRJob | ? {$cmdArgs[4] -eq $_.Id.ToString()}).Name
return $jobName
}
# Variables
$finalResult = @()
$bkpJobName = Get-VbrJobNameFromPID
$bkpJob = Get-VBRJob -Name $bkpJobName -WarningAction SilentlyContinue
$bkpSession = Get-VBRBackupSession| Where-Object {$_.jobId -eq $bkpJob.Id.Guid} | Where-Object {$_.sessioninfo.SessionAlgorithm -eq "Increment"} | Sort-Object EndTimeUTC -Descending
# Get Backup Session
$BackupSession = Get-VbrJobSessionFromPID
# Put the information together
for ($i = 0; $i -le $bkpSession.count; $i++) {
foreach ($sessDetails in $bkpSession) {
$finalResult += New-Object psobject -Property @{
TransferedSize = $sessDetails[0].sessioninfo.Progress.TransferedSize[$i]
}
}
}
### Backup Size Calculation ###
if($Growth){
# Get the last 5 values from the array
$lastValues = $finalResult.TransferedSize[0..$Depth]
# Calculate the average of the last 5 backups
$average = ($lastValues | Measure-Object -Average).Average
# Check if any of the last x backups are more than x% larger than the average
if (($lastValues | Where-Object { $_ -gt $average * $Growth }).Count -gt 0) {
$BackupSession.Logger.AddWarning("Suspicious backup file sizes detected!")
} else {
$BackupSession.Logger.AddSuccess("No unexpected growth detected in the last $Depth incremental Backups")
}
}
### Job Duration Calculation ###
if($Duration){
# Get the last x values (Depth) from the array
$lastValues = $finalResult.DurationSec[0..$Depth]
# Calculate the average of the last x backups
$average = ($lastValues | Measure-Object -Average).Average
# Check if any of the last x backups took more time than the average
if (($lastValues | Where-Object { $_ -gt $average * $Duration }).Count -gt 0) {
$BackupSession.Logger.AddWarning("Unusual job duration")
} else {
$BackupSession.Logger.AddSuccess("Normal job duration")
}
}