#!/bin/sh # Nightly backup: database dump + uploaded files (songs, covers, photos). Keeps 14 days. # Copy the backups folder off the server too (another machine, or cloud storage). # # Cron (as the user that runs docker): # 30 2 * * * sh /path/to/server/backup.sh >> $HOME/nk-backups/backup.log 2>&1 set -eu PROJECT="${1:-$HOME/nk-backend/supabase-project}" DEST="${2:-$HOME/nk-backups}" STAMP="$(date +%Y%m%d-%H%M)" mkdir -p "$DEST" cd "$PROJECT" PGPASSWORD="$(grep -E '^POSTGRES_PASSWORD=' .env | cut -d= -f2-)" docker compose exec -T -e PGPASSWORD="$PGPASSWORD" db \ pg_dump -h localhost -U postgres -d postgres -Fc > "$DEST/db-$STAMP.dump" tar -czf "$DEST/storage-$STAMP.tar.gz" -C volumes storage find "$DEST" -name 'db-*.dump' -mtime +14 -delete find "$DEST" -name 'storage-*.tar.gz' -mtime +14 -delete echo "$(date -Is) backup ok: db-$STAMP.dump storage-$STAMP.tar.gz"