142 lines
6.1 KiB
Bash
Executable File
142 lines
6.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# restore.sh — brings a sightbox deployment back from an R2 backup onto any
|
|
# Linux VPS, including a brand-new one from a different provider. The only
|
|
# real prerequisite is Docker + Docker Compose — installed here via
|
|
# Docker's own official script, which is the same across any modern
|
|
# distro, so nothing about this is tied to a specific Ubuntu version.
|
|
#
|
|
# Usage:
|
|
# ./restore.sh <target-root> [backup-object-path]
|
|
#
|
|
# <target-root> Absolute path this deployment should live at,
|
|
# e.g. /opt/sightbox — the archive's app/ and
|
|
# data/ get placed at <target-root>/app and
|
|
# <target-root>/data.
|
|
# [backup-object-path] Optional: an exact object key under the R2
|
|
# bucket's daily/ or weekly/ prefix (e.g.
|
|
# "daily/sightbox-backup-2026-09-16T03-00-00Z.tar.gz").
|
|
# Omit to restore the single most recent backup
|
|
# across both prefixes.
|
|
#
|
|
# R2 credentials are read the same way backup.sh reads them: from a
|
|
# backup/.env file. Since this script may be run before any of the repo
|
|
# exists on this new host, point RESTORE_ENV_FILE at wherever you've
|
|
# placed that one file ahead of time (e.g. scp'd over separately) —
|
|
# defaults to ./backup/.env next to this script, same as backup.sh.
|
|
|
|
set -euo pipefail
|
|
|
|
if [[ $# -lt 1 ]]; then
|
|
echo "Usage: $0 <target-root> [backup-object-path]" >&2
|
|
exit 1
|
|
fi
|
|
|
|
TARGET_ROOT="$1"
|
|
REQUESTED_OBJECT="${2:-}"
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
ENV_FILE="${RESTORE_ENV_FILE:-${HERE}/.env}"
|
|
|
|
if [[ ! -f "${ENV_FILE}" ]]; then
|
|
echo "restore.sh: ${ENV_FILE} not found. Either place backup/.env next to this script," >&2
|
|
echo " or set RESTORE_ENV_FILE=/path/to/.env before running." >&2
|
|
exit 1
|
|
fi
|
|
# shellcheck disable=SC1090
|
|
set -a; source "${ENV_FILE}"; set +a
|
|
|
|
: "${R2_ACCOUNT_ID:?R2_ACCOUNT_ID must be set in ${ENV_FILE}}"
|
|
: "${R2_ACCESS_KEY_ID:?R2_ACCESS_KEY_ID must be set in ${ENV_FILE}}"
|
|
: "${R2_SECRET_ACCESS_KEY:?R2_SECRET_ACCESS_KEY must be set in ${ENV_FILE}}"
|
|
: "${R2_BUCKET:?R2_BUCKET must be set in ${ENV_FILE}}"
|
|
|
|
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
|
|
RCLONE_FLAGS=(--s3-no-check-bucket)
|
|
|
|
# Step 1: Docker + Compose, if not already present. Idempotent — get.docker.com
|
|
# itself no-ops cleanly on a host that already has Docker installed.
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
echo "restore.sh: Docker not found — installing via Docker's official script..."
|
|
curl -fsSL https://get.docker.com | sh
|
|
fi
|
|
if ! docker compose version >/dev/null 2>&1; then
|
|
echo "restore.sh: 'docker compose' plugin not found after install — see" >&2
|
|
echo " https://docs.docker.com/compose/install/ and re-run this script." >&2
|
|
exit 1
|
|
fi
|
|
if ! command -v rclone >/dev/null 2>&1; then
|
|
echo "restore.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
|
|
|
|
# Step 2: figure out which object to restore.
|
|
if [[ -n "${REQUESTED_OBJECT}" ]]; then
|
|
OBJECT_PATH="${REQUESTED_OBJECT}"
|
|
else
|
|
echo "restore.sh: no backup specified — finding the most recent across daily/ and weekly/..."
|
|
# Sort key must be the filename ALONE (its embedded ISO-8601 timestamp
|
|
# sorts correctly as a plain string) — confirmed the hard way while
|
|
# building this script: prefixing "daily/"/"weekly/" before sorting
|
|
# makes the prefix itself dominate the comparison ("weekly/" > "daily/"
|
|
# lexically, regardless of date), silently picking the wrong backup.
|
|
# Tab-separating filename from its prefixed path and sorting on the
|
|
# first field only avoids that.
|
|
OBJECT_PATH="$(
|
|
{ rclone lsf "r2:${R2_BUCKET}/daily/" "${RCLONE_FLAGS[@]}" 2>/dev/null | awk '{print $0"\tdaily/"$0}'
|
|
rclone lsf "r2:${R2_BUCKET}/weekly/" "${RCLONE_FLAGS[@]}" 2>/dev/null | awk '{print $0"\tweekly/"$0}'
|
|
} | sort | tail -n1 | cut -f2
|
|
)"
|
|
if [[ -z "${OBJECT_PATH}" ]]; then
|
|
echo "restore.sh: no backups found in r2:${R2_BUCKET}/{daily,weekly}/" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
echo "restore.sh: restoring ${OBJECT_PATH}"
|
|
|
|
# Step 3: download, then unpack directly into place.
|
|
WORK_DIR="$(mktemp -d)"
|
|
cleanup() { rm -rf "${WORK_DIR}"; }
|
|
trap cleanup EXIT
|
|
|
|
rclone copy "r2:${R2_BUCKET}/${OBJECT_PATH}" "${WORK_DIR}/" "${RCLONE_FLAGS[@]}"
|
|
ARCHIVE_PATH="${WORK_DIR}/$(basename "${OBJECT_PATH}")"
|
|
|
|
mkdir -p "${TARGET_ROOT}"
|
|
echo "restore.sh: unpacking into ${TARGET_ROOT}..."
|
|
tar -xzf "${ARCHIVE_PATH}" -C "${TARGET_ROOT}"
|
|
|
|
APP_DIR="${TARGET_ROOT}/app"
|
|
DATA_DIR="${TARGET_ROOT}/data"
|
|
|
|
if [[ ! -f "${APP_DIR}/docker-compose.yml" ]]; then
|
|
echo "restore.sh: ${APP_DIR}/docker-compose.yml not found after unpacking — archive layout unexpected." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Every bind-mount target directory must exist before `docker compose up` —
|
|
# the archive already contains them (they came from a real $SIGHTBOX_DATA_DIR),
|
|
# but on the off chance one's missing, create it rather than fail obscurely.
|
|
mkdir -p "${DATA_DIR}"/{sites,db-data,redis-queue-data,billing-state}
|
|
|
|
# tenants.json travels inside $SIGHTBOX_DATA_DIR already (see backup.sh's
|
|
# docstring) — nothing further to restore for it. backend/.env was retired
|
|
# in favor of plain Compose/Dokploy environment variables, so there's no
|
|
# file-based equivalent to restore for backend-billing's own config either.
|
|
|
|
echo "restore.sh: bringing the stack up..."
|
|
SIGHTBOX_DATA_DIR="${DATA_DIR}" docker compose \
|
|
-f "${APP_DIR}/docker-compose.yml" \
|
|
-f "${APP_DIR}/docker-compose.data-dir.yml" \
|
|
--project-directory "${APP_DIR}" \
|
|
up -d
|
|
|
|
echo "restore.sh: done. Verify with:"
|
|
echo " docker compose -f ${APP_DIR}/docker-compose.yml -f ${APP_DIR}/docker-compose.data-dir.yml ps"
|