# Engineering Review — MySQL Backup Cadence ## Current state - **MySQL runs as a Docker container** on `art0` (host `104.190.60.129`), port `:3306`. - **Data volume:** `fetcherpay_mysql_data` → `/var/lib/docker/volumes/fetcherpay_mysql_data/_data`. - **Database:** `hermes_oauth` (~0.5 MB total; tables are tiny right now). - **Binary logging:** enabled (`binlog_format=ROW`), current logs are small and only cover since the last DB re-init. - **Existing backup script:** `/home/garfield/backup-docker-data.sh` backs up Postgres, Redis, and Gitea volumes to `/opt/fetcherpay-backups/`, but **does not back up MySQL**. - **Last useful SQL backup:** none recent; only an old `email-data-backup-20260423-195421.tar.gz` and stale volume tarballs from Sep 2025. - **Disk:** `/` has 752 GB free, so local retention is not a constraint. ## What happened yesterday The entire `hermes_oauth` schema was recreated around 17:30 UTC. Because there is no SQL dump backup, the only recovery path was manual account recreation. This is exactly the scenario a backup policy is meant to prevent. ## Options evaluated ### Option A — Logical dumps with `mysqldump` Use a cron job to run `docker exec mysql mysqldump ...` and write compressed `.sql.gz` files to `/home/garfield/backups/mysql/`. **Pros** - Simple, portable, human-readable/editable. - Easy per-database, per-table restore. - Can capture a consistent snapshot with `--single-transaction`. - Tiny DB means dumps complete in seconds. **Cons** - For very large databases, restore is slower than physical backups; not a concern today. - Does not preserve binlog position unless you add `--master-data=2`. ### Option B — Physical volume backup Extend `backup-docker-data.sh` to tar the MySQL Docker volume while the container is stopped (or use `mariabackup` for hot physical backup). **Pros** - Fast restore on the same host/OS/MySQL version. **Cons** - Stopping MySQL means downtime; hot physical backup tools add complexity. - Harder to do partial restores (e.g., restore only `customers`). - Not portable across MySQL versions. ### Option C — Point-in-time recovery (PITR) via binlogs Keep logical dumps plus retain binlogs. Restores use the latest dump + replay binlogs to a specific position/time. **Pros** - Can recover to just before a bad deletion. - Ideal for the "delete one account" scenario. **Cons** - Adds operational complexity (must manage binlog retention and expiration). - Requires dump to capture binlog coordinates. ## Recommendation **Adopt Option A now, with Option C as a fast follow.** For the current data size and risk profile, a daily logical dump is the best cost/benefit. It protects against the two failures we just saw: 1. Accidental full DB reset/re-init. 2. Accidental row/table deletion. Once the dump is in place, extend it to capture binlog coordinates and configure binlog expiration so we can do PITR. ## Proposed cadence and retention | Backup type | Cadence | Retention | Notes | |---|---|---|---| | Full logical dump (`hermes_oauth`) | Daily at 03:00 UTC | 14 days local + copy to off-host | Compressed `.sql.gz`; ~KB–MB in size | | Docker volume tarball | Weekly (Sundays) | 4 weeks | Existing script already does this for other volumes; add MySQL | | Binlog retention | Continuous | 7 days | Enables PITR; set `binlog_expire_logs_seconds=604800` | | Off-host copy | Daily after dump | 30 days | `rsync` or `rclone` to another server/cloud storage | ## Suggested implementation ### 1. Backup script Create `/home/garfield/backup-mysql.sh`: ```bash #!/bin/bash set -euo pipefail BACKUP_DIR="/home/garfield/backups/mysql/$(date +%Y%m%d_%H%M%S)" mkdir -p "$BACKUP_DIR" DUMP_FILE="$BACKUP_DIR/hermes_oauth.sql.gz" docker exec mysql mysqldump \ -uroot -pfetcherpay \ --single-transaction \ --routines --triggers \ --databases hermes_oauth \ | gzip > "$DUMP_FILE" # Keep only the last 14 daily dumps ls -1td /home/garfield/backups/mysql/*/ 2>/dev/null | tail -n +15 | xargs -r rm -rf # Ensure binlogs are retained for 7 days for PITR # (Dynamic setting; re-applied each run in case the container was restarted.) docker exec mysql mysql -uroot -pfetcherpay -e "SET GLOBAL binlog_expire_logs_seconds = 604800;" >/dev/null 2>&1 || true echo "[$(date -Iseconds)] Backup complete: $DUMP_FILE ($(stat -c%s "$DUMP_FILE" | numfmt --to=iec))" ls -lh "$DUMP_FILE" ``` Make it executable: ```bash chmod 700 /home/garfield/backup-mysql.sh ``` ### 2. Cron schedule Add to the `garfield` crontab: ```cron # Daily MySQL logical backup at 03:00 UTC 0 3 * * * /home/garfield/backup-mysql.sh >> /home/garfield/backups/mysql/backup.log 2>&1 ``` ### 3. Verify a backup immediately Run the script once, then restore to a temporary database to validate the dump is usable: ```bash LATEST=$(ls -1td /home/garfield/backups/mysql/*/ | head -1) docker exec -i mysql mysql -uroot -pfetcherpay -e " DROP DATABASE IF EXISTS hermes_oauth_restore; CREATE DATABASE hermes_oauth_restore; " gunzip < "${LATEST}hermes_oauth.sql.gz" | \ sed 's/`hermes_oauth`/`hermes_oauth_restore`/g' | \ docker exec -i mysql mysql -uroot -pfetcherpay docker exec -i mysql mysql -uroot -pfetcherpay -e " SELECT 'prod' AS db, COUNT(*) AS customers FROM hermes_oauth.customers UNION ALL SELECT 'restore', COUNT(*) FROM hermes_oauth_restore.customers; " ``` ### 4. Add MySQL to the existing volume backup script (weekly) Add a line to `/home/garfield/backup-docker-data.sh`: ```bash sudo docker run --rm -v fetcherpay_mysql_data:/source:ro -v $BACKUP_DIR:/backup alpine \ tar czf /backup/mysql_data.tar.gz -C /source . ``` > Note: this script currently uses `sudo`, so this step needs root or passwordless sudo configured in cron. ### 5. Configure binlog expiration for PITR Set dynamically (already included in the backup script): ```sql SET GLOBAL binlog_expire_logs_seconds = 604800; -- 7 days ``` To persist across container recreation, add `--binlog-expire-logs-seconds=604800` to the MySQL `docker run` command or mount a custom `my.cnf`. ## Risks and mitigations | Risk | Mitigation | |---|---| | Backup script not run / cron fails | Check backup file age; alert if older than 25 hours | | Backup stored only on the same host | Add off-host copy (rsync/rclone) as a second step | | Secrets in backup command (root password) | Script is readable only by owner (`chmod 700`) | | MySQL version mismatch on restore | Use the same MySQL 8.0 container consistently | | Restore never tested | Run restore validation into `hermes_oauth_restore` weekly | ## What this would have changed yesterday If daily dumps existed, we could have restored the `customers` table (or the whole `hermes_oauth` database) from the most recent backup instead of manually recreating both accounts. With binlog PITR, we might have recovered right up to the moment of the accidental deletion. --- # Implementation completed — 2026-06-17 ## What was done 1. Created `/home/garfield/backup-mysql.sh` - Dumps `hermes_oauth` with `--single-transaction`. - Compresses to `/home/garfield/backups/mysql/YYYYMMDD_HHMMSS/hermes_oauth.sql.gz`. - Keeps the last 14 daily dumps. - Re-applies `binlog_expire_logs_seconds = 604800` each run. 2. Set script permissions to `700` (owner-only). 3. Added a daily cron job at `03:00 UTC`: ```cron 0 3 * * * /home/garfield/backup-mysql.sh >> /home/garfield/backups/mysql/backup.log 2>&1 ``` 4. Ran the script manually twice to confirm it works. 5. Restored the latest dump into `hermes_oauth_restore` and verified row counts match production: ``` db customers prod 3 restore 3 ``` ## Files created / changed - `/home/garfield/backup-mysql.sh` (new) - `garfield` crontab (added daily backup line) - `/home/garfield/backups/mysql/20260617_093802/hermes_oauth.sql.gz` - `/home/garfield/backups/mysql/20260617_093949/hermes_oauth.sql.gz` ## Still to do 1. Set up an off-host copy of `/home/garfield/backups/mysql/`. 2. Add the MySQL volume tarball to `/home/garfield/backup-docker-data.sh` (needs root/sudo handling). 3. Persist `binlog_expire_logs_seconds` in the MySQL container start command or a mounted `my.cnf` so it survives container recreation. 4. Add a backup freshness alert (e.g., check file age in monitoring).