Files
lexware-db-mirror/push-loop.ps1
T
2026-06-08 13:43:46 +02:00

46 lines
1.7 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Lexware DB Sync Loop-Wrapper
# Laeuft als dauerhafter Prozess (Scheduled Task bei Systemstart).
# Startet push-dump.ps1 und wartet danach immer 120 Sekunden,
# unabhaengig davon wie lange der Lauf selbst gedauert hat.
$script = "C:\lexware-db-connect\push-dump.ps1"
$logFile = "C:\Users\Administrator\Desktop\LexWare-DB-Mirror.log"
$utf8NoBom = [System.Text.UTF8Encoding]::new($false)
$waitAfterRun = 120 # Sekunden Pause nach jedem Lauf
function Write-LoopLog([string]$msg) {
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$entry = "[$ts] [LOOP] $msg`n"
[System.IO.File]::AppendAllText($logFile, $entry, $utf8NoBom)
}
Write-LoopLog "Loop gestartet (Interval: ${waitAfterRun}s nach Lauf-Ende)"
while ($true) {
# Fallback: nur dumpen wenn Logical Replication nicht aktiv ist
$replOk = $false
try {
$sshKey = "C:\lexware-db-connect\ssh\id_rsa"
$result = & ssh -i $sshKey -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@192.168.115.113 `
"sudo -u postgres psql -d f1 -tAc 'SELECT COUNT(*) FROM pg_stat_subscription WHERE received_lsn IS NOT NULL;' 2>/dev/null"
$replOk = ([int]($result.Trim()) -ge 4)
} catch {}
if ($replOk) {
Start-Sleep -Seconds $waitAfterRun
continue
}
Write-LoopLog "Replikation inaktiv starte Dump-Fallback"
$sw = [System.Diagnostics.Stopwatch]::StartNew()
& powershell.exe -NonInteractive -ExecutionPolicy Bypass -File $script
$sw.Stop()
$elapsed = [int]$sw.Elapsed.TotalSeconds
$wait = [Math]::Max(10, $waitAfterRun - $elapsed)
Write-LoopLog "Lauf beendet nach ${elapsed}s - naechster Start in ${wait}s"
Start-Sleep -Seconds $wait
}