mysql chokes halfway through the SQL file. The dump is truncated. The last few tables are missing entirely. The backup job never lied about running — it lied about finishing cleanly.
This happens more often than people expect, and the root cause is almost always the same: something killed or interrupted mysqldump mid-stream, but the script that called it never checked properly.
Why mysqldump Exits Dirty but Your Script Says "Success"
mysqldump straight into gzip or xz. In bash, the exit code of a pipeline is the exit code of the last command. So mysqldump ... | gzip > backup.sql.gz returns the exit code of gzip, not mysqldump. If mysqldump dies mid-stream (lost connection, killed by OOM, authentication timeout), gzip still finishes compressing whatever partial data it received and exits 0. Your script sees 0 and moves on.
Disk fills up. mysqldump writes to stdout, the shell redirects to a file, the filesystem runs out of space. Depending on buffering and how the write error propagates, mysqldump may or may not get a non-zero exit. Even when it does, many wrapper scripts do not check $? at all — they just run the next line.
Timeout or OOM kill. On large databases, mysqldump can run for a long time. If a cron timeout, systemd TimeoutSec, or the kernel OOM killer terminates the process, the dump file is cut off wherever the write buffer stopped. The parent script may not distinguish between a normal exit and a signal kill unless it inspects the exit code carefully.
Check 1: Capture the Real Exit Code from a Pipeline
mysqldump into a compressor, you need PIPESTATUS (bash) or set -o pipefail to catch failures from earlier stages of the pipeline.
Add set -o pipefail near the top of your backup script. With this option, the pipeline returns the exit code of the rightmost command that failed, not just the last command.
BASH
#!/bin/bash
set -o pipefail
mysqldump -u root --single-transaction --all-databases | gzip > /backup/all-databases.sql.gz
RC=$?
if [ $RC -ne 0 ]; then
echo "mysqldump pipeline failed with exit code $RC" >&2
# send alert, do not rotate old backups away
exit 1
fiset -o pipefail, that $RC would reflect only gzip. With it, a mysqldump failure propagates.
If you need to know exactly which stage failed, inspect the PIPESTATUS array right after the pipeline runs. {PIPESTATUS[0]} is the exit code of mysqldump, {PIPESTATUS[1]} is gzip.
Note: PIPESTATUS is a bash feature. If your script runs under /bin/sh (which might be dash on Debian/Ubuntu), this does not exist. Check your shebang line.
Check 2: Look for the Completion Marker
mysqldump writes a comment block at the very end of a successful dump. The exact text varies slightly between MySQL and MariaDB versions, but it typically includes a line like:
TEXT
-- Dump completed on 2025-07-11 14:32:05.sql file:
BASH
tail -5 /backup/all-databases.sql | grep -c 'Dump completed'0, the dump is incomplete. If it is 1, the dump at least finished writing.
For a gzip-compressed file, use zcat piped to tail:
BASH
zcat /backup/all-databases.sql.gz | tail -5 | grep -c 'Dump completed'--single-transaction was not used on an InnoDB database, or because MyISAM tables changed during the dump. The completion marker only proves mysqldump reached the end. It does not prove the data is internally consistent.
Check 3: Test-Restore to a Throwaway Database
- Create a throwaway database (or use
--one-databaseto import into a test schema). - Run
mysqlagainst the dump file and check the exit code. - Run a few
SELECT COUNT(*)queries on key tables and compare row counts against production. - Drop the throwaway database.
--all-databases dump, be careful: importing it on a live server will overwrite existing databases. For test-restore purposes, it is safer to spin up a temporary MySQL instance on a different port or use a container.
How often to test-restore depends on how much you trust your pipeline and how painful a failed restore would be. For databases that matter, once a week is a reasonable starting point. Automate it: a cron job that restores last night's dump into a test instance, checks row counts, and alerts on failure.
For more background on why a green status on your backup dashboard does not mean your restore will work, see Why a Successful Backup Job Does Not Guarantee a Working Database Restore.
Check 4: Monitor File Size Over Time
Putting It Together in a Backup Script
- Use
set -o pipefailso pipeline failures propagate. - Capture
$?after themysqldump | gzippipeline. - If exit code is non-zero, alert and do not rotate old backups.
- If exit code is zero, decompress the tail and check for the completion marker.
- Compare file size against the previous backup.
- Periodically (weekly or per your policy), run a test-restore and verify row counts.
When the Problem Is Upstream
rsync interrupted by a network timeout, scp killed by a SSH session limit, an S3 multipart upload that silently dropped a part. The file on the remote end is smaller than the local copy.
After transferring, compare checksums. md5sum or sha256sum on both ends. If they do not match, the remote copy is not trustworthy. Some backup tools do this automatically; if yours does not, add it.
Backup verification is not glamorous work. Nobody notices it until a restore fails. But the difference between a backup that works and a backup that looked like it worked is the difference between a bad day and a catastrophic one. Check exit codes, check the completion marker, compare file sizes, and test-restore regularly. That is the whole recipe.