mysqldump on a schedule: cron on the server or backups to your Mac

The most common way to automate MySQL backups is a mysqldump script in cron. It works until it breaks, and it usually breaks silently.

A typical script

#!/bin/bash
set -euo pipefail
d=$(date +%F)
mysqldump --single-transaction --routines --triggers --events shop \
  | gzip > /var/backups/shop-$d.sql.gz
find /var/backups -name 'shop-*.sql.gz' -mtime +14 -delete

And a line in crontab -e to run it every night at 3:00 AM:

0 3 * * * /usr/local/bin/backup-shop.sh

Where these scripts let you down

Keeping copies on your own machine

You can pull copies to your own computer: dump over SSH straight into a file on the Mac, as in "Back up MySQL over SSH to your Mac". Instead of cron, the Mac has launchd, which can do something cron can't: if the Mac was asleep at 3:00 AM, a StartCalendarInterval job runs when it wakes up.

Verification, rotation, retries and notifications are still yours to write.

A good backup checklist