Skip to content

rohitrsrohit/day-03-backup-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

🗄️ Day 03 — Automated Backup Script with Rotation

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.


📸 Sample Output

╔══════════════════════════════════════════════════════╗
║       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
══════════════════════════════════════════════════════

📁 Project Structure

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

⚙️ Features

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

🚀 Quick Start

1. Clone

git clone https://github.com/YOUR_USERNAME/day-03-backup-script.git
cd day-03-backup-script
chmod +x scripts/backup.sh

2. Configure

nano config/backup.conf

Key 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")

3. Run backup

bash scripts/backup.sh
# or explicitly:
bash scripts/backup.sh backup

4. List all backups

bash scripts/backup.sh list

5. Restore a backup

# 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/restored

6. Run tests

bash tests/test_backup.sh

⏰ Automate with Cron

crontab -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

📂 Archive Naming Convention

{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

🔧 Remote Backup via rsync

# In backup.conf:
ENABLE_REMOTE=true
REMOTE_DEST="ubuntu@192.168.1.100:/mnt/nas/backups"

Requires SSH key authentication set up between machines.


🛠️ Requirements

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

📊 Backup Rotation Explained

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.


🤝 Part of 30 Days of DevOps

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

📄 License

MIT — free to use, modify, and distribute.

About

day-03-backup-script

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors