banco-dados-pralog/backup.ps1

36 lines
1.1 KiB
PowerShell

# Database Backup Script using Docker
# This script reads credentials from chaves.md and runs pg_dump in a Docker container
$chavesPath = ".\chaves.md"
if (-Not (Test-Path $chavesPath)) {
Write-Error "chaves.md not found!"
exit 1
}
# Extract credentials (simplifying for reliable parsing)
$content = Get-Content $chavesPath
$password = ($content | Select-String "GLOBAL_DATABASE_URL=postgresql://postgres:(.*)@pikachu").Matches.Groups[1].Value
$dbHost = "pikachu.cnsiese46wxf.sa-east-1.rds.amazonaws.com"
$user = "postgres"
$databases = @("global", "tenant_example")
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
foreach ($db in $databases) {
$outputFile = "backup_${db}_${timestamp}.sql"
Write-Host "Backing up database: $db to $outputFile..."
# Run pg_dump inside Docker
docker run --rm `
-e PGPASSWORD=$password `
postgres:16-alpine `
pg_dump -h $dbHost -U $user -d $db > $outputFile
if ($LASTEXITCODE -eq 0) {
Write-Host "Backup of $db completed successfully."
} else {
Write-Error "Backup of $db failed!"
}
}