# Operations

## Single-VPS limitations (read this)

Everything (web, API, worker, Postgres, Redis, Caddy) runs on one machine. That's fine
for a pilot and dozens of small shops, with these consequences:

* **Single point of failure.** If the VPS or its disk dies, every client site is down until
  you restore onto a new server. Expect roughly 1–2 hours if you've practised the drill.
* **Data loss window.** Backups are nightly plus one before every deploy, so a disk failure
  can lose up to ~24h of orders. Before taking real orders, add continuous WAL archiving
  (e.g. pgBackRest to S3) or move Postgres to a managed database.
* **Deploys cause a few seconds of downtime** while containers restart.
* **Shared resources.** One busy shop (or a traffic spike) slows all shops. Builds also run
  on the box, so deploy at quiet times or move builds to CI images.
* **Let's Encrypt limits.** On-demand certificates are rate-limited per registered domain;
  fine for normal onboarding, but don't bulk-add hundreds of domains at once.
* **No off-box monitoring yet.** Add an external uptime checker (e.g. UptimeRobot,
  Better Stack) on `https://app.<domain>/api/health` and each live shop.

## Backups

* Nightly at 03:15 (cron, installed by setup-server.sh) and before every deploy.
* Encrypted with `BACKUP_PASSPHRASE`, uploaded to `BACKUP_S3_*`, last 7 days kept locally.
* **Keep `BACKUP_PASSPHRASE` and `APP_ENCRYPTION_KEY` in your password manager.** Without
  them a backup can't be read (or MFA secrets can't be decrypted).
* Set a lifecycle rule on the bucket (e.g. keep 30 daily, 12 monthly).

## Restore drill (monthly)

```bash
bash scripts/restore.sh backups/<latest>.dump.enc
```
Restores into a separate `restore_drill` database and prints row counts. Production is untouched.
Record the date and how long it took.

## Disaster recovery (new server)

1. New VPS → follow DEPLOY.md section 1, but copy your saved `.env` instead of generating one.
2. Download the latest backup from the bucket into `backups/`.
3. `docker compose up -d postgres`, then:
   ```bash
   set -a; source .env; set +a
   openssl enc -d -aes-256-cbc -pbkdf2 -iter 200000 -pass env:BACKUP_PASSPHRASE -in backups/<file> \
     | docker compose exec -T postgres pg_restore -U "$POSTGRES_USER" -d "$POSTGRES_DB" --clean --if-exists --no-owner
   ```
4. `bash scripts/deploy.sh`, then point DNS at the new IP.

## Logs

```bash
docker compose logs -f api worker web caddy
docker compose ps
```
