📚 Encyclopedia

Databases · updated 2026-10-13

Nightly MySQL/MariaDB backups with mysqldump and cron

mysqlmariadbmysqldumpcronbackup

A small, dependable backup recipe for a single-server MariaDB database: a shell script run by cron that writes a gzipped mysqldump per night and prunes old ones. Used for the Hunsley Hills Salon hhs database on the Alma 9 server.

The script

#!/bin/bash
set -euo pipefail
DIR="$HOME/db-backups/hhs"
mkdir -p "$DIR"
mysqldump --defaults-extra-file="$HOME/.hhs-backup.cnf" \
  --single-transaction --routines --triggers hhs \
  | gzip > "$DIR/hhs-$(date +%F).sql.gz"
find "$DIR" -name '*.sql.gz' -mtime +14 -delete

Crontab: 15 3 * * * ~/bin/backup-hhs-db.sh (03:15 nightly, 14-day retention). Keep the script in the repo (deploy/backup-hhs-db.sh) and install a copy to ~/bin on the server.

Keep the password off the command line

-p'secret' on the command line shows up in ps output and in shell history, and ends up in the repo if the script is committed. Instead put credentials in an options file only the user can read:

# ~/.hhs-backup.cnf  (chmod 600, server-only, never committed)
[client]
user=...
password=...

and pass --defaults-extra-file= as the first option (mysqldump requires it first).

Notes

Second instance: the run database (2026-10-13)

The same recipe was added for Run's run database after an audit found it had no backup at all (only hhs was in the crontab): ~/bin/backup-run-db.sh (source deploy/backup-run-db.sh in the run repo), cron 20 3 * * *, dumps to ~/db-backups/run/, 14-day retention, credentials in ~/.run-backup.cnf. Restore: gunzip -c run_*.sql.gz | mysql run. Stagger the cron minute per database so dumps do not overlap. Lesson: whenever a new database goes live on the server, add its backup line the same day; "no backup" is easy to miss because nothing breaks until it matters.

Schema baseline and migrations without a runner

Data cleanups: guard with a timestamp

When deleting rows written by a since-fixed client bug, bound the delete by created_at before the fix deployed. Run purged bogus world-level scores stored as level_index = -1, a value that after the fix legitimately meant "world level 1", so an unbounded DELETE ... WHERE level_index = -1 would have wiped real scores too. Take or verify a backup first.

See also

Linked from

Sources: hunsley memory (hhs-cms, hhs-deployment), 2026-06-30; run memory (session-v085.md, improvement-cycles.md, MEMORY.md), tldr post #1769