Compact PostgreSQL backup utility for a DevOps portfolio: it creates a custom-format pg_dump archive, optionally inspects it with pg_restore --list, and can keep a bounded set of recent local archives.
Project status: compact reference implementation. The project focuses on deterministic local backup, archive verification, and safe retention cleanup. Database access, credentials, storage durability, and operational policy remain deployment responsibilities.
- timestamped custom-format backups under
BACKUP_DIR; - archive inspection with
pg_restore --list; - retention cleanup that keeps the newest archives and removes only matching local backup files;
- clear exit codes and human-readable CLI output;
- optional Telegram notification with the standard library only;
- Docker Compose example with a disposable PostgreSQL service.
Install Python 3.11+ and the PostgreSQL client tools (pg_dump and pg_restore). Then configure the environment:
Copy-Item .env.example .env
$env:DATABASE_URL = "postgresql://user:password@localhost:5432/app"
$env:BACKUP_DIR = ".\\backups"
python .\backup_proof.py backup --verifyKeep the three newest archives after a successful backup (and verification, when --verify is used):
python .\backup_proof.py backup --verify --keep 3The same policy can be configured with RETENTION_COUNT=3. The command keeps files named backup-*.dump, orders them by their UTC timestamped filename, and removes only older regular files in BACKUP_DIR. A value of --keep takes precedence over the environment variable; the minimum is 1.
Verify an existing archive:
python .\backup_proof.py verify .\backups\backup-20260818T120000Z.dumpAdd --notify to either command when TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID are configured. Notifications are best-effort and do not add a required Python dependency.
The Compose file starts a disposable PostgreSQL instance, waits for its healthcheck, and runs one backup-and-verify job:
docker compose up --build --abort-on-container-exit
docker compose down -vThe database and named backup volume are intentionally disposable. For a deployed environment, provide your own DATABASE_URL, durable storage, credentials handling, encryption, and restore drill process.
The utility is intentionally focused on one backup job rather than scheduling, remote object storage, encryption, or restore orchestration. A successful pg_restore --list confirms that the archive can be listed; it does not prove that a full restore is safe. Treat Telegram notifications as best-effort and keep independent copies of important data.