A production-style Bash backup script using rsync, designed for reliability, automation, and system administration practice.
This project demonstrates:
- Incremental backups
- File exclusions
- Logging
- systemd timer automation
- Safe restoration procedures
- Copies only new and modified files
- Preserves:
- ownership
- permissions
- ACLs
- extended attributes
- hard links
- Supports multiple source directories
- Excludes cache and trash
- Logs all activity
- Email notifications
- Automated via systemd timer
- Safe error handling (
set -euo pipefail)
The script:
- Ensures it runs as root
- You can backup multiple directories by listing them in
SOURCESarray in/etc/home-backup.conf:
SOURCES=(
"/home/user/Documents/"
"/home/user/Downloads/"- Builds dynamic
--excludearguments - Executes
rsyncwith proper flags - Logs output and exit status
- Returns correct exit code for automation
Copy the example config to /etc:
sudo cp home-backup.conf.example /etc/home-backup.confrsync -aAXHv \
--delete \
"${RSYNC_EXCLUDE[@]}" \
"$SRC" "$DST"Flags Explained
| Flag | Purpose |
|---|---|
| -A | Preserve ACLs |
| -X | Preserve extended attributes |
| -H | Preserve hard links |
| -v | Verbose output |
| --delete | Mirror destination |
| --exclude | Ignore specified patterns |
- Copy the script:
sudo cp home-backup.sh /usr/local/bin/- Make it executable:
sudo chmod +x /usr/local/bin/home-backup.sh- Normal launch or Dry-run
sudo home-backup.sh4.(Optional) Dry-run(check only, no copying)
sudo ./home-backup.sh --dry-runCopy the service and timer files:
sudo cp systemd/home-backup.* /etc/systemd/system
Reload systemd and enable the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now home-backup.timerCheck status:
systemctl list-timersView log file:
sudo tail -n 50 /var/log/rsync-home-backup.logOr via journal:
journalctl -u home-backup.serviceThe script can send email alerts when a backup fails. Example configuration:
ALERT_EMAIL="admin@example.com"
Required packages:
sudo apt install mailutilsFull restore:
sudo rsync -aAXHv \
/media/user/UUID/home_backup/ \
/home/- Snapshot support (
--link-dest) - Email notifications on failure
- Integrity verification
- Migration to borg or restic
- Remote backup via SSH
This project demonstrates:
- Practical Bash scripting
- Proper rsync usage
- systemd timer automation
- Logging and error handling
- Production-style Linux administration workflow