76 lines
2.8 KiB
PowerShell
76 lines
2.8 KiB
PowerShell
param(
|
||
[Parameter(Mandatory=$true)]
|
||
[string]$DumpFile,
|
||
[string]$DockerBin = "C:\Program Files\Docker\Docker\resources\bin",
|
||
[string]$DbContainer = "aufmass_web-db-1",
|
||
[string]$WebContainer = "aufmass_web-web-1",
|
||
[string]$DbUser = "aufmass",
|
||
[string]$DbName = "aufmassweb"
|
||
)
|
||
|
||
Write-Host "=== AufmaßWeb – Datenimport (Docker PostgreSQL) ===" -ForegroundColor Cyan
|
||
Write-Host "Dump: $DumpFile"
|
||
Write-Host "Ziel: Container $DbContainer, DB $DbName"
|
||
Write-Host ""
|
||
|
||
if (-not (Test-Path $DumpFile)) {
|
||
Write-Error "Dump-Datei nicht gefunden: $DumpFile"
|
||
exit 1
|
||
}
|
||
|
||
$dockerd = Join-Path $DockerBin "docker.exe"
|
||
if (-not (Test-Path $dockerd)) {
|
||
Write-Error "Docker nicht gefunden unter: $dockerd"
|
||
exit 1
|
||
}
|
||
|
||
# 1. Prüfen ob Container läuft
|
||
Write-Host "[1/5] Prüfe Docker-Container ..." -NoNewline
|
||
$running = & $dockerd ps --filter "name=$DbContainer" --filter "status=running" --format "{{.Names}}" 2>$null
|
||
if (-not $running) {
|
||
Write-Host " FEHLER" -ForegroundColor Red
|
||
Write-Error "Container $DbContainer läuft nicht. Starte zuerst 'docker compose up -d'"
|
||
exit 1
|
||
}
|
||
Write-Host " OK ($running)" -ForegroundColor Green
|
||
|
||
# 2. Dump in Container kopieren
|
||
Write-Host "[2/5] Kopiere Dump in Container ..." -NoNewline
|
||
$remotePath = "/tmp/$(Split-Path $DumpFile -Leaf)"
|
||
& $dockerd cp $DumpFile "${DbContainer}:${remotePath}"
|
||
Write-Host " OK → $remotePath" -ForegroundColor Green
|
||
|
||
# 3. Datenbank droppen + neu anlegen (verbinde mit 'postgres' DB, nicht mit der Zieldatenbank)
|
||
Write-Host "[3/5] Lösche alte Datenbank ..." -NoNewline
|
||
& $dockerd exec -i $DbContainer psql -U $DbUser -d postgres -c "DROP DATABASE IF EXISTS \"$DbName\";" 2>$null
|
||
Write-Host " OK" -ForegroundColor Green
|
||
|
||
Write-Host "[4/5] Erstelle neue Datenbank ..." -NoNewline
|
||
& $dockerd exec -i $DbContainer psql -U $DbUser -d postgres -c "CREATE DATABASE \"$DbName\";" 2>$null
|
||
Write-Host " OK" -ForegroundColor Green
|
||
|
||
# 4. Restore
|
||
Write-Host "[5/5] Stelle Daten aus Dump wieder her ..." -ForegroundColor Yellow
|
||
& $dockerd exec -i $DbContainer pg_restore -U $DbUser -d $DbName --verbose --exit-on-error $remotePath
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Error "pg_restore fehlgeschlagen (Exit-Code: $LASTEXITCODE)"
|
||
exit 1
|
||
}
|
||
Write-Host "Datenbank-Restore erfolgreich!" -ForegroundColor Green
|
||
|
||
# 5. Web-Container neuladen
|
||
Write-Host "Starte Web-Container neu ..." -NoNewline
|
||
& $dockerd restart $WebContainer 2>$null
|
||
Write-Host " OK" -ForegroundColor Green
|
||
|
||
# Aufräumen
|
||
& $dockerd exec $DbContainer rm -f $remotePath 2>$null
|
||
|
||
$size = (Get-Item $DumpFile).Length / 1MB
|
||
Write-Host ""
|
||
Write-Host "=== Fertig! ===" -ForegroundColor Cyan
|
||
Write-Host "Dump: $DumpFile ({0:N1} MB)" -f $size
|
||
Write-Host "App: http://localhost:5000" -ForegroundColor Green
|
||
Write-Host ""
|
||
Write-Host "Tipp: Du kannst den Dump archivieren oder löschen." -ForegroundColor Gray
|