30 Days of DevOps | Day 3/30 — Linux & Shell Scripting
A production-grade Bash backup system that compresses sources into .tar.gz archives,
verifies integrity with SHA-256 checksums, auto-rotates old backups, supports restore,
and generates an HTML summary report — all driven by a single config file.
╔══════════════════════════════════════════════════════╗
║ AUTOMATED BACKUP SCRIPT WITH ROTATION ║
╚══════════════════════════════════════════════════════╝
Destination : /tmp/devops-backups
Retention : 7 days
── Backing up: project-alpha
✔ Archive created : project-alpha_20260601_103000.tar.gz
✔ Size : 4.2K
✔ Checksum : a3f1c9d8e2b7...
✔ Integrity verified (archive is valid)
── Rotating old backups (>7 days)
✔ No old backups to rotate
══════════════════════════════════════════════════════
BACKUP SUMMARY
Succeeded : 2
Failed : 0
Total size: 12 KB
══════════════════════════════════════════════════════
day-03-backup-script/
├── scripts/
│ └── backup.sh # Main backup engine
├── config/
│ └── backup.conf # Sources, retention, remote settings
├── sample-data/
│ ├── project-alpha/ # Sample source to back up
│ └── project-beta/ # Sample source to back up
├── tests/
│ └── test_backup.sh # 12 automated tests
├── logs/ # Runtime logs (gitignored)
├── docs/
│ └── architecture.md
├── .gitignore
└── README.md
| Feature | Description |
|---|---|
| Multi-source backup | Configure any number of dirs/files in backup.conf |
| tar + gzip | Compressed .tar.gz archives with configurable excludes |
| SHA-256 checksums | .sha256 file created per archive for verification |
| Integrity check | tar -tzf validates archive before marking success |
| Auto-rotation | Deletes archives older than RETENTION_DAYS |
| Restore mode | Extract any archive with checksum verification |
| Manifest log | Running log of every backup with timestamp + size |
| HTML report | Dark-themed summary dashboard per run |
| Remote copy | Optional rsync to remote server |
| Email alerts | Alert on failure (requires mailutils) |
| List mode | See all current backups with size and date |
git clone https://github.com/YOUR_USERNAME/day-03-backup-script.git
cd day-03-backup-script
chmod +x scripts/backup.shnano config/backup.confKey settings:
BACKUP_DEST="/tmp/devops-backups" # Where to store archives
BACKUP_SOURCES=(
"sample-data/project-alpha" # Relative to project root
"/var/www/html" # Absolute path
"/etc/nginx"
)
RETENTION_DAYS=7 # Delete archives older than 7 days
EXCLUDE_PATTERNS=("*.log" ".git" "node_modules")bash scripts/backup.sh
# or explicitly:
bash scripts/backup.sh backupbash scripts/backup.sh list# Restore to default /tmp/restore_<timestamp>
bash scripts/backup.sh restore /tmp/devops-backups/project-alpha_20260601_103000.tar.gz
# Restore to a custom destination
bash scripts/backup.sh restore /tmp/devops-backups/project-alpha_20260601_103000.tar.gz /home/ubuntu/restoredbash tests/test_backup.shcrontab -e# Daily backup at 2am
0 2 * * * /bin/bash /path/to/day-03-backup-script/scripts/backup.sh >> /tmp/backup_cron.log 2>&1
# Every 6 hours
0 */6 * * * /bin/bash /path/to/day-03-backup-script/scripts/backup.sh backup{source_name}_{YYYYMMDD}_{HHMMSS}.tar.gz
{source_name}_{YYYYMMDD}_{HHMMSS}.tar.gz.sha256
Example:
project-alpha_20260601_020000.tar.gz
project-alpha_20260601_020000.tar.gz.sha256
# In backup.conf:
ENABLE_REMOTE=true
REMOTE_DEST="ubuntu@192.168.1.100:/mnt/nas/backups"Requires SSH key authentication set up between machines.
| Requirement | Notes |
|---|---|
| OS | Linux / macOS |
| Shell | Bash 4.0+ |
| Commands | tar, gzip, find, stat, du — all standard |
| Optional | rsync for remote copy, mail for alerts, sha256sum for checksums |
Day 1: project-alpha_20260601_020000.tar.gz ← kept
Day 2: project-alpha_20260602_020000.tar.gz ← kept
...
Day 7: project-alpha_20260607_020000.tar.gz ← kept
Day 8: project-alpha_20260608_020000.tar.gz ← kept, Day 1 deleted ✓
Day 9: project-alpha_20260609_020000.tar.gz ← kept, Day 2 deleted ✓
The rotation uses find -mtime +N which checks the file modification timestamp.
| Day | Topic |
|---|---|
| Day 1 | ✅ System Health Monitor |
| Day 2 | ✅ Log Analyzer & Report Generator |
| Day 3 | ✅ Automated Backup with Rotation (this project) |
| Day 4 | User Management Automation |
| Day 5 | Disk Usage Alerting |
MIT — free to use, modify, and distribute.