Skip to content

Requirements — Backup Script

What you will build and what you need before starting.

Learning Objectives

  • Use rsync for incremental, bandwidth-efficient backups
  • Create timestamped archives with tar
  • Implement a rotation policy: keep the last N backups, delete older ones
  • Verify backup integrity with checksums (sha256sum)
  • Build a recovery/restore mode alongside the backup mode

What You Will Build

backup.sh — a script that backs up one or more source directories to a destination, manages backup rotation, and can restore from any saved backup:

~/backups/ ├── 2024-01-15_090000/ │ ├── home_user_projects.tar.gz │ └── home_user_projects.tar.gz.sha256 ├── 2024-01-14_090000/ │ └── ... ├── 2024-01-13_090000/ │ └── ... └── backup.log

Running backup.sh --list shows:

Available backups: 1 2024-01-15_090000 (245 MB) ← newest 2 2024-01-14_090000 (242 MB) 3 2024-01-13_090000 (239 MB)

Prerequisites

Skill Where to review
rsync basics Week 02 Day 03 Part 1 — File Operations at Scale
tar and gzip Week 02 Day 03 Part 1 — File Operations at Scale
find with -mtime Day 05 Part 2 — Find & Locate
Functions Week 02 Day 01 Part 1
trap for cleanup Week 02 Day 02 Part 1 — Error Handling

Command-Line Interface

backup.sh [OPTIONS] [COMMAND] Commands: backup Run a backup (default) restore [N] Restore from backup N (1=newest) list List available backups verify [N] Verify backup N integrity Options: -s, --source DIR Source directory (repeatable for multiple dirs) -d, --dest DIR Backup destination (default: ~/backups) -k, --keep N Number of backups to keep (default: 7) -c, --compress Create compressed tar archive (default: rsync only) -h, --help Show this help

What rsync Does

rsync copies only changed files, making subsequent backups fast:

rsync -av --delete \
      ~/projects/ \
      ~/backups/2024-01-15_090000/projects/
  • -a — archive mode: preserves permissions, timestamps, symlinks
  • -v — verbose: show what was transferred
  • --delete — remove files from dest that no longer exist in source

Time Estimate

Part Time
Setup + structure 15 min
rsync backup 25 min
Timestamped archives 20 min
Rotation (keep N) 20 min
Checksum verification 15 min
Restore mode 20 min
Total ~115 min

overview | setup