Files
2026-08-10 22:10:17 +02:00

1335 lines
18 KiB
PowerShell

<#
---------------------------------------------------------------------------
Module......: BackupJobs.psm1
Projekt.....: Veeam Enterprise Installer
Version.....: 2.0.0
Beschreibung:
Verwaltung von Veeam Backup Jobs.
Ziel:
Veeam Backup & Replication 13
Funktionen:
- Job Definition
- Zeitplanung
- Retention
- Guest Processing
- Job Prüfung
---------------------------------------------------------------------------#>
Set-StrictMode -Version Latest
#region Variablen
$Script:BackupJobsVersion = "2.0.0"
#endregion
############################################################
# New-BackupJobDefinition
############################################################
function New-BackupJobDefinition {
<#
.SYNOPSIS
Erstellt Backup Job Definition.
#>
param(
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[string]$Repository,
[ValidateRange(1, 9999)]
[int]$RetentionDays = 14
)
[PSCustomObject]@{
Name =
$Name
Repository =
$Repository
RetentionDays =
$RetentionDays
Enabled =
$true
Created =
Get-Date
}
}
############################################################
# Test-BackupJobDefinition
############################################################
function Test-BackupJobDefinition {
<#
.SYNOPSIS
Prüft Job Definition.
#>
param(
[Parameter(Mandatory)]
[PSCustomObject]$Job
)
$RequiredProperties = @("Name", "Repository", "RetentionDays")
$MissingProperties = @(
$RequiredProperties |
Where-Object { $Job.PSObject.Properties.Name -notcontains $_ }
)
$NameDefined = $false
$RepositoryDefined = $false
$RetentionValid = $false
if($MissingProperties.Count -eq 0)
{
$NameDefined = ![string]::IsNullOrWhiteSpace([string]$Job.Name)
$RepositoryDefined = ![string]::IsNullOrWhiteSpace([string]$Job.Repository)
$RetentionValid = ($Job.RetentionDays -is [int]) -and ($Job.RetentionDays -gt 0)
}
[PSCustomObject]@{
Name = if($Job.PSObject.Properties.Name -contains "Name") { $Job.Name } else { $null }
NameDefined = $NameDefined
RepositoryDefined = $RepositoryDefined
RetentionValid = $RetentionValid
MissingProperties = $MissingProperties
Valid = ($NameDefined -and $RepositoryDefined -and $RetentionValid)
Status = "Checked"
}
}
#Beginn PART 2
############################################################
# New-BackupSchedule
############################################################
function New-BackupSchedule {
<#
.SYNOPSIS
Erstellt Backup Zeitplan.
#>
param(
[ValidateSet(
"Daily",
"Weekly",
"Monthly"
)]
[string]$Type = "Daily",
[ValidatePattern('^(?:[01]\d|2[0-3]):[0-5]\d$')]
[string]$Time = "22:00"
)
[PSCustomObject]@{
Type =
$Type
Time =
$Time
Enabled =
$true
Created =
Get-Date
}
}
############################################################
# New-AlphabeticalBackupSchedules
############################################################
function New-AlphabeticalBackupSchedules {
<#
.SYNOPSIS
Ordnet VMs anhand des Anfangszeichens konfigurierten Zeitfenstern zu.
#>
param(
[Parameter(Mandatory)]
[string]$JobName,
[AllowEmptyCollection()]
[string[]]$VirtualMachines = @(),
[Parameter(Mandatory)]
[object[]]$ScheduleGroups
)
$Results = @()
foreach($Group in $ScheduleGroups)
{
$Required = @("Name", "From", "To", "Time")
$Missing = @(
$Required |
Where-Object { $Group.PSObject.Properties.Name -notcontains $_ }
)
if($Missing.Count -gt 0)
{
throw "Zeitplangruppe enthält nicht alle Pflichtwerte: $($Missing -join ', ')"
}
if([string]$Group.Time -notmatch '^(?:[01]\d|2[0-3]):[0-5]\d$')
{
throw "Ungültige Uhrzeit in Zeitplangruppe '$($Group.Name)': $($Group.Time)"
}
$IsOtherGroup = ([string]$Group.From -eq "Other") -and ([string]$Group.To -eq "Other")
$Members = @()
foreach($VirtualMachine in $VirtualMachines)
{
if([string]::IsNullOrWhiteSpace($VirtualMachine))
{
continue
}
$Initial = $VirtualMachine.Substring(0, 1).ToUpperInvariant()
$IsLetter = $Initial -match '^[A-Z]$'
$Matches = $false
if($IsOtherGroup)
{
$Matches = !$IsLetter
}
elseif($IsLetter)
{
$From = ([string]$Group.From).ToUpperInvariant()
$To = ([string]$Group.To).ToUpperInvariant()
$Matches = ($Initial -ge $From) -and ($Initial -le $To)
}
if($Matches)
{
$Members += $VirtualMachine
}
}
if($Members.Count -gt 0)
{
$Results += [PSCustomObject]@{
Group = [string]$Group.Name
JobName = "{0}-{1}" -f $JobName, $Group.Name
VirtualMachines = $Members
Schedule = New-BackupSchedule -Type Daily -Time ([string]$Group.Time)
}
}
}
return $Results
}
############################################################
# New-FullBackupSchedule
############################################################
function New-FullBackupSchedule {
<#
.SYNOPSIS
Erstellt Full Backup Planung.
#>
param(
[ValidateSet(
"Weekly",
"Monthly"
)]
[string]$Type = "Weekly",
[string]$Day = "Saturday"
)
[PSCustomObject]@{
Type =
"Full Backup"
Schedule =
$Type
Day =
$Day
Enabled =
$true
}
}
############################################################
# New-SyntheticFullConfiguration
############################################################
function New-SyntheticFullConfiguration {
<#
.SYNOPSIS
Erstellt Synthetic Full Konfiguration.
#>
param(
[bool]$Enabled = $true,
[string]$Day = "Saturday"
)
[PSCustomObject]@{
Enabled =
$Enabled
Day =
$Day
Type =
"Synthetic Full"
}
}
############################################################
# Get-BackupJobOptions
############################################################
function Get-BackupJobOptions {
<#
.SYNOPSIS
Standard Job Optionen.
#>
[PSCustomObject]@{
Compression =
"Optimal"
StorageOptimization =
"LocalTarget"
Encryption =
$true
ChangedBlockTracking =
$true
}
}
#Beginn PART 3
############################################################
# New-ApplicationAwareConfiguration
############################################################
function New-ApplicationAwareConfiguration {
<#
.SYNOPSIS
Erstellt Application Aware Processing.
#>
param(
[bool]$Enabled = $true,
[bool]$RequireSuccessfulBackup = $true
)
[PSCustomObject]@{
Enabled =
$Enabled
RequireSuccessfulBackup =
$RequireSuccessfulBackup
VSS =
$true
SQLProcessing =
$true
ExchangeProcessing =
$true
}
}
############################################################
# New-GuestProcessingConfiguration
############################################################
function New-GuestProcessingConfiguration {
<#
.SYNOPSIS
Erstellt Guest Processing Einstellungen.
#>
param(
[string]$CredentialAccount,
[bool]$EnableGuestIndexing = $false
)
[PSCustomObject]@{
Enabled =
$true
Credential =
$CredentialAccount
GuestIndexing =
$EnableGuestIndexing
FileSystemIndexing =
$false
}
}
############################################################
# New-BackupCredentialReference
############################################################
function New-BackupCredentialReference {
<#
.SYNOPSIS
Erstellt Credential Referenz.
#>
param(
[Parameter(Mandatory)]
[string]$Account
)
[PSCustomObject]@{
Account =
$Account
Protected =
$false
Created =
Get-Date
}
}
############################################################
# Deploy-BackupJobDefinition
############################################################
function Deploy-BackupJobDefinition {
<#
.SYNOPSIS
Prüft die Voraussetzungen für ein Backup-Job-Deployment.
.DESCRIPTION
Erstellt keinen Veeam-Job, solange Plattform und zu schützende
Veeam-Objekte nicht Bestandteil der Definition sind.
#>
param(
[Parameter(Mandatory)]
[PSCustomObject]$Job
)
$Validation =
Test-BackupJobDefinition `
-Job $Job
[PSCustomObject]@{
Job =
$Job
Validation =
$Validation
DeploymentReady =
$false
Deployed =
$false
Reason =
if(!$Validation.Valid)
{
"Die Jobdefinition ist ungültig."
}
else
{
"Plattform und zu sichernde Veeam-Objekte fehlen. Es wurde kein Veeam-Job erstellt."
}
Time =
Get-Date
}
}
#Beginn PART 4
############################################################
# New-BackupJobDefinitionFile
############################################################
function New-BackupJobDefinitionFile {
<#
.SYNOPSIS
Speichert Job Definition.
#>
param(
[Parameter(Mandatory)]
[PSCustomObject]$Job,
[string]$Path =
(Join-Path (Get-ProjectPaths).Config "Jobs")
)
if(!(Test-Path $Path))
{
New-Item `
-Path $Path `
-ItemType Directory `
-Force |
Out-Null
}
$SafeName = $Job.Name -replace '[\\/:*?"<>|]', '_'
$File =
Join-Path `
$Path `
(
$SafeName +
".json"
)
$Job |
ConvertTo-Json `
-Depth 8 |
Out-File `
-FilePath $File `
-Encoding UTF8 `
-Force
return $File
}
############################################################
# Get-BackupJobDefinitions
############################################################
function Get-BackupJobDefinitions {
<#
.SYNOPSIS
Lädt Job Definitionen.
#>
param(
[string]$Path =
(Join-Path (Get-ProjectPaths).Config "Jobs")
)
if(!(Test-Path $Path))
{
return $null
}
Get-ChildItem `
-Path $Path `
-Filter "*.json" |
ForEach-Object {
Get-Content -LiteralPath $_.FullName -Raw |
ConvertFrom-Json
}
}
############################################################
# Test-BackupJobHealth
############################################################
function Test-BackupJobHealth {
<#
.SYNOPSIS
Prüft Backup Job Status.
#>
param(
[Parameter(Mandatory)]
[PSCustomObject]$Job
)
[PSCustomObject]@{
Name =
$Job.Name
Enabled =
$Job.Enabled
Repository =
$Job.Repository
Retention =
$Job.RetentionDays
Status =
if((Test-BackupJobDefinition -Job $Job).Valid)
{
"DefinitionValid"
}
else
{
"ERROR"
}
Checked =
Get-Date
}
}
############################################################
# Get-BackupJobReport
############################################################
function Get-BackupJobReport {
<#
.SYNOPSIS
Erstellt Job Report.
#>
param(
[Parameter(Mandatory)]
[PSCustomObject]$Job
)
[PSCustomObject]@{
Job =
$Job
Health =
Test-BackupJobHealth `
-Job $Job
Options =
Get-BackupJobOptions
Time =
Get-Date
}
}
#Beginn PART 5
############################################################
# Deploy-BackupEnvironment
############################################################
function Deploy-BackupEnvironment {
<#
.SYNOPSIS
Bereitet komplette Backup Umgebung vor.
#>
param(
[Parameter(Mandatory)]
[array]$Jobs
)
$Results = @()
foreach($Job in $Jobs)
{
$Results +=
Deploy-BackupJobDefinition `
-Job $Job
}
return $Results
}
############################################################
# Test-BackupEnvironment
############################################################
function Test-BackupEnvironment {
<#
.SYNOPSIS
Prüft Backup Umgebung.
#>
param(
[Parameter(Mandatory)]
[array]$Jobs
)
$Report = @()
foreach($Job in $Jobs)
{
$Report +=
Test-BackupJobHealth `
-Job $Job
}
return $Report
}
############################################################
# Get-BackupConfigurationSummary
############################################################
function Get-BackupConfigurationSummary {
<#
.SYNOPSIS
Erstellt Zusammenfassung.
#>
param(
[Parameter(Mandatory)]
[array]$Jobs
)
[PSCustomObject]@{
JobCount =
$Jobs.Count
EnabledJobs =
(
$Jobs |
Where-Object {
$_.Enabled
}
).Count
Repositories =
(
$Jobs.Repository |
Sort-Object |
Select-Object -Unique
)
Created =
Get-Date
}
}
############################################################
# Remove-BackupJobDefinition
############################################################
function Remove-BackupJobDefinition {
<#
.SYNOPSIS
Entfernt gespeicherte Job Definition.
#>
param(
[Parameter(Mandatory)]
[string]$Name,
[string]$Path =
(Join-Path (Get-ProjectPaths).Config "Jobs")
)
$SafeName = $Name -replace '[\\/:*?"<>|]', '_'
$File =
Join-Path `
$Path `
(
$SafeName +
".json"
)
if(Test-Path -LiteralPath $File -PathType Leaf)
{
Remove-Item `
-LiteralPath $File `
-Force
return $true
}
return $false
}
#Beginn PART 6
############################################################
# Get-BackupJobConfigurationReport
############################################################
function Get-BackupJobConfigurationReport {
<#
.SYNOPSIS
Erstellt vollständigen Job Bericht.
#>
param(
[Parameter(Mandatory)]
[array]$Jobs
)
[PSCustomObject]@{
Summary =
Get-BackupConfigurationSummary `
-Jobs $Jobs
Health =
Test-BackupEnvironment `
-Jobs $Jobs
Jobs =
$Jobs
Generated =
Get-Date
}
}
############################################################
# Test-BackupConfiguration
############################################################
function Test-BackupConfiguration {
<#
.SYNOPSIS
Vollständige Backup Prüfung.
#>
param(
[Parameter(Mandatory)]
[array]$Jobs
)
$Report =
Get-BackupJobConfigurationReport `
-Jobs $Jobs
[PSCustomObject]@{
Valid =
(
$Report.Health.Status -notcontains "ERROR"
)
Report =
$Report
Time =
Get-Date
}
}
############################################################
# Get-BackupJobsModuleInformation
############################################################
function Get-BackupJobsModuleInformation {
<#
.SYNOPSIS
Liefert Modulstatus.
#>
[PSCustomObject]@{
Module =
"BackupJobs.psm1"
Version =
$Script:BackupJobsVersion
Loaded =
$true
}
}
#Beginn PART 7
############################################################
# Export Module Functions
############################################################
Export-ModuleMember `
-Function `
New-BackupJobDefinition,
Test-BackupJobDefinition,
New-BackupSchedule,
New-AlphabeticalBackupSchedules,
New-FullBackupSchedule,
New-SyntheticFullConfiguration,
Get-BackupJobOptions,
New-ApplicationAwareConfiguration,
New-GuestProcessingConfiguration,
New-BackupCredentialReference,
Deploy-BackupJobDefinition,
New-BackupJobDefinitionFile,
Get-BackupJobDefinitions,
Test-BackupJobHealth,
Get-BackupJobReport,
Deploy-BackupEnvironment,
Test-BackupEnvironment,
Get-BackupConfigurationSummary,
Remove-BackupJobDefinition,
Get-BackupJobConfigurationReport,
Test-BackupConfiguration,
Get-BackupJobsModuleInformation
############################################################
# Ende BackupJobs.psm1
############################################################