A layered, monitored 3-2-1 backup setup for a homelab, built from small shell scripts and systemd timers. Pull data, let a snapshotting filesystem own the history, push an encrypted copy offsite, and alert if any layer goes stale.
Three tiers:
source host --(pull)--> local staging --(snapshot)--> point-in-time history
|
'--(borg)--> encrypted, deduped offsite
The scripts are short; the reasoning is the point.
- Pull, not push. The backup host reaches out to the source over SSH. A source box that gets compromised has no path to the backups or their history, it can't reach in and delete them.
- Consistent SQLite dumps before rsync. A live
.dbcopied mid-write is a torn, possibly corrupt file.backup-pull.shruns SQLite's online.backupon the remote first, then rsyncs the clean.bak. No torn databases. - Snapshots are the history, not
--link-dest. The pull usesrsync --deleteto keep an exact mirror, then a CoW filesystem (ZFS via sanoid, or btrfs via btrbk) takes an atomic snapshot. That's cheaper than hardlink trees, prunes cleanly on a retention ladder, and the snapshot is taken after a complete pull, never mid-rsync. - Freshness is gated on success. The
_last_success_timestampmarker (and its Prometheus textfile metric) is written only when every path pulled cleanly. So a partial or failed run leaves the timestamp stale, and monitoring alerts on it. A backup that silently half-fails is worse than one that loudly fails. - Offsite is encrypted and deduped.
backup-offsite.shuses borg (zstd, client-side encryption) to a remote repo, with its own retention. The passphrase is read from a file, never inline in the script. - Timers don't collide. The pull runs hourly; the offsite runs at a fixed early hour so it never overlaps a top-of-hour pull.
pull/backup-pull.sh rsync data + consistent SQLite dumps + freshness marker
offsite/backup-offsite.sh borg push to an encrypted offsite repo
snapshots/sanoid.conf.example ZFS snapshot retention templates
snapshots/btrbk.conf.example btrfs snapshot + off-box send config
systemd/ .service + .timer for the pull and offsite jobs
- Pull: edit the CONFIG block in
pull/backup-pull.sh(remote host, paths, SQLite DBs), install it to/usr/local/bin/, and enablebackup-pull.timer. - Snapshots: put
sanoid.conf.exampleat/etc/sanoid/sanoid.conf(ZFS) orbtrbk.conf.exampleat/etc/btrbk/btrbk.conf(btrfs), adjust the datasets/retention. The pull callssanoid --cronafter a successful run. - Offsite: edit the repo +
BORG_PASSPHRASEfile path inoffsite/backup-offsite.sh, install it, and enablebackup-offsite.timer.
Each job writes a *_last_success_timestamp_seconds metric into the node_exporter
textfile directory. Alert on time() - backup_pull_last_success_timestamp_seconds and
its offsite counterpart to catch a backup that has stopped running. Pairs directly with
homelab-monitoring.
MIT, see LICENSE.