Skip to content

Tarbox/home-backup

Repository files navigation

Linux Home Backup Script (rsync + systemd)

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

📌 Features

  • 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)

🧠 How It Works

The script:

  1. Ensures it runs as root
  2. You can backup multiple directories by listing them in SOURCES array in /etc/home-backup.conf:
SOURCES=(
  "/home/user/Documents/"
  "/home/user/Downloads/"
  1. Builds dynamic --exclude arguments
  2. Executes rsync with proper flags
  3. Logs output and exit status
  4. Returns correct exit code for automation

🗂 Configuration

Copy the example config to /etc:

sudo cp home-backup.conf.example /etc/home-backup.conf

🔄 rsync Command Used

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

🚀 Installation

  1. Copy the script:
sudo cp home-backup.sh /usr/local/bin/
  1. Make it executable:
sudo chmod +x /usr/local/bin/home-backup.sh
  1. Normal launch or Dry-run
sudo home-backup.sh

4.(Optional) Dry-run(check only, no copying)

sudo ./home-backup.sh --dry-run

⏱ Automation with systemd

Copy 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.timer

Check status:

systemctl list-timers

📜 Logs

View log file:

sudo tail -n 50 /var/log/rsync-home-backup.log

Or via journal:

journalctl -u home-backup.service

The script can send email alerts when a backup fails. Example configuration:

ALERT_EMAIL="admin@example.com"

Required packages:

sudo apt install mailutils

🔄 Restore Procedure

Full restore:

sudo rsync -aAXHv \
  /media/user/UUID/home_backup/ \
  /home/

🏗 Future Improvements

  • Snapshot support (--link-dest)
  • Email notifications on failure
  • Integrity verification
  • Migration to borg or restic
  • Remote backup via SSH

🎯 Project Purpose

This project demonstrates:

  • Practical Bash scripting
  • Proper rsync usage
  • systemd timer automation
  • Logging and error handling
  • Production-style Linux administration workflow

About

Backup home directory to external SSD

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages