The complete folder structure has been changed or updated
This commit is contained in:
205
dev/Backup-PostgresSQLDatabase/Backup-SQLDatabase.ps1
Normal file
205
dev/Backup-PostgresSQLDatabase/Backup-SQLDatabase.ps1
Normal file
@@ -0,0 +1,205 @@
|
||||
cls
|
||||
#MK / 15.11.2024
|
||||
|
||||
import-module SQLPS -Scope Local -Force
|
||||
|
||||
Set-Variable Servername -Value "DD-VMP02-DB01\ZEIT" -Scope global
|
||||
Set-Variable SicherungsPfad -Value "E:\Sicherung\SQL" -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
|
||||
#######################################################################################################################
|
||||
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) {
|
||||
|
||||
# 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 "$SicherungsPfadTemp\$($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) -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) {
|
||||
|
||||
[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!"
|
||||
|
||||
}
|
||||
|
||||
#################################################################
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sichert alle PostgreSQL-Datenbanken per pg_dump.
|
||||
|
||||
.DESCRIPTION
|
||||
Dieses Skript liest alle Datenbanken vom Server aus und erstellt
|
||||
für jede ein komprimiertes SQL-Dump-File mit Zeitstempel im Dateinamen.
|
||||
|
||||
.PARAMETER Host
|
||||
Hostname oder IP-Adresse des PostgreSQL-Servers.
|
||||
|
||||
.PARAMETER Port
|
||||
Port, auf dem PostgreSQL-Verbindungen akzeptiert (Standard: 5432).
|
||||
|
||||
.PARAMETER User
|
||||
Benutzername für die Authentifizierung.
|
||||
|
||||
.PARAMETER Password
|
||||
Passwort für den Benutzer (wird in Umgebungsvariable PGPASSWORD abgelegt).
|
||||
|
||||
.PARAMETER BackupPath
|
||||
Verzeichnis, in dem die Backup-Dateien abgelegt werden.
|
||||
|
||||
.EXAMPLE
|
||||
.\Backup-Postgres.ps1 -Host "db-server" -Port 5432 `
|
||||
-User "backupuser" -Password "geheimesPasswort" `
|
||||
-BackupPath "C:\Backups\Postgres"
|
||||
#>
|
||||
|
||||
param (
|
||||
[Parameter(Mandatory = $true)] [string]$Host,
|
||||
[Parameter(Mandatory = $false)] [int] $Port = 5432,
|
||||
[Parameter(Mandatory = $true)] [string]$User,
|
||||
[Parameter(Mandatory = $true)] [string]$Password,
|
||||
[Parameter(Mandatory = $true)] [string]$BackupPath
|
||||
)
|
||||
|
||||
# Datum und Uhrzeit für Dateinamen
|
||||
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
|
||||
|
||||
# Umgebungsvariable für pg_dump-Passwort
|
||||
$env:PGPASSWORD = $Password
|
||||
|
||||
# Backup-Verzeichnis anlegen, falls nicht vorhanden
|
||||
if (!(Test-Path -Path $BackupPath)) {
|
||||
New-Item -ItemType Directory -Path $BackupPath | Out-Null
|
||||
}
|
||||
|
||||
# Liste aller Datenbanken abrufen (ohne Template-DBs)
|
||||
$databases = & psql -h $Host -p $Port -U $User -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;" `
|
||||
| ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
|
||||
|
||||
foreach ($db in $databases) {
|
||||
Write-Host "Sichere Datenbank '$db'..." -ForegroundColor Cyan
|
||||
|
||||
$dumpFile = Join-Path $BackupPath "$($db)_$($timestamp).sql.gz"
|
||||
|
||||
# pg_dump mit gzip komprimieren
|
||||
& pg_dump -h $Host -p $Port -U $User -F p -d $db `
|
||||
| gzip > $dumpFile
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host " Backup erfolgreich: $dumpFile" -ForegroundColor Green
|
||||
}
|
||||
else {
|
||||
Write-Host " Fehler bei Backup von $db" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
# Passwort aus Umgebungsvariable entfernen
|
||||
Remove-Item Env:\PGPASSWORD
|
||||
Write-Host "Alle Backups abgeschlossen." -ForegroundColor Yellow
|
||||
|
||||
45
dev/Backup-PostgresSQLDatabase/Backup-SQLDatabase_Caller.cmd
Normal file
45
dev/Backup-PostgresSQLDatabase/Backup-SQLDatabase_Caller.cmd
Normal file
@@ -0,0 +1,45 @@
|
||||
@ECHO OFF
|
||||
TITLE DIGITAL DATA - Run all files in directory with current permissions
|
||||
ECHO -
|
||||
ECHO Batch Script
|
||||
ECHO Run all files in directory with current permissions
|
||||
ECHO -
|
||||
ECHO Digital Data
|
||||
ECHO Ludwig-Rinn-Strasse 16
|
||||
ECHO 35452 Heuchelheim
|
||||
ECHO Tel.: 0641 / 202360
|
||||
ECHO E-Mail: info@didalog.de
|
||||
ECHO -
|
||||
ECHO Version 1.0.0.0
|
||||
ECHO Date: 13.08.2015
|
||||
ECHO -
|
||||
ECHO Program Startup %date% at %time:~0,8% oclock, on %computername%.
|
||||
|
||||
REM --------------------------------------------------------------
|
||||
REM ------------------------set variables-------------------------
|
||||
REM --------------------------------------------------------------
|
||||
|
||||
setlocal enableextensions
|
||||
|
||||
SET DIRECTORY="%cd%"
|
||||
SET FILEEXTENSION=*.ps1
|
||||
SET COUNT=0
|
||||
|
||||
REM --------------------------------------------------------------
|
||||
REM ---------------------Program 1 / Script 1---------------------
|
||||
REM --------------------------------------------------------------
|
||||
|
||||
FOR /F "tokens=*" %%f in ('dir /S /b %FILEEXTENSION%') do (ECHO %%f && set /a count+=1)
|
||||
ECHO -
|
||||
ECHO Found %count% File(s) with File Extension %FILEEXTENSION% in Directory:
|
||||
ECHO %DIRECTORY%.
|
||||
|
||||
ECHO -
|
||||
ECHO Running this/them now, with your permissions!
|
||||
FOR /F "tokens=*" %%f in ('dir /S /b %FILEEXTENSION%') do (PowerShell.exe -Command "& {Start-Process PowerShell.exe -WindowStyle hidden '-ExecutionPolicy Bypass -File "%%f"'}")
|
||||
|
||||
ECHO -
|
||||
ECHO This Window will close in:
|
||||
#timeout /T 10
|
||||
endlocal
|
||||
exit
|
||||
Binary file not shown.
Reference in New Issue
Block a user