Gitfed
bastien-mrq/gitfed / deploy / backup.sh
#!/usr/bin/env bash
# Snapshots everything gitfed can't function without — the bbolt store,
# the instance's CA key, the SSH host key, and every bare repo, all under
# /data on the gitfed-data PVC (see deploy/k8s/README.md "Backups") — to a
# timestamped tarball downloaded into ./backups/ on this machine. Keeping
# the only copy on the VPS defeats the point: this is meant to survive
# losing the VPS entirely.
#
# Streams the tar straight off the running container (kubectl exec | ssh),
# so it needs no elevated access beyond what deploy/update.sh already uses,
# and doesn't depend on the underlying storage class's on-disk layout.
#
# This is a live copy, not a transactional snapshot — gitfed-server keeps
# running and writing while it's taken. bbolt is crash-consistent by
# design (the same guarantee as surviving a power loss mid-write), so this
# is safe, but a push landing in the exact instant of the tar could be
# split across two backups. Acceptable for a personal/small-team instance;
# not a substitute for a maintenance-window backup if that ever matters.
#
# Usage:
#   deploy/backup.sh              # snapshot now, prune to the 14 most recent
#   deploy/backup.sh --keep N     # snapshot now, prune to the N most recent
#   deploy/backup.sh --keep 0     # snapshot now, keep everything

set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."

VPS_HOST="${GITFED_VPS_HOST:-ubuntu@51.77.215.92}"
NAMESPACE="gitfed"
KEEP=14

if [ "${1:-}" = "--keep" ]; then
  KEEP="${2:?--keep needs a number}"
elif [ -n "${1:-}" ]; then
  echo "usage: $0 [--keep N]" >&2
  exit 1
fi

mkdir -p backups
timestamp="$(date -u +%Y%m%dT%H%M%SZ)"
out="backups/gitfed-$timestamp.tar.gz"
tmp="$out.part"

echo "==> archiving /data on $VPS_HOST (namespace $NAMESPACE)"
ssh "$VPS_HOST" "kubectl -n $NAMESPACE exec deployment/gitfed -c server -- tar czf - -C /data ." > "$tmp"
mv "$tmp" "$out"

size="$(du -h "$out" | cut -f1)"
echo "==> done: $out ($size)"

if [ "$KEEP" -gt 0 ]; then
  pruned="$(ls -1t backups/gitfed-*.tar.gz 2>/dev/null | tail -n +"$((KEEP + 1))")"
  if [ -n "$pruned" ]; then
    echo "==> pruning to the $KEEP most recent backups"
    echo "$pruned" | xargs -I{} rm -v -- {}
  fi
fi