Automating Server Backups with Advanced Shell Scripting
Introduction to Backup Automation
Manual server backups are error-prone and time-consuming. Automating this process with advanced shell scripting ensures consistent, scheduled data protection. This guide provides a practical, production-ready approach using Bash scripts, cron, and common Linux utilities. You’ll learn to create incremental backups, manage retention, and handle restoration verification.
Prerequisites for Shell Script Backup Automation
- Linux server (Ubuntu 20.04+ or CentOS 7+ recommended)
- Root or sudo access
- Basic Bash familiarity
- rsync installed (package:
rsync) - tar and gzip
- cron daemon running
Step 1: Create the Backup Script
Navigate to /usr/local/bin/ and create backup.sh:
sudo nano /usr/local/bin/backup.sh
Insert the following advanced script:
#!/bin/bash
# Advanced server backup script - Version 2.1
# Variables
BACKUP_DIR="/var/backups/server"
SOURCE_DIR="/var/www/html" # Change as needed
LOG_FILE="/var/log/backup.log"
DATE=$(date +%Y-%m-%d_%H%M%S)
RETENTION_DAYS=7
REMOTE_SERVER="user@backup.example.com:/remote/path/"
# Create backup directory if not exists
mkdir -p "$BACKUP_DIR"
# Full backup using tar
tar -czf "$BACKUP_DIR/full_$DATE.tar.gz" "$SOURCE_DIR" 2>> "$LOG_FILE"
# Verify backup integrity
tar -tzf "$BACKUP_DIR/full_$DATE.tar.gz" > /dev/null 2>> "$LOG_FILE"
if [ $? -eq 0 ]; then
echo "Full backup successful: $DATE" >> "$LOG_FILE"
else
echo "Backup verification failed at $DATE" >> "$LOG_FILE"
exit 1
fi
# Incremental backup with rsync
rsync -avz --delete "$SOURCE_DIR" "$BACKUP_DIR/incremental_$DATE/" >> "$LOG_FILE" 2>&1
# Sync to remote server (optional)
rsync -avz --delete "$BACKUP_DIR/" "$REMOTE_SERVER" >> "$LOG_FILE" 2>&1
# Retention policy: remove old backups
find "$BACKUP_DIR" -name "*.tar.gz" -type f -mtime +"$RETENTION_DAYS" -exec rm {} ;
find "$BACKUP_DIR" -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} ;
echo "Backup cycle completed at $DATE" >> "$LOG_FILE"
Step 2: Set Permission and Make Executable
sudo chmod +x /usr/local/bin/backup.sh
sudo chown root:root /usr/local/bin/backup.sh
Step 3: Configure Log Rotation
To prevent log overflow, create /etc/logrotate.d/backup:
/var/log/backup.log {
daily
rotate 7
compress
missingok
notifempty
}
Step 4: Schedule with Cron
Open crontab editor: sudo crontab -e
Add for daily backup at 2 AM:
0 2 * * * /usr/local/bin/backup.sh
For hourly incremental syncing:
0 * * * * /usr/local/bin/backup.sh --incremental-only
(Modify script to accept flag for incremental-only mode)
Step 5: Implement Disaster Recovery Testing
Create a testing script /usr/local/bin/test-restore.sh:
#!/bin/bash # Test restore from latest backup LATEST=$(ls -t /var/backups/server/full_*.tar.gz | head -1) tar -xzf "$LATEST" -C /tmp/backup_test/ && echo "Restore test passed" || echo "Restore test failed"
Step 6: Monitor and Alert
Add email alert on failure by inserting before exit 1:
mail -s "Backup failed on $(hostname)" admin@example.com < "$LOG_FILE"
Ensure mailutils or sendmail is installed.
Best Practices for Shell Scripted Backups
- Use absolute paths in cron to avoid environment issues.
- Separate critical data into multiple backup jobs (database, application, configuration).
- Encrypt backups for remote transfer using GPG:
gpg --encrypt --recipient key backup.tar.gz. - Validate checksums with
md5sumorsha256sumafter each backup. - Document your script with comments for team handover.
Conclusion
This advanced shell scripting approach automates server backups with integrity verification, retention management, remote syncing, and monitoring. Customize the SOURCE_DIR and retention policy for your environment. Regularly test restoration to ensure your backup automation works when needed.