Weekly production DB backups: pull over SSH, verify before trust
A weekly cron pulls a MySQL dump over SSH into gzip on a second machine and runs three sanity gates before the backup file gets its final name.
TL;DR
A second machine pulls the blog's MariaDB backup from its VPS weekly over SSH, compressing it into one .sql.gz file. Nothing counts as a backup until the streamed .part file passes three checks: minimum size, counted CREATE TABLE statements, and the mysqldump "Dump completed on" footer. flock and BatchMode keep the cron job safe and non-interactive.
The production database of this blog lives inside a MariaDB container on a VPS. Last week I added one line to the crontab of a second machine: every Sunday at 04:30, that machine pulls the entire database over SSH, compresses it into a single .sql.gz file, and keeps only the eight newest copies. One detail makes this job more than routine housekeeping. The final backup file may not appear before it passes three checks, and those three checks are the most important part of the whole script.
The reasoning comes from documentation far older than the script. The MySQL manual states the purpose of backups plainly: data must be recoverable when system crashes, hardware failures, or users deleting data by mistake happen [2]. All three risks strike the machine where the data lives. As long as the backup copy sits on the same host as production, one failed disk is enough to erase both at once.
The pull model reverses the direction of the copy. The second machine pulls from production instead of production pushing to some destination. The practical consequences: the VPS does not need to know the backup location exists, no extra configuration lands on the server, and the transfer runs over standard SSH encryption. I installed nothing new over there; all it takes is SSH access and the mysqldump that already exists inside the container.
Three gates before trusting the file
My old habit: once the dump command exited without an error message, the file was considered good. I carried that assumption around without questioning it for years. Yet a dump streaming across a network can stop halfway, and a truncated file shows no visible difference from the outside. A corrupt backup is more dangerous than having no backup at all, because it hands out a false sense of safety.
So the script parks the streamed output as a .part file and runs three gates before granting the final name. Gate one checks a minimum size. Gate two counts CREATE TABLE statements inside the dump; a zero means the schema was not captured. Gate three leans on built-in mysqldump behavior: the utility writes a "Dump completed on" comment at the end of the dump [1]. The script reads the last five lines; without that marker, the run is declared failed. Only after all three pass does the .part file get promoted to its final name, and then retention sweeps away everything older than the eight newest copies.
The order is not a writing style choice. Verification deliberately runs against a temporary file that is cheap to throw away; the final name is granted only once the file has proven whole. A network failure in the middle of a dump then leaves nothing behind except a silently deleted .part, not a fake backup sitting among the healthy copies.
The details that make the cron trustworthy
The core of the whole operation is one command, run from the second machine:
ssh -o BatchMode=yes user@host-vps \
"docker exec container-app mysqldump --single-transaction --quick database-name" \
| gzip -c > .backup/db_$(date +%F_%H%M%S).sql.gz.part
The --single-transaction option [1] makes mysqldump open a transaction with REPEATABLE READ isolation, so the InnoDB snapshot stays consistent without blocking applications that are running. The --quick option forces row-by-row reads instead of buffering an entire table in memory. That behavior is already active through the default --opt option group; I write it explicitly so the script explains itself. Both choices fit the streaming plan: data flows from the server straight into gzip on the second machine without forming a temporary dump file on the VPS.
Two last mechanisms keep the script well-behaved as a cron worker. flock -n [4] makes a new run stop immediately while a previous run is still going, instead of stacking heavy dump processes on production. BatchMode=yes [3] turns off every interactive prompt; when the SSH key is rejected, the cron job stops fast with a clear exit code instead of hanging forever waiting for a password nobody will ever type.
What changed compared to the old way is not the file location but the conditions for its existence. Trust in a backup is now mechanical: there is no moment of "it seems finished", only a final file that stands on three checks or does not exist at all. Eight weekly copies are nowhere near the league of large infrastructure snapshots. For the database of one blog, the distance between production and its copy is already enough to close a risk I used to ignore.
Sources
- MySQL 8.0 Reference Manual, mysqldump page (accessed 2026-12-05 via Wayback snapshot 2026-11-13).
- MySQL 8.0 Reference Manual, chapter Backup and Recovery (accessed 2026-12-05 via Wayback snapshot 2026-12-01).
- OpenSSH manual, ssh_config(5), BatchMode option (accessed 2026-12-05).
- util-linux, flock(1) manual page, Arch Linux mirror (accessed 2026-12-05).