Nightly Sanitized PostgreSQL Backups to AWS S3, Cloudflare R2 and DigitalOcean Spaces

Developers need a fresh, realistic database to work against, but nobody should be pulling dumps from production by hand. The fix is a nightly job that exports an anonymized copy and drops it in object storage, ready for anyone to restore.

This guide covers two ways to do it: a free GitHub Actions job using the open-source CLI, and a self-hosted setup that syncs to several clouds at once.

What a good sanitized backup job does

  • Reads from a read-only user or replica, in one consistent snapshot.
  • Replaces personal data with deterministic fakes, so joins and foreign keys keep working.
  • Produces a compressed SQL file you can restore with plain psql.
  • Uploads it to storage with a predictable name and prunes old copies.
  • Tells you when it fails.

Option 1: GitHub Actions and the free CLI

The Cloud-DB-Anonymizer CLI can sanitize and upload in one command. Add this workflow to a repository that contains your rules.json:

name: Nightly sanitized dump
on:
  schedule: [{ cron: "15 3 * * *" }]
  workflow_dispatch:
jobs:
  dump:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: { python-version: "3.12" }
      - run: sudo apt-get install -y postgresql-client && pip install -r requirements.txt
      - run: python main.py sanitize --rules rules.json --upload ${{ vars.BACKUP_BUCKET }}
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
          ANON_SALT: ${{ secrets.ANON_SALT }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: us-east-1

For Cloudflare R2 or DigitalOcean Spaces, set S3_ENDPOINT_URL, for example https://<ACCOUNT_ID>.r2.cloudflarestorage.com or https://fra1.digitaloceanspaces.com, and use that provider's access keys.

Make sure the runner can reach your database. Managed databases often need an IP allow-list or a bastion host for CI runners.

Restoring a backup

aws s3 cp s3://YOUR_BUCKET/sanitized_production_20261029T031500Z.sql.gz - \
  | gunzip | psql "$STAGING_DATABASE_URL"

Choosing a storage provider

Provider Good for Endpoint setting
AWS S3 Teams already on AWS, IAM policies, lifecycle rules Default (no endpoint needed)
Cloudflare R2 No egress fees when developers download dumps https://<ACCOUNT_ID>.r2.cloudflarestorage.com
DigitalOcean Spaces Simple flat pricing, teams on DigitalOcean https://<region>.digitaloceanspaces.com

Keeping a copy in two providers protects you if one account is locked or misconfigured.

Option 2: Self-hosted, multi-cloud, with alerts

A CI job is fine until you want history, retries, several destinations and someone to be told when it breaks. The Cloud-DB-Anonymizer Pro Suite runs on your own server with docker compose up -d and adds:

  • A cron scheduler for any interval.
  • Parallel upload to AWS S3, Cloudflare R2 and DigitalOcean Spaces, with retention pruning.
  • A web dashboard showing every run, its size, row counts and which clouds succeeded.
  • Telegram and Discord alerts on success or failure.
  • A rules editor that validates rules.json before saving.

Your data and cloud keys stay on your infrastructure. If you set this up for clients, the Agency License covers unlimited client deployments.

Checklist before you turn it on

  • The database user is read-only.
  • The anonymization salt and cloud keys are stored as secrets.
  • The bucket is private and has a retention policy.
  • Someone receives failure alerts.
  • You have tested a full restore at least once.

New to anonymization? Start with our guide to anonymizing a PostgreSQL database for staging.