#!/usr/bin/env bash # Garage backup — meta (frequent) and data (nightly), copied OFF the Pi. # # garage-backup.sh meta # consistent metadata snapshot + layout + rpc env # garage-backup.sh data # object data tree # # Destination is the Linode VPS (10.20.0.1) over the wg-remote tunnel, NOT the # Pi and NOT a bucket in Garage. A bucket that backs up into itself is not a # backup: losing meta/ or the disk loses the "backup" with it. # # Run as root (systemd unit does): node_key is 0600 root:root, and rsync needs # to read the whole meta tree. The SSH key is the sirius user's linode_admin, # readable by root. # # Restore procedure: see ../README.md. Exit code is the truth — the units are # oneshot and `systemctl status garage-*-backup.service` shows the last result. set -euo pipefail mode=${1:-} case "$mode" in meta|data) ;; *) echo "usage: $0 meta|data" >&2; exit 2 ;; esac GARAGE_DIR=/home/sirius/websites/garage KEY=/home/sirius/.ssh/linode_admin REMOTE=root@10.20.0.1 DEST=/root/garage-backup KEEP_LOCAL_SNAPSHOTS=14 # Host key: the Pi's root has no known_hosts entry for the VPN peer, and a # rebuilt Pi would not have one either. accept-new pins it on first contact and # still fails loudly if the key later changes. The transport is the WireGuard # tunnel, so first contact is already authenticated at the network layer. export RSYNC_RSH="ssh -i $KEY -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ConnectTimeout=20" log() { printf '%s %s\n' "$(date -u +%FT%TZ)" "$*"; } # Fail loudly and early if the tunnel/target is not there. A silent "backup ok" # with the VPS unreachable is the failure mode this whole file exists to avoid. log "target check: $(ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ConnectTimeout=20 "$REMOTE" 'hostname; df -h / | tail -1')" # rsync will not create the parent of a --delete target, so make the tree first. ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new "$REMOTE" \ "install -d -m 0700 $DEST $DEST/meta $DEST/meta/snapshots $DEST/data" case "$mode" in meta) # `meta snapshot` is Garage's supported consistent copy of db.sqlite while # the server keeps running. Never rsync a live db.sqlite+wal by hand. log "taking metadata snapshot" docker exec garage /garage -c /etc/garage.toml meta snapshot --all # Prune local snapshots so /home does not fill with dailies. mapfile -t snaps < <(find "$GARAGE_DIR/meta/snapshots" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p\n' \ | sort -rn | awk '{print $2}') if (( ${#snaps[@]} > KEEP_LOCAL_SNAPSHOTS )); then log "pruning $(( ${#snaps[@]} - KEEP_LOCAL_SNAPSHOTS )) old local snapshot(s)" printf '%s\0' "${snaps[@]:$KEEP_LOCAL_SNAPSHOTS}" | xargs -0 -r rm -rf fi log "syncing metadata to $REMOTE:$DEST/meta" rsync -a --delete "$GARAGE_DIR/meta/snapshots/" "$REMOTE:$DEST/meta/snapshots/" # Layout + node identity + the RPC secret. Without node_key the restored # node gets a new identity and meta/data no longer agree. rsync -a "$GARAGE_DIR/meta/cluster_layout" \ "$GARAGE_DIR/meta/data_layout" \ "$GARAGE_DIR/meta/node_key" \ "$REMOTE:$DEST/meta/" install -d -m 0700 /etc/garage rsync -a /etc/garage/garage.toml /etc/garage/garage.env "$REMOTE:$DEST/meta/" ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new "$REMOTE" \ "chmod 600 $DEST/meta/garage.env $DEST/meta/node_key" ;; data) log "syncing object data to $REMOTE:$DEST/data" # --delete keeps the remote an exact mirror (deleted objects stay deleted). # multipart/ is in-flight upload scratch and is deliberately not mirrored. rsync -a --delete --exclude '/multipart/' \ "$GARAGE_DIR/data/" "$REMOTE:$DEST/data/" ;; esac log "remote state after $mode backup:" ssh -i "$KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new "$REMOTE" \ "du -sh $DEST/* 2>/dev/null; echo '--- newest meta snapshot ---'; ls -1dt $DEST/meta/snapshots/*/ 2>/dev/null | head -3" log "OK"