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
--single-transactiongives a consistent snapshot of InnoDB tables without locking.- Test a restore occasionally:
gunzip -c file.sql.gz | mysql dbname_restore_test. - Backups on the same disk protect against bad writes and mistakes, not against losing the
server. Copy them off-box if the data matters. Note also that runtime data outside the DB
(SQLite files, uploaded photos under
/var/www/<app>-data/) is not covered by this. - On this small 25 GB server, keep an eye on
df -h; compressed dumps of small databases are tiny, but retention adds up for big ones.
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
- Commit a structure-only dump as the schema baseline (
mysqldump --no-data dbname > api/schema.sql). Before this, 14 of Run's 15 tables had no DDL anywhere in the repo, so the backend could not be rebuilt from source. - If the deploy script only copies migration files, someone has to apply them by hand:
ssh server "mysql dbname" < api/migrations/NNN.sql(with credentials from an options file, not on the command line). Record which migrations have been applied. - Testing API changes against a throwaway DB on the server (no local PHP/MySQL on the
desktop): rsync
api/to a scratch dir, createdbname_testfromschema.sql+ migrations, symlink the prodvendor/, copy configs with the DB name swapped, runphp -S 127.0.0.1:8099 -t public public/index.php, curl it, then drop the DB and the dir. - Gotcha: do not
pkill -f <pattern>over ssh when the pattern also appears in your own ssh command line; it kills your session. Free the port withfuser -k 8099/tcpinstead.
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
- Setting up an Alma Linux 9 LAMP server
- Linode (Akamai) Object Storage
- Slim 4 PHP APIs (and migrating from Limonade)
- Game content data models (stable ids, schemas, serialization)
Linked from
- A lightweight CMS for a small business site
- Backing up Linode Object Storage buckets with rclone
- Changing a production MariaDB safely (schema, data fixes, id generation)
- Deploy scripts with rsync (patterns and traps)
- Run (runrunrunrun.run), Justin's speedrun platformer
- Slim 4 PHP APIs (and migrating from Limonade)