Prune WAL and old full backups after each successful basebackup

Archive was growing unbounded (~97 GB / 6k segments). Now runs
pg_archivecleanup against the newest .backup marker and deletes
full_* directories beyond FULL_RETENTION=5.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-19 10:05:35 +02:00
co-authored by Claude Opus 4.7
parent 7808dc8288
commit 182ae1407b
+16 -1
View File
@@ -41,11 +41,26 @@ services:
- |
mkdir -p /backup/wal /backup/full
chown 999:999 /backup/wal
FULL_RETENTION=5
while true; do
STAMP=$$(date +%Y-%m-%d_%H-%M-%S)
DEST="/backup/full/full_$$STAMP"
echo "Starting full backup: full_$$STAMP"
pg_basebackup -h postgres-db -U "$$POSTGRES_USER" -D "/backup/full/full_$$STAMP" -Fp -Xs -P
if pg_basebackup -h postgres-db -U "$$POSTGRES_USER" -D "$$DEST" -Fp -Xs -P; then
echo "Full backup complete: full_$$STAMP"
LATEST_BACKUP_MARKER=$$(ls /backup/wal/*.backup 2>/dev/null | sort | tail -n1)
if [ -n "$$LATEST_BACKUP_MARKER" ]; then
echo "Cleaning WAL older than $$(basename "$$LATEST_BACKUP_MARKER")"
pg_archivecleanup /backup/wal "$$(basename "$$LATEST_BACKUP_MARKER")"
fi
ls -1dt /backup/full/full_* | tail -n +$$((FULL_RETENTION + 1)) | while read -r old; do
echo "Removing old full backup: $$old"
rm -rf "$$old"
done
else
echo "Full backup FAILED for full_$$STAMP - skipping cleanup"
rm -rf "$$DEST"
fi
sleep 604800
done