Make backup/.env durable under Dokploy via BACKUP_ENV_FILE override

Confirmed the hard way, live, on the migrated deployment: backup.sh
stopped every writer container, archived everything, restarted
containers -- all successfully -- and only failed on "rclone: command
not found" that run. Re-running after the rclone self-install fix then
hit a second, deeper problem: backup/.env itself had been wiped by the
Dokploy redeploy in between, since it lived next to backup.sh inside the
git checkout. This is the exact backup/restore-vs-AutoDeploy
architectural tension flagged earlier in this project but never
resolved until now -- same category of bug as backend/.env and
tenants.json before it.

BACKUP_ENV_FILE (new, defaults to the old next-to-the-script path for
local dev / a traditional non-Dokploy VPS, where the checkout is
stable) lets a Dokploy deployment's cron entry point backup.sh at
$SIGHTBOX_DATA_DIR/backup.env instead -- durable, survives every
redeploy, same pattern as TENANTS_JSON_PATH. Documented in both
INSTALLATION_PLAYBOOK.md Stage 10 and BACKUP_RESTORE.md, including the
corrected cron entry (the Dokploy-managed checkout path, not the
traditional /opt/sightbox/app assumption the original cron example
baked in).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012pc2x7vyuUofdT3kLuqF3Y
This commit is contained in:
JasonFraser 2026-10-03 23:42:03 -04:00
parent 9faeeb7ee0
commit 8ac5f12acd
3 changed files with 56 additions and 12 deletions

View File

@ -30,10 +30,19 @@
set -euo pipefail set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="${HERE}/.env" # 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 if [[ ! -f "${ENV_FILE}" ]]; then
echo "backup.sh: ${ENV_FILE} not found — copy backup/.env.example to backup/.env and fill it in." >&2 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 exit 1
fi fi
# shellcheck disable=SC1090 # shellcheck disable=SC1090

View File

@ -51,13 +51,25 @@ writer containers first, every time).
2. Set `SIGHTBOX_DATA_DIR=/opt/sightbox/data` in the root `.env` (next to `docker-compose.yml`) 2. Set `SIGHTBOX_DATA_DIR=/opt/sightbox/data` in the root `.env` (next to `docker-compose.yml`)
— Compose reads it from there. — Compose reads it from there.
3. `cp backup/.env.example backup/.env`, fill in: 3. `cp backup/.env.example backup/.env`, fill in:
- `SIGHTBOX_APP_DIR` / `SIGHTBOX_DATA_DIR` — same paths as above. - `SIGHTBOX_APP_DIR` / `SIGHTBOX_DATA_DIR` — same paths as above. **Under Dokploy**,
`SIGHTBOX_APP_DIR` is the Dokploy-managed checkout (e.g.
`/etc/dokploy/compose/<project-id>/code`), not a traditional manual clone.
- `R2_ACCOUNT_ID`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_BUCKET` — see "Cloudflare - `R2_ACCOUNT_ID`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_BUCKET` — see "Cloudflare
R2 credentials" below. R2 credentials" below.
- Retention days, if you want something other than the 14 daily / 8 weekly (56 days) default. - Retention days, if you want something other than the 14 daily / 8 weekly (56 days) default.
⚠ **Confirmed the hard way — under Dokploy, this `backup/.env` file itself can't live next to
`backup.sh` inside the checkout**, the same way `backend/.env` and `tenants.json` once broke:
that whole directory is wiped and re-cloned on every AutoDeploy. Create it at
`$SIGHTBOX_DATA_DIR/backup.env` instead, and pass `BACKUP_ENV_FILE=$SIGHTBOX_DATA_DIR/backup.env`
to every `backup.sh` invocation (steps 5 below and the cron entry) — see
`INSTALLATION_PLAYBOOK.md` Stage 10. Not needed for a traditional, non-Dokploy VPS, where the
checkout is stable and the default next-to-the-script location is correct.
4. `docker compose -f docker-compose.yml -f docker-compose.data-dir.yml up -d` 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 5. Test one backup by hand (prefix with `BACKUP_ENV_FILE=...` under Dokploy, per above):
in the R2 bucket's `daily/` prefix before trusting it to cron. `./backup/backup.sh` — confirm it completes and the archive shows up in the R2 bucket's
`daily/` prefix before trusting it to cron. Its first-ever run also self-installs `rclone` if
missing, same as `restore.sh` already does.
6. Add the cron job: 6. Add the cron job:
``` ```
crontab -e crontab -e

View File

@ -539,22 +539,45 @@ deployment to Cloudflare R2, restorable onto any brand-new Linux VPS with one sc
and every gotcha found building it: [`BACKUP_RESTORE.md`](./BACKUP_RESTORE.md). and every gotcha found building it: [`BACKUP_RESTORE.md`](./BACKUP_RESTORE.md).
1. `cp backup/.env.example backup/.env`, fill in: 1. `cp backup/.env.example backup/.env`, fill in:
- `SIGHTBOX_APP_DIR` / `SIGHTBOX_DATA_DIR` — the same paths used in Stage 1. - `SIGHTBOX_APP_DIR` — **for a real Dokploy deployment, this is the Dokploy-managed checkout**
(e.g. `/etc/dokploy/compose/<project-id>/code`), not a traditional `/opt/sightbox/app` clone
— confirm the real path via this Compose app's own container labels
(`com.docker.compose.project.working_dir`) if unsure. `SIGHTBOX_DATA_DIR` — same path as
Stage 1 either way (e.g. `/opt/sightbox/data`).
- `R2_ACCOUNT_ID`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_BUCKET` — from Cloudflare's - `R2_ACCOUNT_ID`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_BUCKET` — from Cloudflare's
R2 dashboard. ⚠ The headline `cfat_`-prefixed value R2 shows first is a general Cloudflare R2 dashboard. ⚠ The headline `cfat_`-prefixed value R2 shows first is a general Cloudflare
API Token, not S3-compatible — rclone rejects it outright. The real Access Key ID API Token, not S3-compatible — rclone rejects it outright. The real Access Key ID
(32 characters) and Secret Access Key (64 characters) are separate fields on the same (32 characters) and Secret Access Key (64 characters) are separate fields on the same
screen; see `BACKUP_RESTORE.md` for exactly where to look. screen; see `BACKUP_RESTORE.md` for exactly where to look.
- Retention days, if you want something other than 14 daily / 8 weekly (56 days). - Retention days, if you want something other than 14 daily / 8 weekly (56 days).
2. 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. ⚠ **Confirmed the hard way:** on ⚠ **Confirmed the hard way — under Dokploy, `backup/.env` itself must NOT live next to
a brand-new VPS this first run installs `rclone` itself (same self-install `restore.sh` already `backup.sh` inside the checkout.** That whole directory gets wiped and re-cloned on every
did) — nothing to do manually, but the very first run takes a few seconds longer and prints an AutoDeploy, so a file placed there (the same way `backend/.env` and `tenants.json` once broke)
install log before the actual backup starts. is gone by the next deploy, or even the next cron tick after one. Instead, create it at
3. Add the cron job: `$SIGHTBOX_DATA_DIR/backup.env` (e.g. `/opt/sightbox/data/backup.env`) and point
`backup.sh` at it via `BACKUP_ENV_FILE` — see step 3's cron entry. Skip this relocation
entirely for local dev or a traditional, non-Dokploy VPS where the checkout is stable;
`backup/.env` next to the script is correct there.
2. Test one backup by hand and confirm it completes and the archive shows up in the R2 bucket's
`daily/` prefix before trusting it to cron:
```
BACKUP_ENV_FILE=/opt/sightbox/data/backup.env ./backup/backup.sh
```
(omit the `BACKUP_ENV_FILE=` prefix for local dev / a traditional VPS, where the default
next-to-the-script location is correct.) ⚠ **Confirmed the hard way:** on a brand-new VPS this
first run installs `rclone` itself (same self-install `restore.sh` already did) — nothing to
do manually, but the very first run takes a few seconds longer and prints an install log
before the actual backup starts.
3. Add the cron job — for a real Dokploy deployment, point it at the actual checkout path from
step 1 and set `BACKUP_ENV_FILE`:
``` ```
crontab -e crontab -e
# nightly at 3am server time # nightly at 3am server time
0 3 * * * BACKUP_ENV_FILE=/opt/sightbox/data/backup.env /etc/dokploy/compose/<project-id>/code/backup/backup.sh >> /var/log/sightbox-backup.log 2>&1
```
For local dev / a traditional, non-Dokploy VPS:
```
0 3 * * * /opt/sightbox/app/backup/backup.sh >> /var/log/sightbox-backup.log 2>&1 0 3 * * * /opt/sightbox/app/backup/backup.sh >> /var/log/sightbox-backup.log 2>&1
``` ```