From c8830d0a4060533e2175e5be7922f5e73e99f167 Mon Sep 17 00:00:00 2001 From: KammM Date: Thu, 21 Nov 2024 17:27:17 +0100 Subject: [PATCH] Backup-Database: Script functions extended --- .../Backup-MariaDatabase.ps1 | 95 ++++++++++----- .../Backup-SQLDatabase/Backup-SQLDatabase.ps1 | 115 ++++++++++++------ 2 files changed, 142 insertions(+), 68 deletions(-) diff --git a/current/Backup-MariaDatabase/Backup-MariaDatabase.ps1 b/current/Backup-MariaDatabase/Backup-MariaDatabase.ps1 index 2cc6b68..fd2f0cf 100644 --- a/current/Backup-MariaDatabase/Backup-MariaDatabase.ps1 +++ b/current/Backup-MariaDatabase/Backup-MariaDatabase.ps1 @@ -1,13 +1,19 @@ cls +#MK / 15.11.2024 +#Next task: write test to final path +#Get dbs by SHOW DATABASES; -import-module SQLPS +import-module SQLPS -Scope Local -Force +Install-Module MySQLCmdlets Set-Variable Servername -Value "localhost" -Scope global Set-Variable Username -Value "root" -Scope global Set-Variable Passwort -Value "123456789dd!" -Scope global Set-Variable SicherungsPfad -Value "E:\Sicherung\MariaDB" -Scope global -Set-Variable SicherungsPfadTemp -Value $null -Scope global Set-Variable holedatum -Value (get-date -Format 'yyyyMMdd_HHmm') -Scope global +Set-Variable SicherungsPfadTemp -Value "$($SicherungsPfad)\$holedatum" -Scope global +Set-Variable SicherungsPfadTempExists -Value $False -Scope global +Set-Variable SicherungsPfadTempIsClean -Value $False -Scope global Set-Variable SicherungsPfadFinal -Value "\\dd-sto01\f$\DD-STO01-A3\Backup\Computer\DD-VMP02-DB01\MariaDB" -Scope global Set-Variable Datenbanken -Value @("nextcloud") -Scope global @@ -22,37 +28,64 @@ Function Backup-SQLDatabases { [string[]]$instanzen, [parameter(mandatory=$true)] [validatepattern("[A-Z]")] - [validatecount(1,99)] #[validatecount(1, 9)] + [validatecount(1,99)] [string]$dbs ) - - $pfadzumbackup = $SicherungsPfad - - if(!(test-path -Path "$($pfadzumbackup)\$holedatum")){ + + if( -not (test-path -Path $SicherungsPfadTemp)){ try { - new-item -path $pfadzumbackup -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null - Set-Variable SicherungsPfadTemp -Value "$($pfadzumbackup)\$holedatum" -Scope global + new-item -path $SicherungsPfad -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null + $SicherungsPfadTempExists = $True + + } catch { + write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow + $SicherungsPfadTempExists = $False } - catch { - write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow - } - } - else{ - write-host "Backup-Ordner existiert bereits" -ForegroundColor Yellow - Set-Variable SicherungsPfadTemp -Value "$($pfadzumbackup)\$holedatum" -Scope global - } - - write-host "Backup von der Datenbank $dbs wird erstellt" -ForegroundColor Yellow - $arguments = "--host=$($Servername) --port=3306 --user=$($Username) --password=$($Passwort) --databases $($dbs) --result-file $($SicherungsPfadTemp)\$($db).sql" - write-host "Parameter $($arguments)" -ForegroundColor Yellow - Start-Process -FilePath "mysqldump.exe" -ArgumentList $arguments -Wait + } else{ + write-host "Backup-Ordner existiert bereits" -ForegroundColor Yellow + $SicherungsPfadTempExists = $True + } + + IF ($SicherungsPfadTempExists = $True) { + + write-host "Backup von der Datenbank $dbs wird erstellt" -ForegroundColor Yellow + $arguments = "--host=$($Servername) --port=3306 --user=$($Username) --password=$($Passwort) --databases $($dbs) --result-file $($SicherungsPfadTemp)\$($db).sql" + write-host "Parameter $($arguments)" -ForegroundColor Yellow + Start-Process -FilePath "mysqldump.exe" -ArgumentList $arguments -Wait + + } } ####################################################################################################################### #Call backup task for every db - #target must be online - if (test-path -Path $SicherungsPfadFinal ) { +#target must be online +if ((test-path -Path $SicherungsPfadFinal) -and (New-Item -Path $SicherungsPfadFinal -Name "AccessTest.tmp" -ItemType "file" -Force -ErrorAction Stop)) { + + Write-Host "Final Path is reachable" + Remove-Item -Path "$SicherungsPfadFinal\AccessTest.tmp" -Force -ErrorAction Continue + + Write-Host "Checking for garbage in: $SicherungsPfad" + $Items = Get-ChildItem -LiteralPath $SicherungsPfad -Recurse:$False -Force -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer } + If ($Items) { + Try { + FOREACH ($Item in $Items) { + Write-Host "Moving '$($Item.FullName)' to '$SicherungsPfadFinal'" + Move-Item -Path $($Item.FullName) -Destination $SicherungsPfadFinal -Force -ErrorAction Stop + } + $SicherungsPfadTempIsClean = $True + + } Catch { + Write-Host "Cannot clean up temp dir!" + Write-Host $Error[0].ToString() + $SicherungsPfadTempIsClean = $False + } + + } Else { + $SicherungsPfadTempIsClean = $True + } + + If ($SicherungsPfadTempIsClean -eq $True) { FOREACH ($db in $Datenbanken) { @@ -67,12 +100,18 @@ Function Backup-SQLDatabases { } ####################################################################################################################### #move dir incl. backup files to sto01 - write-host $SicherungsPfadTemp - Move-Item -Path $SicherungsPfadTemp -Destination $SicherungsPfadFinal -Force -} + Try { + write-host $SicherungsPfadTemp + Move-Item -Path $SicherungsPfadTemp -Destination $SicherungsPfadFinal -Force -ErrorAction Stop + } Catch { + Write-Host "Error moving files!" + Write-Host $Error[0].ToSting() + } + + } -Else { +} Else { Write-Host "Target is offline!" diff --git a/current/Backup-SQLDatabase/Backup-SQLDatabase.ps1 b/current/Backup-SQLDatabase/Backup-SQLDatabase.ps1 index d7c63d2..c256036 100644 --- a/current/Backup-SQLDatabase/Backup-SQLDatabase.ps1 +++ b/current/Backup-SQLDatabase/Backup-SQLDatabase.ps1 @@ -1,11 +1,14 @@ cls +#MK / 15.11.2024 -import-module SQLPS +import-module SQLPS -Scope Local -Force -Set-Variable Servername -Value "DD-VMP02-DB01" -Scope global +Set-Variable Servername -Value "DD-VMP02-DB01\ZEIT" -Scope global Set-Variable SicherungsPfad -Value "E:\Sicherung\SQL" -Scope global -Set-Variable SicherungsPfadTemp -Value $null -Scope global Set-Variable holedatum -Value (get-date -Format 'yyyyMMdd_HHmm') -Scope global +Set-Variable SicherungsPfadTemp -Value "$($SicherungsPfad)\$holedatum" -Scope global +Set-Variable SicherungsPfadTempExists -Value $False -Scope global +Set-Variable SicherungsPfadTempIsClean -Value $False -Scope global Set-Variable SicherungsPfadFinal -Value "\\dd-sto01\f$\DD-STO01-A3\Backup\Computer\DD-VMP02-DB01\SQL\" -Scope global $dbs = Get-SqlDatabase -ServerInstance $Servername @@ -19,56 +22,83 @@ Function Backup-SQLDatabases { [string[]]$instanzen, [parameter(mandatory=$true)] [validatepattern("[A-Z]")] - [validatecount(1,99)] #[validatecount(1, 9)] + [validatecount(1,99)] [string]$dbs ) - $pfadzumbackup = $SicherungsPfad - - if(!(test-path -Path "$($pfadzumbackup)\$holedatum")){ + if( -not (test-path -Path $SicherungsPfadTemp)){ try { - new-item -path $pfadzumbackup -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null - Set-Variable SicherungsPfadTemp -Value "$($pfadzumbackup)\$holedatum" -Scope global + new-item -path $SicherungsPfad -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null + $SicherungsPfadTempExists = $True + + } catch { + write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow + $SicherungsPfadTempExists = $False } - catch { - write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow - } - } - else{ + } else{ write-host "Backup-Ordner existiert bereits" -ForegroundColor Yellow + $SicherungsPfadTempExists = $True } + + IF ($SicherungsPfadTempExists = $True) { - # For-Schleife für mehrere Instanzen - for($i = 0; $instanzen.count -gt $i; $i++){ - write-host "Backup von der Datenbank $dbs auf der Instanz $($instanzen[$i]) wird erstellt" -ForegroundColor Yellow + # For-Schleife für mehrere Instanzen + for($i = 0; $instanzen.count -gt $i; $i++){ + write-host "Backup von der Datenbank $dbs auf der Instanz $($instanzen[$i]) wird erstellt" -ForegroundColor Yellow - # Backup für remote Instanzen - if($instanzen[$i] -ne $Servername -and !$instanzen[$i] -eq $false){ + # Backup für remote Instanzen + if($instanzen[$i] -ne $Servername -and !$instanzen[$i] -eq $false){ - Write-host $args[1].Substring(1, 15) - Write-Host $args[0] - Invoke-Command -ComputerName $instanzen[$i].Substring(1, 15) -ScriptBlock { + Write-host $args[1].Substring(1, 15) + Write-Host $args[0] + Invoke-Command -ComputerName $instanzen[$i].Substring(1, 15) -ScriptBlock { - Backup-SqlDatabase -ServerInstance $args[1].Substring(1, 15) -Database $args[0] -BackupFile "$($args[2])\$($args[0]).bak" + Backup-SqlDatabase -ServerInstance $args[1].Substring(1, 15) -Database $args[0] -BackupFile "$($args[2])\$($args[0]).bak" - } -ArgumentList $dbs, $instanzen[$i], $pfadzumbackup, $holedatum - } - # Backup für eine lokale Instanz - elseif($instanzen[$i] -eq $Servername -and !$instanzen[$i] -eq $false){ - Backup-SqlDatabase -ServerInstance $instanzen[$i] -Database $dbs -BackupFile "$pfadzumbackup\$holedatum\$($dbs).bak" + } -ArgumentList $dbs, $instanzen[$i], $pfadzumbackup, $holedatum + } + # Backup für eine lokale Instanz + elseif($instanzen[$i] -eq $Servername -and !$instanzen[$i] -eq $false){ + Backup-SqlDatabase -ServerInstance $instanzen[$i] -Database $dbs -BackupFile "$SicherungsPfadTemp\$($dbs).bak" - } - else{ - write-host "$($instanzen[$i]) existiert nicht" - } - } # ende von for-schleife + } else{ + write-host "$($instanzen[$i]) existiert nicht" + } + } # ende von for-schleife + + } } ####################################################################################################################### #Call backup task for every db - #target must be online - if (test-path -Path $SicherungsPfadFinal ) { +#target must be online +if ((test-path -Path $SicherungsPfadFinal) -and (New-Item -Path $SicherungsPfadFinal -Name "AccessTest.tmp" -ItemType "file" -Force -ErrorAction Stop)) { + + Write-Host "Final Path is reachable" + Remove-Item -Path "$SicherungsPfadFinal\AccessTest.tmp" -Force -ErrorAction Continue + + Write-Host "Checking for garbage in: $SicherungsPfad" + $Items = Get-ChildItem -LiteralPath $SicherungsPfad -Recurse:$False -Force -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer } + If ($Items) { + Try { + FOREACH ($Item in $Items) { + Write-Host "Moving '$($Item.FullName)' to '$SicherungsPfadFinal'" + Move-Item -Path $($Item.FullName) -Destination $SicherungsPfadFinal -Force -ErrorAction Stop + } + $SicherungsPfadTempIsClean = $True + + } Catch { + Write-Host "Cannot clean up temp dir!" + Write-Host $Error[0].ToString() + $SicherungsPfadTempIsClean = $False + } + + } Else { + $SicherungsPfadTempIsClean = $True + } + + If ($SicherungsPfadTempIsClean -eq $True) { FOREACH ($db in $dbs) { @@ -83,12 +113,17 @@ Function Backup-SQLDatabases { } ####################################################################################################################### #move dir incl. backup files to sto01 - write-host $SicherungsPfadTemp - Move-Item -Path $SicherungsPfadTemp -Destination $SicherungsPfadFinal -Force - -} + + Try { + write-host $SicherungsPfadTemp + Move-Item -Path $SicherungsPfadTemp -Destination $SicherungsPfadFinal -Force -ErrorAction Stop + } Catch { + Write-Host "Error moving files!" + Write-Host $Error[0].ToSting() + } + } -Else { +} Else { Write-Host "Target is offline!"