When working with tar
and bzip2
compression, you might encounter this puzzling message:
tar: dump.sql: File shrank by 19573448400 bytes; padding with zeros
This typically occurs during extraction when the actual file size is smaller than what the tar header indicates. Let's dive into why this happens and how to handle it properly.
1. Interrupted Compression Process: When the original tar creation was interrupted mid-process:
# Example of a problematic creation command
tar -cvjf backup.tar.bz2 large_db_dump.sql
# If interrupted with Ctrl+C or system crash
2. Improper File Handling: When files are modified during archiving:
# While this command runs:
tar -cvjf live_data.tar.bz2 /var/lib/mysql/
# If MySQL writes to the files simultaneously
Solution 1: Always verify your archives after creation:
# Create with verification
tar -cvjWf verified_backup.tar.bz2 important_data/
# Then test extraction
tar -tvjf verified_backup.tar.bz2
Solution 2: For critical systems, use atomic operations:
# Create temporary file first
tar -cvjf backup_temp.tar.bz2 data/
# Then rename atomically
mv backup_temp.tar.bz2 final_backup.tar.bz2
When you must recover data from a problematic archive:
# Try partial recovery with bsdtar
bsdtar -xvjf damaged.tar.bz2 --ignore-zeros
# Or with GNU tar's rescue option
tar -xvjf damaged.tar.bz2 --ignore-zeros --wildcards '*.sql'
For programming contexts, consider implementing checks:
import tarfile
def safe_extract(tar_path):
try:
with tarfile.open(tar_path, "r:bz2") as tar:
for member in tar.getmembers():
if member.size > 100 * 1024 * 1024: # 100MB threshold
print(f"Warning: Large file detected {member.name}")
tar.extract(member)
except tarfile.TarError as e:
print(f"Extraction error: {e}")
# Implement recovery logic here
- Always check available disk space before creating large archives
- Use
sync
before archiving critical data
- Consider splitting large backups into multiple volumes
- Implement MD5/SHA checksum verification
Remember that while the zero-padding warning might seem harmless, it often indicates deeper issues with your backup integrity that should be investigated.
When working with large database dumps or binary files, you might encounter this peculiar tar warning during compression:
tar: dump.sql: File shrank by 19573448400 bytes; padding with zeros
This occurs when:
- The source file is actively being written during tar operation (common with log rotation or database dumps)
- Network filesystems (NFS/SMB) report incorrect file sizes during transfer
- File truncation happens mid-operation due to storage issues
For database dumps:
# Proper way to handle MySQL dumps
mysqldump -u user -p db | bzip2 > dump.sql.bz2
# Instead of:
mysqldump -u user -p db > dump.sql && tar cjvf dump.sql.tar.bz2 dump.sql
Using GNU tar sparse file handling:
tar --sparse -cvjf archive.tar.bz2 large_file.bin
To validate file integrity before compression:
# Check file stability
filesize=$(stat -c%s "dump.sql")
sleep 5
if [ $(stat -c%s "dump.sql") -ne $filesize ]; then
echo "File still changing - wait for write completion"
exit 1
fi
For NFS-mounted volumes, add sync option to /etc/fstab:
nfs-server:/path /mnt nfs rw,sync,hard 0 0
Consider parallel compression for large files:
pbzip2 -c dump.sql > dump.sql.bz2
“Resolving ‘tar: File Shrank by X Bytes Padding with Zeros’ Error During bz2 Compression”
2 views