sightbox/backup/backup.sh

114 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# backup.sh — nightly snapshot of the whole sightbox deployment (repo code +
# every client site's data) to Cloudflare R2. Meant to run from cron on the
# production Linux host, as a user with docker and filesystem permissions
# for $SIGHTBOX_APP_DIR/$SIGHTBOX_DATA_DIR.
#
# What this backs up, and why raw-copy instead of `bench backup`/`bench
# restore` per site: every persistent volume this deployment uses is
# bind-mounted under $SIGHTBOX_DATA_DIR (docker-compose.data-dir.yml) —
# briefly stopping the writer containers and tarring that whole tree,
# together with the app/ repo checkout itself (including backend/.env and
# backend/tenants.json, real secrets never committed to git), gives one
# self-contained, site-count-agnostic archive. Restoring never needs to
# know how many client sites exist or loop over them individually the way
# `bench restore` would — see restore.sh, which really is just "untar,
# docker compose up -d".
#
# Consistency: MariaDB's datadir must not be copied while MariaDB is
# writing to it (raw InnoDB files copied mid-write risk a crash-inconsistent
# snapshot) — confirmed the hard way during this design's own local testing
# that InnoDB *can* usually self-heal via redo-log replay, but this script
# doesn't rely on that: it stops every writer container first.
#
# Usage: backup/backup.sh (reads backup/.env next to this script)
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="${HERE}/.env"
if [[ ! -f "${ENV_FILE}" ]]; then
echo "backup.sh: ${ENV_FILE} not found — copy backup/.env.example to backup/.env and fill it in." >&2
exit 1
fi
# shellcheck disable=SC1090
set -a; source "${ENV_FILE}"; set +a
: "${SIGHTBOX_APP_DIR:?SIGHTBOX_APP_DIR must be set in backup/.env}"
: "${SIGHTBOX_DATA_DIR:?SIGHTBOX_DATA_DIR must be set in backup/.env}"
: "${R2_ACCOUNT_ID:?R2_ACCOUNT_ID must be set in backup/.env}"
: "${R2_ACCESS_KEY_ID:?R2_ACCESS_KEY_ID must be set in backup/.env}"
: "${R2_SECRET_ACCESS_KEY:?R2_SECRET_ACCESS_KEY must be set in backup/.env}"
: "${R2_BUCKET:?R2_BUCKET must be set in backup/.env}"
RETENTION_DAILY_DAYS="${RETENTION_DAILY_DAYS:-14}"
RETENTION_WEEKLY_DAYS="${RETENTION_WEEKLY_DAYS:-56}"
# The exact writer set: everything that touches sites/db-data/redis-queue-data/
# billing-state. redis-cache and configurator are deliberately excluded —
# redis-cache holds nothing persistent (no volume at all) and configurator
# is a one-shot init container that already exited.
WRITER_SERVICES=(backend frontend websocket queue-short queue-long scheduler db redis-queue backend-billing)
COMPOSE_FILES=(-f "${SIGHTBOX_APP_DIR}/docker-compose.yml" -f "${SIGHTBOX_APP_DIR}/docker-compose.data-dir.yml")
TIMESTAMP="$(date -u +%Y-%m-%dT%H-%M-%SZ)"
ARCHIVE_NAME="sightbox-backup-${TIMESTAMP}.tar.gz"
WORK_DIR="$(mktemp -d)"
ARCHIVE_PATH="${WORK_DIR}/${ARCHIVE_NAME}"
cleanup() {
rm -rf "${WORK_DIR}"
}
trap cleanup EXIT
echo "backup.sh: stopping writer containers..."
docker compose "${COMPOSE_FILES[@]}" stop "${WRITER_SERVICES[@]}"
echo "backup.sh: archiving ${SIGHTBOX_APP_DIR} (as app/) and ${SIGHTBOX_DATA_DIR} (as data/)..."
# Symlink-then-dereference (-h) trick: lets tar combine two arbitrary,
# unrelated absolute paths into one archive with clean, predictable
# top-level names ("app/", "data/") regardless of what the source
# directories are actually named or where they live — no --transform
# needed, which GNU tar and BSD tar (macOS, for local dev testing) implement
# differently.
ln -s "${SIGHTBOX_APP_DIR}" "${WORK_DIR}/app"
ln -s "${SIGHTBOX_DATA_DIR}" "${WORK_DIR}/data"
tar -czhf "${ARCHIVE_PATH}" \
--exclude=".git" \
--exclude="__pycache__" \
--exclude=".venv" \
--exclude="node_modules" \
--exclude=".pytest_cache" \
-C "${WORK_DIR}" app data
echo "backup.sh: restarting containers..."
docker compose "${COMPOSE_FILES[@]}" start "${WRITER_SERVICES[@]}"
echo "backup.sh: uploading to R2..."
export RCLONE_CONFIG_R2_TYPE=s3
export RCLONE_CONFIG_R2_PROVIDER=Cloudflare
export RCLONE_CONFIG_R2_ACCESS_KEY_ID="${R2_ACCESS_KEY_ID}"
export RCLONE_CONFIG_R2_SECRET_ACCESS_KEY="${R2_SECRET_ACCESS_KEY}"
export RCLONE_CONFIG_R2_ENDPOINT="https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com"
export RCLONE_CONFIG_R2_REGION=auto
# R2 doesn't support S3 ACLs (an explicit acl setting 400s) and a
# bucket-scoped API token can't CreateBucket/ListBuckets — confirmed
# against the real bucket while building this script. --s3-no-check-bucket
# skips rclone's default "does this bucket exist, create if not" preflight,
# which a scoped token has no permission for anyway.
RCLONE_FLAGS=(--s3-no-check-bucket --checksum)
rclone copy "${ARCHIVE_PATH}" "r2:${R2_BUCKET}/daily/" "${RCLONE_FLAGS[@]}"
if [[ "$(date -u +%u)" == "7" ]]; then
echo "backup.sh: Sunday — also copying to weekly/"
rclone copy "${ARCHIVE_PATH}" "r2:${R2_BUCKET}/weekly/" "${RCLONE_FLAGS[@]}"
fi
echo "backup.sh: pruning backups older than retention..."
rclone delete "r2:${R2_BUCKET}/daily/" --min-age "${RETENTION_DAILY_DAYS}d" "${RCLONE_FLAGS[@]}"
rclone delete "r2:${R2_BUCKET}/weekly/" --min-age "${RETENTION_WEEKLY_DAYS}d" "${RCLONE_FLAGS[@]}"
echo "backup.sh: done — ${ARCHIVE_NAME}"