Backup-Database: Script functions extended
This commit is contained in:
parent
4d95dec651
commit
c8830d0a40
@ -1,13 +1,19 @@
|
|||||||
cls
|
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 Servername -Value "localhost" -Scope global
|
||||||
Set-Variable Username -Value "root" -Scope global
|
Set-Variable Username -Value "root" -Scope global
|
||||||
Set-Variable Passwort -Value "123456789dd!" -Scope global
|
Set-Variable Passwort -Value "123456789dd!" -Scope global
|
||||||
Set-Variable SicherungsPfad -Value "E:\Sicherung\MariaDB" -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 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 SicherungsPfadFinal -Value "\\dd-sto01\f$\DD-STO01-A3\Backup\Computer\DD-VMP02-DB01\MariaDB" -Scope global
|
||||||
Set-Variable Datenbanken -Value @("nextcloud") -Scope global
|
Set-Variable Datenbanken -Value @("nextcloud") -Scope global
|
||||||
|
|
||||||
@ -22,37 +28,64 @@ Function Backup-SQLDatabases {
|
|||||||
[string[]]$instanzen,
|
[string[]]$instanzen,
|
||||||
[parameter(mandatory=$true)]
|
[parameter(mandatory=$true)]
|
||||||
[validatepattern("[A-Z]")]
|
[validatepattern("[A-Z]")]
|
||||||
[validatecount(1,99)] #[validatecount(1, 9)]
|
[validatecount(1,99)]
|
||||||
[string]$dbs
|
[string]$dbs
|
||||||
)
|
)
|
||||||
|
|
||||||
$pfadzumbackup = $SicherungsPfad
|
if( -not (test-path -Path $SicherungsPfadTemp)){
|
||||||
|
|
||||||
if(!(test-path -Path "$($pfadzumbackup)\$holedatum")){
|
|
||||||
try {
|
try {
|
||||||
new-item -path $pfadzumbackup -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null
|
new-item -path $SicherungsPfad -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null
|
||||||
Set-Variable SicherungsPfadTemp -Value "$($pfadzumbackup)\$holedatum" -Scope global
|
$SicherungsPfadTempExists = $True
|
||||||
|
|
||||||
|
} catch {
|
||||||
|
write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow
|
||||||
|
$SicherungsPfadTempExists = $False
|
||||||
}
|
}
|
||||||
|
|
||||||
catch {
|
} else{
|
||||||
write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
write-host "Backup-Ordner existiert bereits" -ForegroundColor Yellow
|
write-host "Backup-Ordner existiert bereits" -ForegroundColor Yellow
|
||||||
Set-Variable SicherungsPfadTemp -Value "$($pfadzumbackup)\$holedatum" -Scope global
|
$SicherungsPfadTempExists = $True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IF ($SicherungsPfadTempExists = $True) {
|
||||||
|
|
||||||
write-host "Backup von der Datenbank $dbs wird erstellt" -ForegroundColor Yellow
|
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"
|
$arguments = "--host=$($Servername) --port=3306 --user=$($Username) --password=$($Passwort) --databases $($dbs) --result-file $($SicherungsPfadTemp)\$($db).sql"
|
||||||
write-host "Parameter $($arguments)" -ForegroundColor Yellow
|
write-host "Parameter $($arguments)" -ForegroundColor Yellow
|
||||||
Start-Process -FilePath "mysqldump.exe" -ArgumentList $arguments -Wait
|
Start-Process -FilePath "mysqldump.exe" -ArgumentList $arguments -Wait
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#######################################################################################################################
|
#######################################################################################################################
|
||||||
#Call backup task for every db
|
#Call backup task for every db
|
||||||
|
|
||||||
#target must be online
|
#target must be online
|
||||||
if (test-path -Path $SicherungsPfadFinal ) {
|
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) {
|
FOREACH ($db in $Datenbanken) {
|
||||||
|
|
||||||
@ -67,12 +100,18 @@ Function Backup-SQLDatabases {
|
|||||||
}
|
}
|
||||||
#######################################################################################################################
|
#######################################################################################################################
|
||||||
#move dir incl. backup files to sto01
|
#move dir incl. backup files to sto01
|
||||||
|
|
||||||
|
Try {
|
||||||
write-host $SicherungsPfadTemp
|
write-host $SicherungsPfadTemp
|
||||||
Move-Item -Path $SicherungsPfadTemp -Destination $SicherungsPfadFinal -Force
|
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!"
|
Write-Host "Target is offline!"
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
cls
|
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 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 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
|
Set-Variable SicherungsPfadFinal -Value "\\dd-sto01\f$\DD-STO01-A3\Backup\Computer\DD-VMP02-DB01\SQL\" -Scope global
|
||||||
|
|
||||||
$dbs = Get-SqlDatabase -ServerInstance $Servername
|
$dbs = Get-SqlDatabase -ServerInstance $Servername
|
||||||
@ -19,26 +22,27 @@ Function Backup-SQLDatabases {
|
|||||||
[string[]]$instanzen,
|
[string[]]$instanzen,
|
||||||
[parameter(mandatory=$true)]
|
[parameter(mandatory=$true)]
|
||||||
[validatepattern("[A-Z]")]
|
[validatepattern("[A-Z]")]
|
||||||
[validatecount(1,99)] #[validatecount(1, 9)]
|
[validatecount(1,99)]
|
||||||
[string]$dbs
|
[string]$dbs
|
||||||
)
|
)
|
||||||
|
|
||||||
$pfadzumbackup = $SicherungsPfad
|
if( -not (test-path -Path $SicherungsPfadTemp)){
|
||||||
|
|
||||||
if(!(test-path -Path "$($pfadzumbackup)\$holedatum")){
|
|
||||||
try {
|
try {
|
||||||
new-item -path $pfadzumbackup -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null
|
new-item -path $SicherungsPfad -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null
|
||||||
Set-Variable SicherungsPfadTemp -Value "$($pfadzumbackup)\$holedatum" -Scope global
|
$SicherungsPfadTempExists = $True
|
||||||
|
|
||||||
|
} catch {
|
||||||
|
write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow
|
||||||
|
$SicherungsPfadTempExists = $False
|
||||||
}
|
}
|
||||||
|
|
||||||
catch {
|
} else{
|
||||||
write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
write-host "Backup-Ordner existiert bereits" -ForegroundColor Yellow
|
write-host "Backup-Ordner existiert bereits" -ForegroundColor Yellow
|
||||||
|
$SicherungsPfadTempExists = $True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IF ($SicherungsPfadTempExists = $True) {
|
||||||
|
|
||||||
# For-Schleife für mehrere Instanzen
|
# For-Schleife für mehrere Instanzen
|
||||||
for($i = 0; $instanzen.count -gt $i; $i++){
|
for($i = 0; $instanzen.count -gt $i; $i++){
|
||||||
write-host "Backup von der Datenbank $dbs auf der Instanz $($instanzen[$i]) wird erstellt" -ForegroundColor Yellow
|
write-host "Backup von der Datenbank $dbs auf der Instanz $($instanzen[$i]) wird erstellt" -ForegroundColor Yellow
|
||||||
@ -56,19 +60,45 @@ Function Backup-SQLDatabases {
|
|||||||
}
|
}
|
||||||
# Backup für eine lokale Instanz
|
# Backup für eine lokale Instanz
|
||||||
elseif($instanzen[$i] -eq $Servername -and !$instanzen[$i] -eq $false){
|
elseif($instanzen[$i] -eq $Servername -and !$instanzen[$i] -eq $false){
|
||||||
Backup-SqlDatabase -ServerInstance $instanzen[$i] -Database $dbs -BackupFile "$pfadzumbackup\$holedatum\$($dbs).bak"
|
Backup-SqlDatabase -ServerInstance $instanzen[$i] -Database $dbs -BackupFile "$SicherungsPfadTemp\$($dbs).bak"
|
||||||
|
|
||||||
}
|
} else{
|
||||||
else{
|
|
||||||
write-host "$($instanzen[$i]) existiert nicht"
|
write-host "$($instanzen[$i]) existiert nicht"
|
||||||
}
|
}
|
||||||
} # ende von for-schleife
|
} # ende von for-schleife
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#######################################################################################################################
|
#######################################################################################################################
|
||||||
#Call backup task for every db
|
#Call backup task for every db
|
||||||
|
|
||||||
#target must be online
|
#target must be online
|
||||||
if (test-path -Path $SicherungsPfadFinal ) {
|
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) {
|
FOREACH ($db in $dbs) {
|
||||||
|
|
||||||
@ -83,12 +113,17 @@ Function Backup-SQLDatabases {
|
|||||||
}
|
}
|
||||||
#######################################################################################################################
|
#######################################################################################################################
|
||||||
#move dir incl. backup files to sto01
|
#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!"
|
Write-Host "Target is offline!"
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user