Files
dyolink/infrastructure/scripts/backup-prod-db.sh
rameen 0c49dddc4c docs: schedule production database dumps and copy them to the Mac
Three dumps a day stay on the server for 3 days. A Mac script copies every dump file after one password prompt.
2026-10-06 18:44:03 +03:30

34 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Scheduled production dump. Run on the Linux host as the dyolink user (cron).
# Does not stop the site. Keeps nudentic_YYYYMMDD_HHMMSS.dump for 3 days.
# Does not delete nudentic_before_restore_*.dump.
set -euo pipefail
INFRA="${PROD_INFRA_DIR:-/opt/dyolink/infrastructure}"
BACKUP_DIR="$INFRA/database/backups"
CONTAINER="${POSTGRES_CONTAINER:-dyolink_db_prod}"
STAMP="$(date -u +%Y%m%d_%H%M%S)"
FILE="$BACKUP_DIR/nudentic_${STAMP}.dump"
mkdir -p "$BACKUP_DIR"
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Starting dump nudentic_${STAMP}.dump"
docker exec -e BACKUP_STAMP="$STAMP" "$CONTAINER" \
sh -c 'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fc -f "/backups/nudentic_${BACKUP_STAMP}.dump"'
if [ ! -s "$FILE" ]; then
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Backup failed: $FILE is missing or empty" >&2
exit 1
fi
BYTES=$(wc -c < "$FILE" | tr -d ' ')
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Wrote $FILE ($BYTES bytes)"
# 8 digits after nudentic_ so nudentic_before_restore_*.dump is left alone.
find "$BACKUP_DIR" -maxdepth 1 -type f \
-name 'nudentic_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_*.dump' \
-mtime +3 -delete
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Removed scheduled dumps older than 3 days"