# Backup and Restore Procedure

This procedure is part of the production handoff. Backups must be encrypted, access-controlled, monitored and periodically restored into an isolated environment. A backup is not considered verified until a restore drill succeeds.

## MySQL backup

Use a dedicated backup account with the minimum privileges required by your hosting environment. Example:

```bash
STAMP=$(date -u +%Y%m%dT%H%M%SZ)
mysqldump \
  --single-transaction \
  --quick \
  --routines \
  --triggers \
  --events \
  --set-gtid-purged=OFF \
  -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USERNAME" -p \
  "$DB_DATABASE" | gzip > "smart-clinic-${STAMP}.sql.gz"
```

Do not put database passwords directly in shell history. Prefer a protected client config, secret manager, or backup platform.

## Application files

Patient documents, if enabled, belong on the configured private filesystem/S3 disk. Back them up independently with server-side encryption and retention/versioning. Public build assets can be regenerated and do not need the same retention policy.

## Restore drill

1. Create an isolated MySQL database with no production application traffic.
2. Restore the dump:

```bash
gunzip -c smart-clinic-YYYYMMDDTHHMMSSZ.sql.gz | \
  mysql -h "$RESTORE_DB_HOST" -P "${RESTORE_DB_PORT:-3306}" \
  -u "$RESTORE_DB_USERNAME" -p "$RESTORE_DB_DATABASE"
```

3. Point a temporary Smart Clinic deployment at the restored database.
4. Run `php artisan migrate:status` and confirm no unexpected pending production migrations.
5. Smoke-test login, bootstrap, patient search, doctor availability, appointment detail, queue, transactions and reports.
6. Validate row counts for critical tables (`patients`, `appointments`, `queue_entries`, `transactions`, `audit_logs`).
7. Record the backup timestamp, restore duration, tester and result.
8. Destroy the isolated restored copy according to the privacy policy.

## Retention and monitoring

Choose retention according to the clinic's legal jurisdiction and contracts. A common operational baseline is daily encrypted backups plus longer weekly/monthly retention, but legal requirements take precedence. Alert when a scheduled backup fails, backup size changes unexpectedly, encryption fails, or a restore drill has not been completed within the clinic's chosen interval.
