118 lines
5.1 KiB
PowerShell
118 lines
5.1 KiB
PowerShell
cls
|
|
#MK / 15.11.2024
|
|
#Next task: write test to final path
|
|
#Get dbs by SHOW DATABASES;
|
|
|
|
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 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
|
|
|
|
# $dbs = Get-SqlDatabase -ServerInstance $Servername
|
|
#######################################################################################################################
|
|
Function Backup-SQLDatabases {
|
|
|
|
# Parameter definieren
|
|
param(
|
|
[parameter(mandatory=$true)]
|
|
[validatecount(1, 10)]
|
|
[string[]]$instanzen,
|
|
[parameter(mandatory=$true)]
|
|
[validatepattern("[A-Z]")]
|
|
[validatecount(1,99)]
|
|
[string]$dbs
|
|
)
|
|
|
|
if( -not (test-path -Path $SicherungsPfadTemp)){
|
|
try {
|
|
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
|
|
}
|
|
|
|
} 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) -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) {
|
|
|
|
[string]$db = $db
|
|
$db = ($db.replace("[",""))
|
|
$db = ($db.replace("]",""))
|
|
|
|
If ($db -ne "tempdb" -and $db -ne "master" -and $db -ne "model" -and $db -ne "msdb") {
|
|
Write-Host "Sicherung von $db"
|
|
Backup-SQLDatabases -instanzen $Servername -dbs $db
|
|
}
|
|
}
|
|
#######################################################################################################################
|
|
#move dir incl. backup files to sto01
|
|
|
|
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 {
|
|
|
|
Write-Host "Target is offline!"
|
|
|
|
} |