<# --------------------------------------------------------------------------- Module......: Repository.psm1 Projekt.....: Veeam Enterprise Installer Version.....: 2.0.0 Beschreibung: Verwaltung von Veeam Backup Repositories. Funktionen: - Repository Pfade prüfen - Speicher prüfen - Ordner erstellen - Berechtigungen vorbereiten - Repository Konfiguration ---------------------------------------------------------------------------#> Set-StrictMode -Version Latest #region Variablen $Script:RepositoryVersion = "2.0.0" #endregion ############################################################ # Test-RepositoryPath ############################################################ function Test-RepositoryPath { <# .SYNOPSIS Prüft Repository Pfad. #> param( [Parameter(Mandatory)] [string]$Path ) return ( Test-Path -Path $Path -PathType Container ) } ############################################################ # Resolve-RepositoryPath ############################################################ function Resolve-RepositoryPath { param( [Parameter(Mandatory)][string]$Path, [AllowEmptyString()][string]$InstallationSource ) if($Path -ne "Auto") { $Root = [IO.Path]::GetPathRoot($Path) if($env:OS -eq "Windows_NT" -and ![string]::IsNullOrWhiteSpace($Root)) { $DriveId = $Root.TrimEnd('\') $Drive = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = '$DriveId'" -ErrorAction SilentlyContinue if($null -ne $Drive -and $Drive.DriveType -ne 3) { throw "Repository-Pfad liegt nicht auf einer lokalen Festplatte: $Path" } } return $Path } if($env:OS -ne "Windows_NT") { throw "Automatische Repository-Laufwerkswahl wird nur unter Windows unterstützt." } $SourceRoot = [IO.Path]::GetPathRoot($InstallationSource) $FixedDrives = @( Get-CimInstance Win32_LogicalDisk -Filter "DriveType = 3" -ErrorAction Stop | Where-Object { [string]::IsNullOrWhiteSpace($SourceRoot) -or $_.DeviceID -ne $SourceRoot.TrimEnd('\') } | Sort-Object FreeSpace -Descending ) if($FixedDrives.Count -eq 0) { throw "Keine lokale Festplatte für das Veeam-Repository gefunden." } return (Join-Path ("{0}\" -f $FixedDrives[0].DeviceID) "VeeamRepository") } ############################################################ # New-RepositoryFolder ############################################################ function New-RepositoryFolder { <# .SYNOPSIS Erstellt Repository Ordner. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path -Path $Path -PathType Container)) { New-Item ` -Path $Path ` -ItemType Directory ` -Force | Out-Null } return $Path } ############################################################ # Get-RepositoryInformation ############################################################ function Get-RepositoryInformation { <# .SYNOPSIS Liefert Repository Informationen. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return $null } $Drive = Split-Path ` $Path ` -Qualifier $Disk = Get-CimInstance ` Win32_LogicalDisk ` -Filter "DeviceID='$Drive'" if($null -eq $Disk) { return [PSCustomObject]@{ Path = $Path SizeGB = $null FreeGB = $null Time = Get-Date } } [PSCustomObject]@{ Path = $Path SizeGB = [math]::Round( $Disk.Size / 1GB, 2 ) FreeGB = [math]::Round( $Disk.FreeSpace / 1GB, 2 ) Time = Get-Date } } #Beginn PART 2 ############################################################ # Test-RepositoryFreeSpace ############################################################ function Test-RepositoryFreeSpace { <# .SYNOPSIS Prüft freien Speicher. #> param( [Parameter(Mandatory)] [string]$Path, [ValidateRange(1, 1048576)] [int]$MinimumGB = 50 ) $Info = Get-RepositoryInformation ` -Path $Path if($null -eq $Info) { return $false } return ( $Info.FreeGB -ge $MinimumGB ) } ############################################################ # Get-RepositoryConfiguration ############################################################ function Get-RepositoryConfiguration { <# .SYNOPSIS Erstellt Repository Konfiguration. #> param( [Parameter(Mandatory)] [string]$Name, [Parameter(Mandatory)] [string]$Path, [ValidateRange(1, 9999)] [int]$RetentionDays = 14 ) [PSCustomObject]@{ Name = $Name Path = $Path RetentionDays = $RetentionDays Type = "Local Repository" Enabled = $true Created = Get-Date } } ############################################################ # Test-RepositoryAccess ############################################################ function Test-RepositoryAccess { <# .SYNOPSIS Prüft Zugriff auf Repository. #> param( [Parameter(Mandatory)] [string]$Path ) try { $TestFile = Join-Path ` $Path ` "repository_test.tmp" New-Item ` -Path $TestFile ` -ItemType File ` -Force | Out-Null Remove-Item ` $TestFile ` -Force return $true } catch { return $false } } ############################################################ # Set-RepositoryPermission ############################################################ function Set-RepositoryPermission { <# .SYNOPSIS Setzt Repository Rechte. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Account ) if(!(Test-Path -Path $Path -PathType Container)) { throw "Repositorypfad nicht gefunden: $Path" } $ACL = Get-Acl ` -Path $Path $Rule = New-Object ` System.Security.AccessControl.FileSystemAccessRule( $Account, "Modify", "ContainerInherit,ObjectInherit", "None", "Allow" ) $ACL.AddAccessRule( $Rule ) Set-Acl ` -Path $Path ` -AclObject $ACL } #Beginn PART 3 ############################################################ # Test-RepositoryHealth ############################################################ function Test-RepositoryHealth { <# .SYNOPSIS Prüft Repository Zustand. #> param( [Parameter(Mandatory)] [string]$Path ) $Exists = Test-RepositoryPath ` -Path $Path $Access = if($Exists) { Test-RepositoryAccess ` -Path $Path } else { $false } $Space = if($Exists) { Test-RepositoryFreeSpace ` -Path $Path } else { $false } [PSCustomObject]@{ Path = $Path Exists = $Exists Access = $Access FreeSpaceOK = $Space Status = if( $Exists -and $Access -and $Space ) { "OK" } else { "ERROR" } } } ############################################################ # Initialize-Repository ############################################################ function Initialize-Repository { <# .SYNOPSIS Bereitet Repository vor. #> param( [Parameter(Mandatory)] [string]$Path, [string]$Account ) New-RepositoryFolder ` -Path $Path if( $Account ) { Set-RepositoryPermission ` -Path $Path ` -Account $Account } return ( Get-RepositoryInformation ` -Path $Path ) } ############################################################ # Get-RepositoryReport ############################################################ function Get-RepositoryReport { <# .SYNOPSIS Erstellt Repository Bericht. #> param( [Parameter(Mandatory)] [string]$Path ) [PSCustomObject]@{ Repository = $Path Health = Test-RepositoryHealth ` -Path $Path Information = Get-RepositoryInformation ` -Path $Path Time = Get-Date } } #Beginn PART 4 ############################################################ # New-RepositoryDefinitionFile ############################################################ function New-RepositoryDefinitionFile { <# .SYNOPSIS Speichert Repository Konfiguration. #> param( [Parameter(Mandatory)] [PSCustomObject]$Configuration, [string]$Path ) if([string]::IsNullOrWhiteSpace($Path)) { $Path = Join-Path ` -Path (Get-ProjectPaths).Config ` -ChildPath "Repositories" } if(!(Test-Path $Path)) { New-Item ` -Path $Path ` -ItemType Directory ` -Force | Out-Null } $SafeName = $Configuration.Name -replace '[^A-Za-z0-9._-]', '_' if([string]::IsNullOrWhiteSpace($SafeName)) { throw "Ungültiger Repositoryname." } $File = Join-Path ` $Path ` ( $SafeName + ".json" ) $Configuration | ConvertTo-Json ` -Depth 5 | Out-File ` -FilePath $File ` -Encoding UTF8 return $File } ############################################################ # Get-RepositoryDefinitions ############################################################ function Get-RepositoryDefinitions { <# .SYNOPSIS Liest Repository Definitionen. #> param( [string]$Path ) if([string]::IsNullOrWhiteSpace($Path)) { $Path = Join-Path ` -Path (Get-ProjectPaths).Config ` -ChildPath "Repositories" } if(!(Test-Path $Path)) { return $null } Get-ChildItem ` -Path $Path ` -Filter "*.json" | ForEach-Object { Get-Content ` -Path $_.FullName ` -Raw ` -Encoding UTF8 | ConvertFrom-Json } } ############################################################ # Test-RepositoryConfiguration ############################################################ function Test-RepositoryConfiguration { <# .SYNOPSIS Prüft Repository Konfiguration. #> param( [Parameter(Mandatory)] [PSCustomObject]$Configuration ) [PSCustomObject]@{ Name = $Configuration.Name PathValid = Test-RepositoryPath ` -Path $Configuration.Path RetentionValid = ( $Configuration.RetentionDays -gt 0 ) Status = "Checked" } } ############################################################ # Get-RepositoryPerformanceSettings ############################################################ function Get-RepositoryPerformanceSettings { <# .SYNOPSIS Repository Performance Optionen. #> [PSCustomObject]@{ ConcurrentTasks = 4 StorageOptimization = "LocalTarget" Compression = "Optimal" Deduplication = $true } } #Beginn PART 5 ############################################################ # Test-ImmutablePreparation ############################################################ function Test-ImmutablePreparation { <# .SYNOPSIS Prüft Voraussetzungen für Immutable Storage. Hinweis: Die eigentliche Linux Hardened Repository Konfiguration erfolgt außerhalb dieses Moduls. #> param( [Parameter(Mandatory)] [string]$Path ) [PSCustomObject]@{ Path = $Path DirectoryExists = Test-RepositoryPath ` -Path $Path NTFSProtection = $false ImmutableReady = $false Note = "Externe Storage-Konfiguration erforderlich." } } ############################################################ # Remove-RepositoryTemporaryFiles ############################################################ function Remove-RepositoryTemporaryFiles { <# .SYNOPSIS Entfernt temporäre Dateien. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return $false } Get-ChildItem ` -Path $Path ` -Filter "*.tmp" ` -Recurse ` -ErrorAction SilentlyContinue | Remove-Item ` -Force ` -ErrorAction SilentlyContinue return $true } ############################################################ # Get-RepositoryStorageAnalysis ############################################################ function Get-RepositoryStorageAnalysis { <# .SYNOPSIS Analysiert Storage. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return $null } $Files = @( Get-ChildItem ` -Path $Path ` -File ` -Recurse ` -ErrorAction SilentlyContinue ) [PSCustomObject]@{ Path = $Path FileCount = $Files.Count SizeGB = [math]::Round( ( $Files | Measure-Object ` -Property Length ` -Sum ).Sum / 1GB, 2 ) Time = Get-Date } } ############################################################ # Repair-RepositoryPermissions ############################################################ function Repair-RepositoryPermissions { <# .SYNOPSIS Repariert Berechtigungen. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Account ) Set-RepositoryPermission ` -Path $Path ` -Account $Account return $true } #Beginn PART 6 ############################################################ # Deploy-RepositoryConfiguration ############################################################ function Deploy-RepositoryConfiguration { <# .SYNOPSIS Bereitet Repository Konfiguration vor. #> param( [Parameter(Mandatory)] [string]$Name, [Parameter(Mandatory)] [string]$Path, [ValidateRange(1, 9999)] [int]$RetentionDays = 14, [string]$Account ) $Configuration = Get-RepositoryConfiguration ` -Name $Name ` -Path $Path ` -RetentionDays $RetentionDays Initialize-Repository ` -Path $Path ` -Account $Account if($null -eq (Get-Command Get-VBRBackupRepository -ErrorAction SilentlyContinue)) { throw "Veeam PowerShell ist nicht verfügbar. Repository kann nicht registriert werden." } $Repository = Get-VBRBackupRepository ` -Name $Name ` -ErrorAction SilentlyContinue | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 if($null -eq $Repository) { $Repository = Add-VBRBackupRepository ` -Name $Name ` -Folder $Path ` -Type WinLocal ` -Description "Bereitgestellt durch Veeam Enterprise Installer" ` -ErrorAction Stop } New-RepositoryDefinitionFile ` -Configuration $Configuration $Configuration | Add-Member ` -MemberType NoteProperty ` -Name VeeamRepository ` -Value $Repository ` -Force return $Configuration } ############################################################ # Test-CompleteRepositoryConfiguration ############################################################ function Test-CompleteRepositoryConfiguration { <# .SYNOPSIS Vollständige Repository Prüfung. #> param( [Parameter(Mandatory)] [string]$Path ) [PSCustomObject]@{ Health = Test-RepositoryHealth ` -Path $Path Storage = Get-RepositoryStorageAnalysis ` -Path $Path Access = Test-RepositoryAccess ` -Path $Path Performance = Get-RepositoryPerformanceSettings Time = Get-Date } } ############################################################ # Get-RepositoryModuleInformation ############################################################ function Get-RepositoryModuleInformation { <# .SYNOPSIS Liefert Modulinformationen. #> [PSCustomObject]@{ Module = "Repository.psm1" Version = $Script:RepositoryVersion Loaded = $true } } #Beginn PART 7 ############################################################ # Export Module Functions ############################################################ Export-ModuleMember ` -Function ` Test-RepositoryPath, Resolve-RepositoryPath, New-RepositoryFolder, Get-RepositoryInformation, Test-RepositoryFreeSpace, Get-RepositoryConfiguration, Test-RepositoryAccess, Set-RepositoryPermission, Test-RepositoryHealth, Initialize-Repository, Get-RepositoryReport, New-RepositoryDefinitionFile, Get-RepositoryDefinitions, Test-RepositoryConfiguration, Get-RepositoryPerformanceSettings, Test-ImmutablePreparation, Remove-RepositoryTemporaryFiles, Get-RepositoryStorageAnalysis, Repair-RepositoryPermissions, Deploy-RepositoryConfiguration, Test-CompleteRepositoryConfiguration, Get-RepositoryModuleInformation ############################################################ # Ende Repository.psm1 ############################################################