#!/bin/sh
# Copy the newest VERIFIED database snapshot off the box. Started by upstream-offsite.service (timer), or by hand with
# `systemctl start upstream-offsite` (that gives it the user, the env file and the sandbox). Nothing happens unless
# OFFSITE_DEST is set (in /etc/upstream/upstream.env); docs/DEPLOY.md section 7 "Off-box copy".
#
#   OFFSITE_DEST          required. rsync destination DIRECTORY: user@host:/srv/backups/upstream/  or a local path (a mounted NAS)
#   OFFSITE_METHOD        rsync (the only method). scp was removed: it cannot refuse to overwrite a same-named file on the far side
#   OFFSITE_SSH_KEY       optional private key file (owned by the service user, mode 0600); used with IdentitiesOnly
#   OFFSITE_KNOWN_HOSTS   known_hosts file for the destination (default /etc/upstream/offsite_known_hosts when it exists);
#                         host keys are always checked (StrictHostKeyChecking=yes), BatchMode=yes: never a prompt
#   OFFSITE_SSH_OPTS      extra ssh options, e.g. "-p 2222"
#   BACKUP_DIR            where snapshots are written (default /var/lib/upstream/backups)
#   UPSTREAM_ADMIN        admin wrapper used for verify-backup (default upstream-admin)
#
#   upstream-offsite --check     exit 0 when OFFSITE_DEST is set, 1 when not (the unit's ExecCondition: unset = skipped quietly)
#
# Steps: pick the newest snapshot (by modification time) -> verify-backup it (integrity_check, schema present) -> copy it, never
# overwriting a file of the same name on the far side -> compare content with --checksum. Old snapshots on the far side
# are never deleted by this script (prune there, or set up your own retention).
# Exit codes: 0 copied (or already there), 1 failed / nothing to copy / not configured under --check, 2 usage or configuration error.
set -eu

BACKUP_DIR=${BACKUP_DIR:-/var/lib/upstream/backups}
ADMIN=${UPSTREAM_ADMIN:-upstream-admin}
METHOD=${OFFSITE_METHOD:-rsync}
DEST=${OFFSITE_DEST:-}
KEY=${OFFSITE_SSH_KEY:-}
KNOWN=${OFFSITE_KNOWN_HOSTS:-/etc/upstream/offsite_known_hosts}
SSH_EXTRA=${OFFSITE_SSH_OPTS:-}

say() { printf '%s\n' "offsite: $*"; }
die() { printf '%s\n' "offsite: $*" >&2; exit "${2:-1}"; }

case ${1:-} in
  --check) [ -n "$DEST" ]; exit $? ;;
  '') ;;
  *) die "usage: upstream-offsite [--check]" 2 ;;
esac

[ -n "$DEST" ] || die "OFFSITE_DEST is not set: off-box copy is not configured (docs/DEPLOY.md section 7)" 2
case $METHOD in
  rsync) ;;
  scp) die "OFFSITE_METHOD=scp is not supported any more: scp cannot refuse to overwrite a file of the same name on the far side. Use rsync (the far side needs rsync, or a forced rrsync command)" 2 ;;
  *) die "OFFSITE_METHOD must be rsync, not '$METHOD'" 2 ;;
esac
[ -d "$BACKUP_DIR" ] || die "no backup directory $BACKUP_DIR"

# newest snapshot by mtime (names sort by time too, but a same-second '-1' file would sort wrongly)
file=
for f in $(ls -1t "$BACKUP_DIR" 2>/dev/null); do
  case $f in upstream-[0-9]*T[0-9]*Z.db|upstream-[0-9]*T[0-9]*Z-[0-9]*.db) file=$BACKUP_DIR/$f; break ;; esac
done
[ -n "$file" ] || die "no snapshot (upstream-*.db) in $BACKUP_DIR: nothing to copy"
"$ADMIN" verify-backup "$file" >/dev/null || die "newest snapshot $file does not verify: NOT copied (the daily backup unit should have failed too: journalctl -u upstream-backup)"

SSH_OPTS="-o BatchMode=yes -o StrictHostKeyChecking=yes"
[ -z "$KEY" ] || SSH_OPTS="$SSH_OPTS -i $KEY -o IdentitiesOnly=yes"
[ ! -f "$KNOWN" ] || SSH_OPTS="$SSH_OPTS -o UserKnownHostsFile=$KNOWN"
[ -z "$SSH_EXTRA" ] || SSH_OPTS="$SSH_OPTS $SSH_EXTRA"
SSH="ssh $SSH_OPTS"                 # (paths in these settings must not contain spaces)
dest=${DEST%/}/
name=$(basename "$file")

# --ignore-existing: a snapshot name is a moment in time, a same-named file on the far side is never replaced
rsync -t --chmod=F600 --ignore-existing -e "$SSH" "$file" "$dest" || die "rsync to $DEST failed"
# content check of what is over there now (empty output = identical bytes)
diff=$(rsync -c -n -i -e "$SSH" "$file" "$dest" 2>&1) || die "could not re-read $DEST to compare: $diff"
[ -z "$diff" ] || die "far-side copy of $name differs from the verified local snapshot ($diff): a different file of that name already exists there"
say "ok: $name is on $DEST (checksum compared)"
