---
title: "Backups & Recovery"
description: "Stream PostgreSQL backups to R2, schedule backups, protect database migrations, and support restoration."
---

> Documentation Index
> Fetch the complete documentation index at: https://flarelens.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Backups & Recovery

Stream PostgreSQL backups directly to Cloudflare R2 with zero local disk staging, schedule automated backups, protect database migrations with pre-deploy snapshots, and support verified point-in-time restoration.

## Zero-Disk Streaming Architecture (`io.Pipe` → R2)

Traditional backup scripts stage multi-gigabyte dumps on local NVMe disks before uploading—risking `ENOSPC` crashes on production database hosts. `flarelensd` executes `pg_dump` inside the target PostgreSQL container, pipes stdout through streaming `gzip` compression in Go memory (`io.Pipe`), and uploads multipart chunks straight to your R2 `BACKUP_BUCKET` via short-lived presigned URLs.

- **Constant ~16 MB Memory Footprint**: Multi-gigabyte databases stream directly to R2 with zero temporary files written to host storage.
- **Multi-Environment DB Resolution**: Automatically resolves the right database container and target database name (`DATABASE_URL`, `POSTGRES_DB`, or `*_DB_NAME`) across `uat` and `prod` environments on shared or dedicated hosts.
- **Verified Metadata Catalog**: Every completed snapshot records byte size, duration, SHA-256 checksum, environment, and revision lineage in D1.

## Pre-Migration Guardrails & Automated Rollback

Pair `pre_deploy_snapshot: true` with `run_migrations: true` on stateful application manifests so every schema change is backed by a fresh R2 restore point:

```yaml title="Automated pre-migration R2 snapshot guardrail"
name: ledger-api
env: prod
strategy: blue_green
image: ghcr.io/acme/ledger-api:v2.1.0

pre_deploy_snapshot: true
run_migrations: true
migration_command: ["goose", "-dir", "/app/migrations", "up"]
```

> **Automatic failure recovery**
>
> If `migration_command` exits non-zero or the candidate slot fails HTTP readiness probes after altering schemas, FlareLens aborts the cutover, keeps the live slot serving traffic, and can stream-restore the pre-deploy R2 snapshot automatically.

## Declarative Scheduled Cron Policies & R2 Retention (`backup:`)

Declare automated UTC backup schedules and R2 retention windows directly inside stateful manifests alongside `jobs:`:

```yaml title="fleet/prod/postgres.yml"
service: postgres
env: prod
mode: singleton
image: postgres:18
container_name: app-prod-postgres

backup:
  schedule: "0 2 * * *"       # Daily at 02:00 UTC
  retain_days: 30             # Automatically prune R2 snapshots older than 30 days
  prefix: postgres/prod
  databases:
    - core_prod
    - workflow_prod
```

- **1-Minute Edge Cron Evaluation**: The Control Plane evaluates `backup.schedule` every minute in UTC with D1 slot deduplication (`backup_cron_slots`), queueing zero-disk streaming `pg_dump` runs for every configured database.
- **Automatic Retention Pruning**: Snapshots under `backup.prefix` older than `retain_days` are pruned automatically from Cloudflare R2 after scheduled runs, while manual snapshots and point-in-time restores remain available one click away in the **Backups** console view.

Source: https://flarelens.dev/docs/backups/index.mdx
