Use a login path so no password touches the command line, and check the pipeline’s exit status, not the shell’s:
mysql_config_editor set --login-path=backup --host=localhost --user=backupuser --password
set -o pipefail
mysqldump --login-path=backup --single-transaction --quick db_name | gzip -c > db.sql.gz || echo "DUMP FAILED"
gunzip -c db.sql.gz | tail -1 | grep -q '^-- Dump completed' || echo "TRUNCATED BACKUP"
The command mysqldump -u root -p[password] db | gzip > out.gz and a cron line built on the same command have two independent failure modes, both of which were reproduced below on MySQL 8.4.10. One of them produces a backup file that is valid, plausible, and empty.
Table of Contents
The backup that isn’t: | gzip throws away the exit status
In a | b, the shell reports b‘s exit status. gzip succeeds at compressing nothing, so a dump that died leaves a perfectly well-formed .gz. The same pipeline, pointed at a database that does not exist:
$ mysqldump -u root hw_w11_does_not_exist | gzip > bad.sql.gz
mysqldump: Got error: 1049: Unknown database 'hw_w11_does_not_exist' when selecting the database
$? = 0
PIPESTATUS = 2 0
gzip -t = 0 (0 = the archive is well formed)
size = 369 bytes on disk
Exit 0. Valid gzip. And this is the entire contents:
-- MySQL dump 10.13 Distrib 9.7.1, for macos26.4 (arm64)
--
-- Host: localhost Database: hw_w11_does_not_exist
-- ------------------------------------------------------
-- Server version 8.4.10
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
...
CREATE/INSERT statements in it: 0
Worse, verifying a backup with gunzip -c backup.sql.gz | head -n 20 and checking that it starts with -- MySQL dump passes on the file above. That verification method certifies an empty backup as good.
The header is written before the first query runs, so it proves nothing. The trailer is written last:
good backup, last line: -- Dump completed on 2026-08-11 12:34:14
bad backup, last line: /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
set -o pipefail makes the pipeline’s exit status the status of the last (rightmost) command to exit non-zero, instead of the status of its last command. Same command, same failure, with pipefail on: exit status with pipefail = 2. If you cannot use pipefail, read ${PIPESTATUS[0]} immediately after the pipeline — any other command in between overwrites it.
A cron line with a bare -p cannot work
0 2 * * * mysqldump -u root -p my_database | gzip > ... uses -p with no value, which prompts. Cron has no terminal. Run the way cron runs it — /bin/sh -c, stdin closed:
shell exit = 0
stderr: Enter password: mysqldump: Got error: 1045: Access denied for user
'hw_w11_bak'@'localhost' (using password: NO) when trying to connect
cron_out.sql.gz: 20 bytes, gzip -t exit 0
tables in it: 0
A twenty-byte file, structurally valid, containing nothing, produced silently every night at 2am. Both bugs compound: the prompt fails, and the pipe hides it.
Passwords: what actually leaks, and what doesn’t
The usual warning is that -p[password] shows up in ps for everyone on the box. On this build that is only half true, and the half that survives is the half that matters. The mysqldump process scrubs its own arguments:
$ ps -Ao pid,args | grep '[m]ysqldump'
14452 mysqldump -u hw_w11_bak -px xxxxxxxxxxxxxxxxxxxxxxxx
But the shell that launched it does not, and that is exactly how cron invokes a crontab line. Both processes, sampled at the same moment — the parent still holds the credential the child has already scrubbed:
15416 /bin/sh -c mysqldump -u hw_w11_bak -p's3cret-not-a-real-password' hw_w11_cons > /dev/null 2>&1
15418 mysqldump -u hw_w11_bak -px xxxxxxxxxxxxxxxxxxxxxxxx
Plaintext, in the process table. The client also warns on every run — mysqldump: [Warning] Using a password on the command line interface can be insecure. — and the command lands verbatim in your shell history file, which was confirmed here by pointing HISTFILE at a scratch file and grepping it.
mysql_config_editor writes ~/.mylogin.cnf with mode 600 and no plaintext (grep for the password returns 0 matches; the file is binary). mysqldump --login-path=backup db then appears in ps with no credential at all. Be clear about what this buys: the file is obfuscated, not encrypted, and anyone who can read it can decode it —
$ my_print_defaults --show hw_w11_backup
--user=hw_w11_bak
--password=s3cret-not-a-real-password
--host=localhost
A plain option file with --defaults-extra-file=./backup.cnf works the same way and is easier to deploy with configuration management. Keep it at 600: at 666 the client refuses it outright rather than falling back silently.
0600 exit=0 stderr=[]
0666 exit=2 stderr=[mysqldump: [Warning] World-writable config file
'./bk2.cnf' is ignored.
mysqldump: Got error: 1045: Access denied for user
'hw_w11_bak'@'localhost' (using password: NO)]
--single-transaction, and what the default is really doing
The reason --single-transaction matters turned out to be different from what I expected, so here is what the server actually logged. With the general query log on, a plain mysqldump db issues:
LOCK TABLES `t_a` READ /*!32311 LOCAL */,`t_b` READ /*!32311 LOCAL */
...
UNLOCK TABLES
So a single-database dump is internally consistent — because it read-locks every table in that database for the whole dump. That is not free. Two tables of 300,000 rows each, one UPDATE issued 1.5 seconds into the dump:
mysqldump db one UPDATE took 3.990 s
mysqldump --single-transaction db one UPDATE took 0.015 s
(no dump running) one UPDATE took 0.012 s
Four seconds of blocked writes, to produce a 1,282,012,246-byte dump. Scale that to a real database. With --single-transaction, mysqldump instead issues SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ and START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */, and takes no locks at all.
The consistency argument bites the moment you dump more than one database — for instance with --all-databases. That command issued 61 separate LOCK TABLES / UNLOCK TABLES pairs here, one per database. Between them, writers run free. Three databases, a writer holding the invariant t_a.bal + t_b2.bal == 0 across two of them:
--databases cons filler cons2 t_a=-3989 t_b2=14572 (off by 10583)
--single-transaction --databases cons filler cons2 t_a=-4471 t_b2=4471 (exactly 0)
The default dump captured a state that never existed. --single-transaction is InnoDB-only; the MySQL manual notes it does not give a consistent read for MyISAM or MEMORY tables, and that DDL running concurrently can still break it. That caveat is cited, not tested here.
Verify the contents, not the exit code
gunzip -c db.sql.gz | mysql --login-path=restore restore_db
mysql -e "CHECKSUM TABLE src.orders, src.customers;"
mysql -e "CHECKSUM TABLE restore_db.orders, restore_db.customers;"
hw_w11_vfy.orders 1390109300 hw_w11_restore.orders 1390109300
hw_w11_vfy.customers 714641681 hw_w11_restore.customers 714641681
CHECKSUM TABLE reads every row, so matching values mean the data round-tripped rather than that the command exited 0. A row with a literal newline and non-ASCII text came back byte-identical, compared with HEX() on both sides. A restore into a scratch database is the only test that exercises the whole path.
MySQL 8.4 specifics
Advice written for 5.7 will mislead you here:
mysql> SELECT @@default_authentication_plugin;
ERROR 1193 (HY000): Unknown system variable 'default_authentication_plugin'
mysql> SELECT @@authentication_policy; -> *,,
PLUGIN_NAME PLUGIN_STATUS
sha256_password ACTIVE
caching_sha2_password ACTIVE
mysql_native_password DISABLED
A backup user created from a 5.7-era guide that pins IDENTIFIED WITH mysql_native_password will fail to connect until that plugin is explicitly enabled. Create it plainly, let caching_sha2_password apply, and grant only what a dump needs — SELECT, SHOW VIEW, LOCK TABLES, PROCESS, RELOAD, EVENT, TRIGGER.
What was and was not tested
Everything above ran on MySQL server 8.4.10 (from SELECT VERSION()) with the Homebrew 9.7.1 client on macOS 26.6 arm64. mysqldump --version reports the client, not the server. Against a local MySQL server that you can reach as root, create-compressed-backup.sh reproduces the exit-0 failed dump, the missing trailer, the lock timings and the CHECKSUM TABLE comparison; the scratch databases and demo account it creates stay until you run the DROP statements it prints.
Not tested here: Linux. The ps result above is macOS behaviour — on Linux /proc/<pid>/cmdline is world-readable by default, which makes the shell-wrapper leak worse, not better, but that was not executed. Also untested: the MyISAM/DDL limits of --single-transaction (cited from the manual), --source-data/binary-log positioning for replication, and any physical-backup tool. This page is about logical dumps, and a logical dump of a large database is not a fast recovery path regardless of how well it compresses.
Scratch databases used here were hw_w11_cons, hw_w11_cons2, hw_w11_filler, hw_w11_vfy and hw_w11_restore. To remove them: DROP DATABASE hw_w11_cons; DROP DATABASE hw_w11_cons2; DROP DATABASE hw_w11_filler; DROP DATABASE hw_w11_vfy; DROP DATABASE hw_w11_restore;
The password visible in the ps and my_print_defaults output above is a throwaway string, not a credential — the point of those blocks is that the string is readable at all, not which string it is. The demo account 'hw_w11_bak'@'localhost' was dropped after the run (DROP USER 'hw_w11_bak'@'localhost';), and the login path was written to a scratch file via MYSQL_TEST_LOGIN_FILE rather than to ~/.mylogin.cnf. If you follow this page, use your own credential and give the backup user only the grants listed above.