What the message looks like
gzip: stdin: unexpected end of file
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting nowCommon causes
- A download or copy was interrupted and the file is shorter than announced.
- The disk filled up during creation or the process was killed.
- A transfer in ASCII mode changed bytes, for example over FTP.
- The archive was read while still being written, or sits on a filesystem with errors.
- The file was modified after creation, for example by an incomplete
scp.
Check
- Test the compression
gzip -t backup.tar.gz; echo "exit=$?" - Check the archive without extracting
tar -tzf backup.tar.gz > /dev/null; echo "exit=$?" - Size and checksum
ls -l backup.tar.gz; sha256sum backup.tar.gz - How far can it be read?
tar -tzf backup.tar.gz 2>/dev/null | tail -n 5
Fix
- Download or recreate it while the source still exists. That is the clean path.
- For a partial archive, salvage the readable parts:
tar -xzf backup.tar.gz --ignore-zerosorgzip -dc backup.tar.gz | tar -x. - Use
gzip -dc | tar -tvto see how far the data is readable. - Store a checksum after creating archives and compare it before restoring.
- Do not write backups to the same filesystem as the source, and check the exit code of the creation step.
Typical scenario
A 40 GB backup was copied to another server with scp, the connection dropped on the way, and the script did not notice. When unpacking, tar -xzf backup.tar.gz reports gzip: stdin: unexpected end of file. First compare size and checksum of source and copy with sha256sum. If they differ, the transfer is to blame and not the original archive.
FAQ
Can I repair a broken gz?
Not fully. Readable parts can be extracted, but the missing bytes are gone.
Why is the archive smaller than expected?
The write was interrupted: full disk, killed process or an aborted transfer.
How do I check archives right after the backup?
gzip -t file.tar.gz tests the compression, tar -tzf file.tar.gz > /dev/null the whole archive. Also store a checksum and compare it after copying. A backup only counts as good once you have tested a restore.
Can I still rescue files up to the break?
Often yes. tar -xzf extracts the files read before the break and reports the error only at the damaged remainder. With zcat backup.tar.gz | tar -x you also get as far as the data goes. The last file is usually incomplete.
How do I avoid truncated archives in scripts?
Set set -o pipefail so an error in a pipeline does not go unnoticed, check free space before the run and write to a temporary name first, renaming it only after a successful test. A full disk is a common cause.