Detect Empty Backup File: Verify Files Aren't Zero Bytes
Your backup script completes successfully, but the backup file is empty. Learn how to detect zero-byte backup files automatically.
Why Backup Files Can Be Empty
Backup files can be created but remain empty for several reasons:
- Database connection fails but script doesn't check before writing
- Disk space runs out during backup, leaving empty file
- File permissions prevent writes, but script doesn't verify
- Backup process is killed mid-operation
- Source data is empty but backup script doesn't validate
- Compression fails silently, leaving empty archive
Without checking file size, you might think backups are working when they're actually empty.
How to Detect Empty Backup Files
Check file size immediately after backup creation. Use file size checks, not just file existence. The check must be inside your backup script, not in the cron line.
Bash Example: Check File Size
#!/bin/bash
BACKUP_FILE="/backups/db-$(date +%Y%m%d).sql.gz"
pg_dump mydb | gzip > "$BACKUP_FILE"
# Get file data
FILE_EXISTS=0
FILE_SIZE=0
if [ -f "$BACKUP_FILE" ]; then
FILE_EXISTS=1
FILE_SIZE=$(stat -f%z "$BACKUP_FILE" 2>/dev/null || stat -c%s "$BACKUP_FILE")
fi
# Single ping with file data in payload
# In DeadManPing panel: set validation rules:
# - "file_exists" == 1
# - "size" > 0
# Panel will automatically detect if file is missing or empty
curl -X POST "https://deadmanping.com/api/ping/backup-daily?file_exists=$FILE_EXISTS&size=$FILE_SIZE"Python Example: Verify File Size
import os
import subprocess
import requests
backup_file = "/backups/db-backup.sql.gz"
subprocess.run(["pg_dump", "mydb"], stdout=open(backup_file.replace('.gz', ''), "w"))
subprocess.run(["gzip", backup_file.replace('.gz', '')])
# Get file data
file_exists = os.path.exists(backup_file)
file_size = os.path.getsize(backup_file) if file_exists else 0
# Single ping with file data in payload
# In DeadManPing panel: set validation rules:
# - "file_exists" == True
# - "size" > 0
# Panel will automatically detect if file is missing or empty
requests.post(f"https://deadmanping.com/api/ping/backup-daily?file_exists={file_exists}&size={file_size}")Node.js Example: Check File Statistics
const fs = require('fs');
const { execSync } = require('child_process');
const https = require('https');
const backupFile = '/backups/db-backup.sql.gz';
execSync(`pg_dump mydb | gzip > ${backupFile}`);
// Get file data
let fileExists = fs.existsSync(backupFile);
let fileSize = 0;
if (fileExists) {
fileSize = fs.statSync(backupFile).size;
}
// Single ping with file data in payload
// In DeadManPing panel: set validation rules:
// - "file_exists" == true
// - "size" > 0
// Panel will automatically detect if file is missing or empty
https.request(`https://deadmanping.com/api/ping/backup-daily?file_exists=${fileExists}&size=${fileSize}`, { method: 'POST' }).end();Monitoring Empty Backup Files
After adding file size checks to your backup script, use a dead man switch to monitor whether the check completed successfully. If your script detects an empty file and exits with error code, the ping never arrives, and you get an alert.
Include the file size in your ping payload so you can track backup sizes over time and detect gradual decreases that might indicate problems.
Bash Example: Monitor Empty Backup Files
#!/bin/bash
BACKUP_FILE="/backups/db-$(date +%Y%m%d).sql.gz"
pg_dump mydb | gzip > "$BACKUP_FILE"
# Get file data
FILE_EXISTS=0
FILE_SIZE=0
if [ -f "$BACKUP_FILE" ]; then
FILE_EXISTS=1
FILE_SIZE=$(stat -f%z "$BACKUP_FILE" 2>/dev/null || stat -c%s "$BACKUP_FILE")
fi
# Validate file size - exit if empty
if [ "$FILE_SIZE" -eq 0 ]; then
echo "Error: Backup file is empty!"
exit 1
fi
# Single ping with file data in payload
# In DeadManPing panel: set validation rules:
# - "file_exists" == 1
# - "size" > 0
# Panel will automatically detect if file is missing or empty
curl -X POST "https://deadmanping.com/api/ping/backup-daily?file_exists=$FILE_EXISTS&size=$FILE_SIZE"Python Example: Monitor Empty Backup Files
import os
import subprocess
import requests
import sys
backup_file = "/backups/db-backup.sql.gz"
subprocess.run(["pg_dump", "mydb"], stdout=open(backup_file.replace('.gz', ''), "w"))
subprocess.run(["gzip", backup_file.replace('.gz', '')])
# Get file data
file_exists = os.path.exists(backup_file)
file_size = os.path.getsize(backup_file) if file_exists else 0
# Validate file size - exit if empty
if file_size == 0:
print("Error: Backup file is empty!")
sys.exit(1)
# Single ping with file data in payload
# In DeadManPing panel: set validation rules:
# - "file_exists" == True
# - "size" > 0
# Panel will automatically detect if file is missing or empty
requests.post(f"https://deadmanping.com/api/ping/backup-daily?file_exists={file_exists}&size={file_size}")Node.js Example: Monitor Empty Backup Files
const fs = require('fs');
const { execSync } = require('child_process');
const https = require('https');
const backupFile = '/backups/db-backup.sql.gz';
execSync(`pg_dump mydb | gzip > ${backupFile}`);
// Get file data
let fileExists = fs.existsSync(backupFile);
let fileSize = 0;
if (fileExists) {
fileSize = fs.statSync(backupFile).size;
}
// Validate file size - exit if empty
if (fileSize === 0) {
console.error('Error: Backup file is empty!');
process.exit(1);
}
// Single ping with file data in payload
// In DeadManPing panel: set validation rules:
// - "file_exists" == true
// - "size" > 0
// Panel will automatically detect if file is missing or empty
https.request(`https://deadmanping.com/api/ping/backup-daily?file_exists=${fileExists}&size=${fileSize}`, { method: 'POST' }).end();Working Examples
See complete, working code examples in our GitHub repository:
View Examples on GitHub →Start Detecting Empty Backup Files
DeadManPing monitors whether your backup file verification completes. Set up monitoring in 2 minutes, get alerts when backups are empty or too small. Use dead man switch to monitor empty backups automatically.
Related Articles
Learn more about cron job monitoring and troubleshooting:
Backup Monitoring Service
Backup monitoring that doesn't touch your execution.
Backup File Zero Bytes
How to detect when backup files are zero bytes.
Verify Backup File Size
How to verify backup file sizes are within expected ranges.
Detect Empty Backup File Cron
How to detect when cron backup jobs create empty files.