140 lines
6.8 KiB
Bash
Executable File
140 lines
6.8 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) —
|
|
# including tenants.json itself (real secrets, never committed to git;
|
|
# TENANTS_JSON_PATH points here specifically so it survives a Dokploy
|
|
# AutoDeploy's git-checkout wipe, see docker-compose.yml's backend-billing
|
|
# service) — briefly stopping the writer containers and tarring that whole
|
|
# tree, together with the app/ repo checkout itself (backend/.env was
|
|
# retired the same way; its four values are now plain Dokploy/Compose
|
|
# environment variables, not a file), 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)"
|
|
# BACKUP_ENV_FILE, not always "${HERE}/.env" — confirmed the hard way, same
|
|
# category of bug as TENANTS_JSON_PATH: under Dokploy, this script's own
|
|
# directory is inside the git checkout that gets wiped and re-cloned on
|
|
# every AutoDeploy, so backup/.env (gitignored, real R2 secrets) never
|
|
# survives to the next deploy or even the next cron tick after one. Default
|
|
# (next to this script) is correct for local dev and any traditional,
|
|
# non-Dokploy VPS where the checkout is stable; a Dokploy deployment's cron
|
|
# entry must set BACKUP_ENV_FILE to an absolute path inside
|
|
# $SIGHTBOX_DATA_DIR instead — see INSTALLATION_PLAYBOOK.md Stage 10.
|
|
ENV_FILE="${BACKUP_ENV_FILE:-${HERE}/.env}"
|
|
|
|
if [[ ! -f "${ENV_FILE}" ]]; then
|
|
echo "backup.sh: ${ENV_FILE} not found — copy backup/.env.example there and fill it in (or set BACKUP_ENV_FILE to point at it)." >&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}"
|
|
|
|
# Confirmed the hard way: nothing installs rclone on a fresh VPS before this
|
|
# script's first run — restore.sh already self-installs it (mirrored here,
|
|
# same install.sh), but backup.sh didn't, so the very first scheduled run on
|
|
# a brand-new box failed after already stopping every writer container.
|
|
# Checked up front, before anything stateful happens below.
|
|
if ! command -v rclone >/dev/null 2>&1; then
|
|
echo "backup.sh: rclone not found — installing via rclone's official script..."
|
|
# Piped to bash, not sh — confirmed the hard way: Ubuntu/Debian's /bin/sh
|
|
# is dash (POSIX-only), and rclone's install script uses bash-specific
|
|
# syntax that fails under dash with a bare "Syntax error: ( unexpected".
|
|
curl -fsSL https://rclone.org/install.sh | bash
|
|
fi
|
|
|
|
# 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}"
|