Back up MySQL over SSH to your Mac: mysqldump by hand and on schedule

One Terminal command dumps the database on the server and writes it straight into a file on your Mac. Nothing stays on the server, and nothing needs installing there.

One command

If you can reach the server over SSH, a MySQL backup is one line:

ssh deploy@example.com "mysqldump --single-transaction --routines --triggers --events shop | gzip" \
  > shop-$(date +%F).sql.gz

mysqldump and gzip run on the server, and the compressed stream lands in a file on your Mac. What the flags do:

Keep the password off the command line

A password passed as -psecret shows up in the process list for other users on the server. Put it in ~/.my.cnf on the server instead:

[mysqldump]
user=shop
password=your-password

and lock the file down with chmod 600 ~/.my.cnf. mysqldump then picks up the login by itself.

Large databases: zstd instead of gzip

If the server has zstd, it compresses faster and uses every core:

ssh deploy@example.com "mysqldump --single-transaction shop | zstd -T0" > shop.sql.zst

To open it on the Mac, install zstd with Homebrew: brew install zstd.

Check the dump is complete

A dropped connection leaves a file that looks fine but stops halfway. Test the archive and look at the last line of the dump:

gzip -t shop-2026-10-18.sql.gz
gunzip -c shop-2026-10-18.sql.gz | tail -n 1

A complete dump ends with -- Dump completed on …. If that line is missing, the dump is cut short.

Restore the database

gunzip -c shop-2026-10-18.sql.gz | mysql shop

For .sql.zst use zstd -dc instead of gunzip -c. It's safer to restore into a test database first so you don't overwrite the live one. Doing that in Docker on your Mac is covered in "How to test a database backup".

On schedule

You can put the command into launchd on the Mac or into cron. But a home-made schedule quickly grows a to-do list: what if the Mac was asleep at 3:00 AM, how to delete old copies, who checks the file and who tells you it failed. More on that in "mysqldump on a schedule: cron or backups to your Mac".