#!/usr/bin/env bash # Nightly backup of the production DB. # Add to crontab: 5 2 * * * /path/to/cash-collection-management-system/scripts/backup.sh >> /var/log/crm_omt_backup.log 2>&1 set -euo pipefail REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)" BACKUP_DIR="${BACKUP_DIR:-$REPO_DIR/backups}" KEEP_DAYS="${KEEP_DAYS:-30}" TS="$(date +%Y%m%d_%H%M%S)" OUT="$BACKUP_DIR/crm_omt_${TS}.sql.gz" mkdir -p "$BACKUP_DIR" docker exec crm_omt_db pg_dump -U postgres -d crm_omt --clean --if-exists \ | gzip -9 > "$OUT" # Prune anything older than KEEP_DAYS days find "$BACKUP_DIR" -name 'crm_omt_*.sql.gz' -type f -mtime "+$KEEP_DAYS" -delete echo "[backup] wrote $OUT ($(du -h "$OUT" | cut -f1))"