Files
bookstore/deploy/backup.sh
T
twooeyandClaude Sonnet 5 b43852e733 Switch deploy pipeline to Gitea polling; fix env-parsing and volume-isolation bugs
- Replace the bare-repo post-receive hook with deploy/poll-deploy.sh: Gitea
  and the Docker hosts are separate machines, so each box polls its branch
  via host crontab instead of needing an exposed webhook receiver.
- Add Blocksy theme + Blocksy Companion auto-install to deploy.sh (free
  tier; the paid Book Store starter site still needs a manual license step).
- Fix deploy.sh/backup.sh/restore.sh sourcing .env files as bash: a bcrypt
  hash's `$2a$14$...` shape breaks under `set -u`. Replaced with
  deploy/lib/env.sh, a literal (non-executing) KEY=VALUE reader.
- Fix docker compose itself mangling the same kind of value: both
  `environment: ${VAR}` and `env_file:` run values through Compose's
  interpolation, which silently blanks `$identifier`-shaped substrings.
  The staging basic-auth hash is now rendered directly into the Caddyfile
  by deploy.sh, bypassing Compose's variable system entirely.
- Fix dev/staging/production silently sharing one Compose project (and
  therefore one db_data volume) by pinning an explicit -p per environment.
- cron and wordpress now share one environment anchor so they can't drift
  apart again (cron was silently missing WORDPRESS_CONFIG_EXTRA before).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-27 10:10:12 -04:00

43 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Off-host database + uploads backup (design doc §01, launch gate: "A full
# backup has been restored successfully into staging" — see restore.sh).
set -euo pipefail
ENVIRONMENT="${1:?Usage: backup.sh <staging|production>}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
ENV_FILE=".env.${ENVIRONMENT}"
# shellcheck source=lib/env.sh
source "$REPO_ROOT/deploy/lib/env.sh"
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
BACKUP_DIR="$(env_get "$ENV_FILE" BACKUP_DIR)"
BACKUP_REMOTE="$(env_get "$ENV_FILE" BACKUP_REMOTE)"
BACKUP_RETENTION_DAYS="$(env_get "$ENV_FILE" BACKUP_RETENTION_DAYS)"
BACKUP_DIR="${BACKUP_DIR:-./backups}/${ENVIRONMENT}"
mkdir -p "$BACKUP_DIR"
COMPOSE="docker compose -p bookstore-${ENVIRONMENT} -f docker-compose.yml -f docker-compose.${ENVIRONMENT}.yml --env-file ${ENV_FILE}"
echo "==> dumping database"
$COMPOSE exec -T db sh -c "exec mysqldump -u\"\$MARIADB_USER\" -p\"\$MARIADB_PASSWORD\" \"\$MARIADB_DATABASE\"" \
| gzip > "$BACKUP_DIR/db-${TIMESTAMP}.sql.gz"
echo "==> archiving uploads"
$COMPOSE run --rm -T wordpress tar -czf - -C /var/www/html/wp-content uploads \
> "$BACKUP_DIR/uploads-${TIMESTAMP}.tar.gz"
if [[ -n "${BACKUP_REMOTE:-}" ]]; then
echo "==> syncing to off-host storage ($BACKUP_REMOTE)"
rclone copy "$BACKUP_DIR/db-${TIMESTAMP}.sql.gz" "$BACKUP_REMOTE/${ENVIRONMENT}/"
rclone copy "$BACKUP_DIR/uploads-${TIMESTAMP}.tar.gz" "$BACKUP_REMOTE/${ENVIRONMENT}/"
else
echo "==> BACKUP_REMOTE not set — backup stayed local only; configure rclone before launch"
fi
echo "==> pruning local backups older than ${BACKUP_RETENTION_DAYS:-14} days"
find "$BACKUP_DIR" -type f -mtime "+${BACKUP_RETENTION_DAYS:-14}" -delete
echo "==> backup complete: $BACKUP_DIR"