NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / DOCKER RECIPES / COMPOSE WITH S3 BACKUPS

Compose with S3 backups

The server backs itself up continuously to S3-compatible object storage: a full snapshot on a schedule plus the write-ahead log every minute, so you can restore to the latest state or to a point in time. This recipe runs a RustFS object store beside it so it works on one machine; point it at AWS S3, Cloudflare R2, Backblaze B2, Wasabi or MinIO for real off-site backups.

What you get

  • rustfs: S3-compatible object storage on the project network, its objects on the volume rasterizeddb-s3_s3data.
  • buckets: a one-shot container that creates the backup bucket if it is missing, then exits.
  • rasterizeddb: the server, backing up every database it holds to s3://rasterizeddb-backup/main/:
    • a snapshot as soon as the prefix is empty, then one a day, the last 7 kept;
    • the log archived every 60 seconds, so a lost host loses at most about a minute of writes;
    • --s3-start-offline, so the server still starts when the bucket is not reachable yet, holding the log on its own disk until it is.

Prerequisites

  • Docker with the Compose plugin.
  • psql to check the result.

What to edit

  1. Copy .env.example to .env.
  2. Set RASTERIZEDDB_PG_PASSWORD, S3_ACCESS_KEY and S3_SECRET_KEY. The S3 keys become RustFS's root credentials and the server signs its requests with them. Use letters, digits, - and _ only, because the bucket-creation step puts them in a URL.
  3. Optional: the schedule, in compose.yaml:
FlagHereDefaultWhat it does
--s3-bucketrasterizeddb-backupnoneSetting it turns backups on
--s3-prefixmainemptyKey prefix in the bucket. One database installation per prefix.
--s3-path-styleonoffhost/bucket/key addressing, needed by RustFS and MinIO
--s3-wal-flush-secs60300How often the log is archived: the most recent history a lost host can lose
--s3-snapshot-interval-secs8640086400Time between full snapshots; 0 means manual only
--s3-retain-snapshots77Snapshots kept, oldest deleted first; 0 keeps all
--s3-start-offlineonoffStart even when the bucket does not answer

The credentials, region and endpoint come from the environment (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, AWS_ENDPOINT_URL), which compose.yaml fills from .env, so they never appear on the server's command line. Every other flag is on Backup and restore.

Backing up to AWS S3 or another provider

  1. Create the bucket with your provider, and credentials limited to the prefix with these permissions: s3:PutObject, s3:GetObject, s3:DeleteObject, s3:AbortMultipartUpload and s3:ListBucketMultipartUploads.
  2. In compose.yaml, delete the rustfs and buckets services, the depends_on block of rasterizeddb, and the s3data volume. For AWS S3, also delete --s3-path-style.
  3. In .env, set S3_ACCESS_KEY, S3_SECRET_KEY, BACKUP_BUCKET and S3_REGION. For AWS S3 set S3_ENDPOINT= (empty) so the regional AWS endpoint is used; for any other provider set its endpoint URL.

Also give the bucket a lifecycle rule that removes incomplete multipart uploads after a few days. A server that is killed during an upload and never restarted leaves its parts behind.

Run it

sh
docker compose up -d
docker compose ps

buckets shows as exited (0) once the bucket exists. That is expected.

Check that it works

Write something, take a snapshot now, and list what the bucket holds:

sh
psql "host=127.0.0.1 port=5432 user=rasterized dbname=postgres sslmode=require" \
  -c "CREATE TABLE orders (id INT PRIMARY KEY, total NUMERIC(10,2))" \
  -c "INSERT INTO orders VALUES (1, 19.99), (2, 5.00)" \
  -c "BACKUP DATABASE" \
  -c "SHOW BACKUPS" \
  -c "SHOW BACKUP STATUS"

SHOW BACKUPS lists one row per snapshot and per archived log bundle. SHOW BACKUP STATUS shows the service's state, including whether the bucket is reachable.

The other backup statements:

sql
BACKUP DATABASE;   -- a snapshot now, archived so it can be restored
BACKUP WAL;        -- archive the log up to this moment
BACKUP PRUNE;      -- apply the retention policy now

Prove a backup restores

--restore-to rebuilds the backup into a directory and exits, without touching the running server. Run it in a throwaway container:

sh
docker compose run --rm --no-deps rasterizeddb \
  --s3-bucket rasterizeddb-backup --s3-prefix main --s3-path-style \
  --restore-to /tmp/verify

It ends with a line that starts Restored snapshot and gives the snapshot, the number of files and log bundles replayed and the point it recovered to. The container and /tmp/verify are deleted when it exits.

Restore

Use this after losing the data volume, or to go back to an earlier point. The backup is rebuilt into a new, empty data volume, and the server then backs up under a new prefix.

  1. Stop and remove the server, and remove its data volume. If the volume may still be worth something, copy it first with Backing up the volume.

    sh
    docker compose stop rasterizeddb
    docker compose rm -f rasterizeddb
    docker volume rm rasterizeddb-s3_data
    docker compose up -d rustfs
  2. Rebuild the backup into a fresh volume. Compose creates the volume again, empty; the restore is built in the container's /tmp and copied in:

    sh
    docker compose run --rm --no-deps --entrypoint sh rasterizeddb -c \
      'rasterizeddb --s3-bucket rasterizeddb-backup --s3-prefix main --s3-path-style --restore-to /tmp/restore/db \
       && cp -a /tmp/restore/db/. /var/lib/rasterizeddb/ && sync'

    To stop at an earlier moment, add --restore-at-time '2026-11-27T09:55:00Z' after --restore-to /tmp/restore/db. It stops at the last log flush at or before that instant. --restore-snapshot <id> restores one snapshot from SHOW BACKUPS exactly.

  3. Give the restored server a new prefix. A restored database is a new installation as far as backups are concerned, and the server refuses to start on a prefix that holds another installation's backups. The old prefix keeps the backups you restored from. In .env:

    text
    BACKUP_PREFIX=restored-1
  4. Start it. It takes a first snapshot under the new prefix straight away:

    sh
    docker compose up -d

Stop and clean up

sh
docker compose down        # keeps both volumes: the data and the backups
docker compose down -v     # deletes the data AND the backups in RustFS

Limits

  • RustFS here sits on the same disk as the database. That protects against a dropped table or a bad deploy, not a lost disk. For real protection put the object store on another machine, or use a cloud provider.
  • An in-place restore (--restore-from-s3) does not work with the image's layout, because the restore is assembled next to /var/lib/rasterizeddb and that directory is the volume's mount point. Use the steps above.
  • If you remove the data volume and start the server again without restoring, it refuses to start: the prefix holds the previous installation's backups. Restore, or set a new BACKUP_PREFIX.
  • The restore needs free space in Docker's storage for two copies of the database while it runs: the rebuilt one in /tmp and the copy in the volume.

The files

compose.yaml · Docker ComposeDOWNLOAD ↓
# RasterizedDB with continuous backup to S3-compatible object storage, here a
# RustFS container beside it. Copy .env.example to .env first.
#
# To back up to AWS S3 or another provider instead, delete the `rustfs` and
# `buckets` services and the depends_on block, and set the AWS_* variables
# and the bucket in .env to your provider's (see README.md).
name: rasterizeddb-s3

services:
  # S3-compatible object storage (https://github.com/rustfs/rustfs).
  rustfs:
    image: rustfs/rustfs:1.0.6
    environment:
      RUSTFS_ACCESS_KEY: ${S3_ACCESS_KEY:?set S3_ACCESS_KEY in .env}
      RUSTFS_SECRET_KEY: ${S3_SECRET_KEY:?set S3_SECRET_KEY in .env}
    volumes:
      - s3data:/data
    # The S3 API (9000) and console (9001) stay on the project network.
    # Publish "127.0.0.1:9001:9001" to open the console from this machine.
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://127.0.0.1:9000/health/ready"]
      interval: 5s
      timeout: 5s
      retries: 24

  # Creates the backup bucket if it is missing, then exits.
  buckets:
    image: rustfs/rustfs:1.0.6
    entrypoint: ["/bin/sh", "-c"]
    command:
      - rc bucket create "local/$$BUCKET" --ignore-existing
    environment:
      BUCKET: ${BACKUP_BUCKET:-rasterizeddb-backup}
      RC_CONFIG_DIR: /tmp
      RC_HOST_local: http://${S3_ACCESS_KEY}:${S3_SECRET_KEY}@rustfs:9000
    depends_on:
      rustfs:
        condition: service_healthy
    restart: "no"

  rasterizeddb:
    image: ${RASTERIZEDDB_IMAGE:-milendenev/rasterizeddb:1.1.0}
    environment:
      RASTERIZEDDB_PG_PASSWORD: ${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD in .env}
      # Read by the server's S3 client: kept out of its command line.
      AWS_ACCESS_KEY_ID: ${S3_ACCESS_KEY}
      AWS_SECRET_ACCESS_KEY: ${S3_SECRET_KEY}
      AWS_REGION: ${S3_REGION:-us-east-1}
      AWS_ENDPOINT_URL: ${S3_ENDPOINT:-http://rustfs:9000}
    command:
      - --s3-bucket
      - ${BACKUP_BUCKET:-rasterizeddb-backup}
      - --s3-prefix
      - ${BACKUP_PREFIX:-main}
      # RustFS and MinIO need path-style addressing. Remove for AWS S3.
      - --s3-path-style
      # How much recent history a lost host can lose, in seconds.
      - --s3-wal-flush-secs
      - "60"
      # A full snapshot a day, the last 7 kept.
      - --s3-snapshot-interval-secs
      - "86400"
      - --s3-retain-snapshots
      - "7"
      # Start even when the bucket does not answer (after a host reboot,
      # RustFS may come up second); the log is held locally until it does.
      - --s3-start-offline
      - --log-level
      - warn
    ports:
      - "127.0.0.1:5432:5432"
      - "127.0.0.1:61170:61170"
    volumes:
      - data:/var/lib/rasterizeddb
    depends_on:
      buckets:
        condition: service_completed_successfully
    restart: unless-stopped
    stop_grace_period: 90s
    healthcheck:
      test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/5432"]
      interval: 10s
      timeout: 5s
      retries: 6
      start_period: 30s

volumes:
  data:
  s3data:
.env.example · Environment fileDOWNLOAD ↓
# Copy to .env next to compose.yaml and change every secret.

# The password of the role `rasterized`, used on the first start only.
RASTERIZEDDB_PG_PASSWORD=change-me-to-a-long-random-password

# Object storage credentials. With the bundled RustFS these become its root
# credentials. Letters, digits, - and _ only: they are also used in a URL.
S3_ACCESS_KEY=rdb-backup
S3_SECRET_KEY=change-me-too-at-least-16-chars

# Where the backups go. A prefix holds the backups of exactly one database:
# after a restore, set a new prefix (see README.md).
BACKUP_BUCKET=rasterizeddb-backup
BACKUP_PREFIX=main

# The bundled RustFS. For AWS S3, remove S3_ENDPOINT (or leave it empty)
# and set your bucket's region.
S3_ENDPOINT=http://rustfs:9000
S3_REGION=us-east-1

# The image and tag. Pin a full version.
RASTERIZEDDB_IMAGE=milendenev/rasterizeddb:1.1.0
✓ page rendered in 127 µs axum + tera + htmx · no other javascript