# Backup & Restore One self-contained nightly snapshot of the whole deployment — every client site, the Frappe bench, and this repo's own code — to Cloudflare R2, restorable onto any brand-new Linux VPS (any provider, any distro) with one script. ## Architecture **Everything persistent lives under one visible host directory**, not Docker's own opaque `/var/lib/docker/volumes/...` storage. `docker-compose.data-dir.yml` — layered on top of the base `docker-compose.yml` on the production host only — redirects every named volume (`sites`, `db-data`, `redis-queue-data`, `billing-state`) to `$SIGHTBOX_DATA_DIR` via a bind mount: ``` docker compose -f docker-compose.yml -f docker-compose.data-dir.yml up -d ``` ⚠ **Confirmed the hard way — never layer `docker-compose.data-dir.yml` on macOS/Windows dev machines.** Docker Desktop's bind mount runs the container's view of that directory through the *host's* filesystem semantics, and macOS's default filesystem is case-insensitive. MariaDB silently sets `lower_case_table_names=2` against a case-insensitive filesystem — which doesn't match how this data's tables were actually created — and every query starts failing with `doesn't exist in engine`. Docker's own named-volume storage (the base file's default, used for local dev) lives inside the Desktop VM's Linux filesystem regardless of host OS, so it never hits this. Only a host bind mount can. The production target is a real Linux VPS, where this is a completely standard, safe pattern — the case-insensitivity problem doesn't exist there at all. **The backup itself is a raw filesystem snapshot, not `bench backup`/`bench restore`.** `bench backup` is the right tool when you need a live, no-downtime, per-site backup — but a restore then has to loop over every client site individually, which is exactly the kind of per-site orchestration this design avoids. Instead: briefly stop every container that writes to `$SIGHTBOX_DATA_DIR`, tar the whole tree (plus the repo checkout itself, `$SIGHTBOX_APP_DIR`, including `backend/.env` and `backend/tenants.json` — real secrets, never committed to git) into one archive, restart. New client sites are automatically included in the very next backup, with zero script changes. Restore genuinely is "unpack, `docker compose up -d`" — no per-site knowledge required at all. **Consistency:** raw-copying MariaDB's datadir while MariaDB is actively writing to it risks a crash-inconsistent snapshot (confirmed while building this: InnoDB's own redo-log replay *usually* self-heals a live-copied datadir, but `backup.sh` doesn't rely on that — it stops the writer containers first, every time). ## First-time setup on the production host 1. Create the data directories (bind mounts don't auto-create them the way a named volume does): ``` mkdir -p /opt/sightbox/data/{sites,db-data,redis-queue-data,billing-state} ``` 2. Set `SIGHTBOX_DATA_DIR=/opt/sightbox/data` in the root `.env` (next to `docker-compose.yml`) — Compose reads it from there. 3. `cp backup/.env.example backup/.env`, fill in: - `SIGHTBOX_APP_DIR` / `SIGHTBOX_DATA_DIR` — same paths as above. - `R2_ACCOUNT_ID`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_BUCKET` — see "Cloudflare R2 credentials" below. - Retention days, if you want something other than the 14 daily / 8 weekly (56 days) default. 4. `docker compose -f docker-compose.yml -f docker-compose.data-dir.yml up -d` 5. Test one backup by hand: `./backup/backup.sh` — confirm it completes and the archive shows up in the R2 bucket's `daily/` prefix before trusting it to cron. 6. Add the cron job: ``` crontab -e # nightly at 3am server time 0 3 * * * /opt/sightbox/app/backup/backup.sh >> /var/log/sightbox-backup.log 2>&1 ``` ## Cloudflare R2 credentials — what to actually pull from the dashboard ⚠ **Confirmed the hard way:** R2's "Manage API Tokens" flow shows a `cfat_`-prefixed value as the headline "Token" — that's a general-purpose **Cloudflare API Token**, not S3-compatible, and rclone rejects it outright (`Credential access key has length 53, should be 32`). The actual S3-compatible **Access Key ID** (32 characters) and **Secret Access Key** (64 characters) are shown as *separate* fields on the same token-creation confirmation screen — look specifically for those, not the main token value. 1. Dashboard → **R2 → Overview** → note the **Account ID** (right sidebar, or in the URL). 2. **"Manage API Tokens"** → **Create API Token** → Permissions: **Object Read & Write**, scoped to **this one bucket only** (not "all buckets" — same least-privilege approach used for the Stripe restricted key elsewhere in this project). 3. Copy the **Access Key ID** and **Secret Access Key** shown — not the `cfat_...` token value. ## rclone — R2-specific flags that actually matter Confirmed against a real R2 bucket while building this: - **No `acl` setting.** R2 doesn't support S3 ACLs; setting one 400s every request. - **`region=auto`** — required; R2 doesn't have real AWS-style regions but the S3 API client still wants one set. - **`--s3-no-check-bucket`** — required for a bucket-scoped token. rclone's default behavior tries a `CreateBucket`/`ListBuckets` preflight a bucket-scoped token has no permission for (correctly rejected with 403 — this isn't a bug, it's the least-privilege token working as intended), which otherwise makes every single operation fail before it even starts. Both scripts set these via `RCLONE_CONFIG_R2_*` environment variables inline — no `~/.config/rclone/rclone.conf` needed, keeping every credential inside this repo's own gitignored `backup/.env`, the same pattern as `backend/.env`/`backend/tenants.json`. ## Restoring — including onto a brand-new VPS, different provider The only real prerequisite is Docker + Docker Compose. `restore.sh` installs both itself via Docker's own official script if they're missing — identical across any modern Linux distro, so nothing here is tied to a specific Ubuntu version. ``` # Place backup/.env somewhere on the new host first (scp it over, or paste # it manually) — restore.sh needs R2 credentials before anything else exists. RESTORE_ENV_FILE=/path/to/.env ./restore.sh /opt/sightbox ``` - Omit a specific backup and it restores the single most recent across both `daily/` and `weekly/` automatically. - To restore a specific date instead: `./restore.sh /opt/sightbox daily/sightbox-backup-2026-09-14T03-00-00Z.tar.gz` (exact object key, found via `rclone lsf r2:/daily/`). - Unpacks to `/app` and `/data`, then runs `docker compose -f app/docker-compose.yml -f app/docker-compose.data-dir.yml up -d` itself — nothing further to do. ⚠ **Confirmed the hard way, twice, while building the "most recent" lookup:** sorting `daily/` and `weekly/` paths together as full strings picks the wrong backup — `"weekly/"` sorts after `"daily/"` lexically regardless of which one is actually newer, so a `weekly/...2026-09-13...` backup would silently beat a `daily/...2026-09-16...` one. `restore.sh` sorts on the bare filename alone (its embedded ISO-8601 timestamp sorts correctly as a plain string) and only attaches the `daily/`/`weekly/` prefix to whichever one wins. ## Retention 14 days of `daily/` backups, 8 weeks (56 days) of `weekly/` backups (the Sunday backup is copied to both prefixes). `backup.sh` prunes both after every run via `rclone delete --min-age`, so retention needs no separate cron job. ## What's deliberately NOT backed up - **`redis-cache`** — no volume at all; it's meant to be fully rebuildable, restarting empty is correct, not a gap. - **`redis-queue`**'s in-flight jobs at the exact moment of a backup — a handful of background jobs mid-flight could be lost on restore. Low-risk at this project's current scale; revisit if that ever stops being true. ## Testing discipline A full backup → wipe → restore cycle on the real production Linux VPS (not just this doc's description of it) should be run at least once before relying on this for a real incident — matching this project's standing rule of verifying live rather than trusting an untested script. Everything in this document *except* the actual bind-mount-on-Linux step has been verified live against the real R2 bucket while building it (upload, download, list, delete, the retention prune, and the most-recent-backup lookup, including catching and fixing the sort bug above) — the one piece not verifiable from this session's own (macOS) environment is the full `docker-compose.data-dir.yml` + backup/restore cycle actually running on Linux, since that's deliberately never exercised on macOS dev machines at all (see the case-insensitivity gotcha above).