Insights

Backup Job Says Success but Restore Fails? Check mysqldump for Truncated Output

November 22, 2026
0:00
Your backup cron job runs every night, exits 0, maybe even sends a "Backup completed" email. Months later you need to restore, and 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.
Three common scenarios produce a truncated dump that looks fine to a naive wrapper script: Pipe swallows the exit code. Many backup scripts pipe 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.
If your backup script pipes 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
fi
Without set -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.
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
If the dump was cut short, this line is missing. This is the single fastest way to verify an existing dump file without restoring it. For an uncompressed .sql file:
BASH
tail -5 /backup/all-databases.sql | grep -c 'Dump completed'
If the output is 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'
This reads through the entire compressed file, so it takes time on large dumps. For a quick sanity check on many files at once, you can script this in a loop over your backup directory. One thing this does not catch: a dump that completed but is logically inconsistent because --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.
The only way to know a backup actually works is to restore it. Checking exit codes and completion markers catches the obvious truncation cases, but subtle corruption — character set issues, a trigger definition that references a missing function, a view that depends on a table dumped later — only surfaces during an actual import. You do not need a second production server for this. A test restore can run on the same machine into a temporary database, or on a small staging VPS. Basic approach:
  1. Create a throwaway database (or use --one-database to import into a test schema).
  2. Run mysql against the dump file and check the exit code.
  3. Run a few SELECT COUNT(*) queries on key tables and compare row counts against production.
  4. Drop the throwaway database.
If you are restoring a full --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.
A truncated dump is usually much smaller than a complete one. If your nightly backup has been producing files around 2 GB and tonight's file is 400 MB, something went wrong. You do not need fancy monitoring for this. A simple script that records the file size after each backup run and compares it to the previous run catches dramatic drops. If the new file is less than, say, 50% of the previous file's size, trigger an alert. The exact threshold depends on your data — a database that rarely changes will produce nearly identical dump sizes night after night. This is a heuristic, not a guarantee. A legitimately smaller dump (someone dropped a large table) will trigger a false alarm. But a false alarm you investigate is far better than a silent truncation you discover during a real outage.
A minimal backup script that covers these checks:
  1. Use set -o pipefail so pipeline failures propagate.
  2. Capture $? after the mysqldump | gzip pipeline.
  3. If exit code is non-zero, alert and do not rotate old backups.
  4. If exit code is zero, decompress the tail and check for the completion marker.
  5. Compare file size against the previous backup.
  6. Periodically (weekly or per your policy), run a test-restore and verify row counts.
Steps 1 through 5 add maybe 10 lines to an existing backup script. Step 6 is a separate job. The key principle: never trust a backup you have not restored. Exit codes and completion markers are fast sanity checks that catch the most common failure mode — truncation — but they are not a substitute for actually importing the data and confirming it is usable.
Sometimes the dump completes fine, but the transfer to offsite storage truncates the file. 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.

Join My Newsletter & Get Updates!

Get notified whenever a new technical guide, server infrastructure breakdown, or personal reflection goes live, plus a curated weekly digest.