8
0
2024-01-24 16:42:38 +01:00

95 lines
4.2 KiB
PowerShell

cls
import-module SQLPS
Set-Variable Servername -Value "DD-VMP02-DB01" -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 SicherungsPfadFinal -Value "\\dd-sto01\f$\DD-STO01-A3\Backup\Computer\DD-VMP02-DB01\SQL\" -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)] #[validatecount(1, 9)]
[string]$dbs
)
$pfadzumbackup = $SicherungsPfad
if(!(test-path -Path "$($pfadzumbackup)\$holedatum")){
try {
new-item -path $pfadzumbackup -Name $holedatum -ItemType Directory -ErrorAction Stop | Out-Null
Set-Variable SicherungsPfadTemp -Value "$($pfadzumbackup)\$holedatum" -Scope global
}
catch {
write-host "Ordner für Backup konnte nicht erstellt werden" -ForegroundColor Yellow
}
}
else{
write-host "Backup-Ordner existiert bereits" -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){
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"
} -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"
}
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 ) {
FOREACH ($db in $dbs) {
[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
write-host $SicherungsPfadTemp
Move-Item -Path $SicherungsPfadTemp -Destination $SicherungsPfadFinal -Force
}
Else {
Write-Host "Target is offline!"
}