#!/bin/bash
# On-site second copy of cloud-primary B2 data (azkaban -> bulk/cloud-copy). See backups/BACKUPS.md "On-site copy".
# usage: cloud-copy-backup [source ...]   (default: all)   -- mounts each source read-only with rclone, restic-backs it up, unmounts.
# Secrets: /etc/cloud-copy/{rclone.conf,restic.pw} (root 0600). Nothing is printed.
set -u
export RCLONE_CONFIG=/etc/cloud-copy/rclone.conf RESTIC_REPOSITORY=/bulk/cloud-copy/restic RESTIC_PASSWORD_FILE=/etc/cloud-copy/restic.pw GOMEMLIMIT=2GiB XDG_CACHE_HOME=/var/cache/cloud-copy
# Home-link courtesy (owner 2026-11-01: bulk pulls stalled Plex in the evening). rclone timetable, host time (Pacific):
# 00:00-01:00 3 MB/s, 01:00-07:00 unlimited, 07:00-17:00 8 MB/s, 17:00-24:00 3 MB/s. Override: BWLIMIT=off cloud-copy-backup
BWLIMIT=${BWLIMIT:-"00:00,3M 01:00,off 07:00,8M 17:00,3M"}
MNT=/run/cloud-copy; STATE=/var/lib/ctg-backup-cloud; mkdir -p "$MNT" "$STATE"
declare -A SRC=(
  [nextcloud-canutethegreat]="nc-canutethegreat:"
  [forgejo]="b2-forgejo:ctg-forgejo"
  [storage]="b2-storage:ctg-storage"
  [files]="b2-files:ctg-files"
)
ORDER=(nextcloud-canutethegreat files storage forgejo)
[ $# -gt 0 ] && ORDER=("$@")
fail=0; skipped=0
for n in "${ORDER[@]}"; do
  m=$MNT/$n; mkdir -p "$m"
  if pgrep -f "restic backup $m " >/dev/null || mountpoint -q "$m"; then echo "$n: a run is already in progress, skipping"; skipped=1; continue; fi
  echo "== $n: mount"
  rclone mount "${SRC[$n]}" "$m" --read-only --daemon --daemon-wait 60s --vfs-cache-mode off --dir-cache-time 10m \
      --buffer-size 32M --transfers 4 --checkers 8 --bwlimit "$BWLIMIT" --allow-other --log-file /var/log/cloud-copy-rclone.log --log-level NOTICE || { echo "mount failed: $n"; fail=1; continue; }
  # guard: an empty mount must never become an "everything deleted" snapshot
  if [ -z "$(ls -A "$m" 2>/dev/null | head -1)" ]; then echo "mount of $n is empty, skipping"; fail=1; fusermount3 -uz "$m"; continue; fi
  # Transient Forgejo data needs no backup: actions_artifacts (CI artifacts, expire on their own; upload parts *.chunk live
  # minutes and vanish on merge), repo-archive (zip/tar downloads Forgejo regenerates on demand), and any *.tmp.
  # (2026-11-23 06:15: restic exit 3 on one 92-byte chunk, snapshot saved but unit "failed".) Not excluded: actions_log
  # (finished job logs are not regenerable), packages, attachments.
  # Exit 3 = snapshot saved but some files unreadable. If EVERY reported path is transient (vanished between listing and
  # read) it is success-with-warning; anything else is retried ONCE (parent snapshot makes pass two cheap), then fails.
  out=$(mktemp "$MNT/.restic-out.XXXXXX")
  for attempt in 1 2; do
    restic backup "$m" --host cloud-copy --tag "$n" --ignore-inode --ignore-ctime --read-concurrency "${RC:-2}" --exclude-caches \
        --exclude '**/actions_artifacts' --exclude '**/repo-archive' --exclude '**/*.chunk' --exclude '**/*.chunk.gz' \
        --exclude '**/*.tmp' > "$out" 2>&1
    rc=$?
    tail -15 "$out"
    [ $rc -ne 3 ] && break
    # error lines restic printed for unreadable files; real = those NOT on a transient path
    real=$(grep -E '^(error|Error):' "$out" | grep -vE '(/actions_artifacts/|/repo-archive/|\.chunk(\.gz)?([: ]|$)|\.tmp([: ]|$))' | wc -l)
    seen=$(grep -cE '^(error|Error):' "$out")
    if [ "$seen" -gt 0 ] && [ "$real" -eq 0 ]; then
      echo "restic backup $n exit 3: $seen unreadable path(s), all transient Forgejo data; snapshot saved, treating as success (warning)"
      rc=0; break
    fi
    echo "restic backup $n exit 3 ($real non-transient unreadable file(s)), attempt $attempt"; sleep 60
  done
  rm -f "$out"
  [ $rc -ne 0 ] && { echo "restic backup $n exit $rc"; fail=1; }
  fusermount3 -u "$m" 2>/dev/null || fusermount3 -uz "$m"
done
[ $fail -eq 0 ] && [ $skipped -eq 0 ] && date +%s > $STATE/last-backup-ok
exit $fail
