# PostgreSQL Database Backup Script # Backs up the craft2prints database daily $backupDir = "D:\Craft2Prints\database-backups" $pgBin = "C:\Program Files\PostgreSQL\18\bin" $dbName = "craft2prints" $dbUser = "postgres" $dbHost = "localhost" $dbPort = "5432" # Create backup directory if it doesn't exist if (!(Test-Path $backupDir)) { New-Item -ItemType Directory -Path $backupDir | Out-Null } # Create timestamped backup filename $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $backupFile = "$backupDir\craft2prints_$timestamp.sql" # Run pg_dump Write-Host "Backing up database to $backupFile..." $env:PGPASSWORD = "postgres" & "$pgBin\pg_dump.exe" -h $dbHost -p $dbPort -U $dbUser -d $dbName -F p -f $backupFile $exitCode = $LASTEXITCODE if ($exitCode -eq 0) { Write-Host "Backup completed successfully" # Keep only the last 7 backups $backups = Get-ChildItem $backupDir -Filter "craft2prints_*.sql" | Sort-Object CreationTime -Descending if ($backups.Count -gt 7) { $backups | Select-Object -Skip 7 | Remove-Item Write-Host "Cleaned up old backups (kept 7 most recent)" } } else { Write-Host "Backup failed with exit code $exitCode" }