# BCP Backup and Disaster Recovery Runbook

## Scope

Back up the following state:

* The PostgreSQL 17.11 `bcp_control_panel` database. It contains all business data, Laravel sessions, cache entries, jobs, failed jobs, audit logs, and scheduler locks.
* The deployed application release matching the database schema: source revision or immutable build artifact, `composer.lock`, and migration files.
* Secret-manager values and certificate material required by the deployment: `APP_KEY`, database credentials and TLS material, mail credentials, object-storage credentials, API tokens, and webhook secrets. Store these in the secret-management system, never in this repository or an unencrypted backup archive.
* Application files only when they contain customer data. The configured `storage/app/private` and `storage/app/public` disks are currently empty except for ignore files, and no application code writes uploads. If uploads are enabled later, back up the active disk or object-store bucket with the same recovery point as the database.

Do not treat `storage/framework` or `storage/logs` as recovery data. They are runtime cache, sessions, locks, and logs; their durable records are already in PostgreSQL where applicable.

## Logical Backup Procedure

Use PostgreSQL 17 client tools compatible with the PostgreSQL 17.11 server. Provide credentials with a protected `PGPASSFILE`, workload identity, or an equivalent secret injection mechanism; do not place passwords on command lines.

```bash
export PGHOST="postgres.internal"
export PGPORT="5432"
export PGUSER="bcp_backup"
export PGSSLMODE="verify-full"
export PGSSLROOTCERT="/run/secrets/postgresql-ca.pem"
export PGPASSFILE="/run/secrets/bcp-backup.pgpass"

backup_timestamp="$(date -u +%Y%m%dT%H%M%SZ)"
pg_dump --host="$PGHOST" --port="$PGPORT" --username="$PGUSER" \
  --format=custom --compress=zstd --no-owner --no-privileges --verbose \
  --file="/secure-backups/bcp_control_panel_${backup_timestamp}.dump" \
  bcp_control_panel
```

For self-managed PostgreSQL, capture global roles separately because they are outside a per-database dump. This output is sensitive and must be encrypted and access-controlled:

```bash
pg_dumpall --host="$PGHOST" --port="$PGPORT" --username="$PGUSER" \
  --globals-only --file="/secure-backups/postgres_globals_${backup_timestamp}.sql"
```

After each backup, validate the command exit code, checksum the archive, record its PostgreSQL tool version and application release revision, then encrypt it before off-machine transfer. A managed PostgreSQL service may provide role and physical-backup handling through its control plane; document that provider-specific procedure alongside this runbook.

## Non-Destructive Restore Drill

Never restore over `bcp_control_panel`. Restore only to a newly created, isolated drill database and use a separate application configuration pointing to that database.

Use a separately injected recovery-operator identity for this procedure. The least-privilege `bcp_backup` identity used for dumps should not have permission to create databases or restore application objects.

```bash
export PGUSER="bcp_recovery_operator"
restore_database="bcp_control_panel_restore_drill_YYYYMMDD"
createdb --host="$PGHOST" --port="$PGPORT" --username="$PGUSER" "$restore_database"

pg_restore --host="$PGHOST" --port="$PGPORT" --username="$PGUSER" \
  --dbname="$restore_database" --no-owner --no-privileges --exit-on-error --verbose \
  "/secure-backups/bcp_control_panel_YYYYMMDDTHHMMSSZ.dump"

psql --host="$PGHOST" --port="$PGPORT" --username="$PGUSER" \
  --dbname="$restore_database" --command="SELECT count(*) AS applied_migrations FROM migrations;"
```

Validate the restored schema with an isolated application instance configured for the drill database, run `php artisan migrate:status`, and verify representative tenant, invoice, license, audit, and support records. Record the actual restore duration. The 2026-08-25 drill used this non-destructive pattern successfully: a PostgreSQL 17.11 custom zstd archive restored only to `bcp_control_panel_restore_drill`; source/restored table counts ($33$), migration counts ($23$), and safe key-row-count summaries matched; Laravel connected successfully; and the drill database plus temporary archive were removed afterward. The $2.37$-second measurement covers restore, validation, and cleanup only, not backup creation, archive encryption/transfer, or production-like application/file recovery.

## Production Recovery Procedure

1. Declare the incident and stop public application traffic, the scheduler, and queue workers before recovery cutover.
2. Preserve the incident state with a final backup when safe. Provision a new recovery database; do not overwrite the original database.
3. Restore the selected archive to the new database using the drill procedure. Restore application files or object-store data from the matching recovery point when uploads exist.
4. Deploy the application release that matches the restored migrations and inject the corresponding secret-manager values. Run `php artisan migrate:status` against the recovery database before cutover.
5. Validate authentication, tenant isolation, billing records, licenses, audit records, and scheduler configuration in the isolated environment.
6. Obtain incident approval, redirect traffic to the verified recovery environment, then restart scheduler and queue processes. Rotate credentials or secrets if incident scope requires it.

## Frequency, Retention, and Recovery Targets

* Create encrypted logical database backups at least every four hours. Keep seven daily, eight weekly, and twelve monthly recovery points, subject to contractual and regulatory retention requirements.
* Keep at least one immutable, encrypted off-machine/off-site copy in a separate account or region. Encryption keys must be managed separately from the backup storage account.
* Run and record a non-destructive restore drill at least quarterly and after a major PostgreSQL or migration change.
* Initial operational targets: $RPO \leq 4$ hours and $RTO \leq 4$ hours. Tighten these targets only after measured backup, restore, validation, and file-recovery timings support them.
* Logical backups alone cannot provide sub-interval point-in-time recovery. For a lower RPO, configure provider snapshots or PostgreSQL WAL archiving with a separately tested point-in-time recovery process.

## Deployment Requirements

* Schedule the dump, encryption, checksum, transfer, and alerting process outside the Laravel application. Verify failures alert an on-call operator.
* Grant the backup role only the database privileges required for dumps. Restrict restore credentials to approved operators and isolated recovery environments.
* When a file upload feature is introduced, add the active local disk or object-store bucket, encryption keys, retention/versioning, and restore verification to this runbook before production use.