Files
goodgo-platform/scripts/backup/pg-backup.sh
Ho Ngoc Hai 775eb7b374 feat(ops): add database backup strategy and log aggregation stack
- Add pg-backup container with daily automated pg_dump (02:00 UTC) and 7-day retention
- Add backup/restore scripts with documented recovery procedure
- Add Loki + Promtail for centralized log aggregation from all Docker containers
- Add Loki as Grafana datasource with correlation ID derived fields
- Add Grafana logs dashboard with volume, error rate, HTTP request, and log viewer panels
- Configure Promtail to parse Pino structured JSON logs with level/context labels
- Enhance LoggerService with string-level formatter and service base field
- Configure 15-day log retention in Loki

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-08 04:04:32 +07:00

43 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
# ── PostgreSQL Automated Backup Script ──
# Runs daily via cron inside the pg-backup container.
# Dumps the database and manages retention.
BACKUP_DIR="${BACKUP_DIR:-/backups}"
RETENTION_DAYS="${RETENTION_DAYS:-7}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/goodgo_${TIMESTAMP}.sql.gz"
echo "[backup] Starting PostgreSQL backup at $(date -Iseconds)"
# Ensure backup directory exists
mkdir -p "${BACKUP_DIR}"
# Run pg_dump with compression
pg_dump \
-h "${PGHOST:-postgres}" \
-p "${PGPORT:-5432}" \
-U "${PGUSER:-goodgo}" \
-d "${PGDATABASE:-goodgo}" \
--no-owner \
--no-privileges \
--format=custom \
--compress=6 \
-f "${BACKUP_FILE}"
FILESIZE=$(du -h "${BACKUP_FILE}" | cut -f1)
echo "[backup] Backup completed: ${BACKUP_FILE} (${FILESIZE})"
# Prune old backups beyond retention period
echo "[backup] Pruning backups older than ${RETENTION_DAYS} days..."
PRUNED=$(find "${BACKUP_DIR}" -name "goodgo_*.sql.gz" -type f -mtime "+${RETENTION_DAYS}" -print -delete | wc -l)
echo "[backup] Pruned ${PRUNED} old backup(s)"
# List current backups
echo "[backup] Current backups:"
ls -lh "${BACKUP_DIR}"/goodgo_*.sql.gz 2>/dev/null || echo " (none)"
echo "[backup] Done at $(date -Iseconds)"