PostgreSQL backup automation via CLI or containerized service, with encrypted/compressed uploads to Cloudflare R2 (S3-compatible), retention policies, Docker, and Railway cron jobs.
Go to file
BigDaddyAman 33da212b16 chores: bump boto3 from 1.40.36 ti 1.40.37 2025-09-24 21:56:01 +08:00
.gitignore Initial release: PostgreSQL backup bot with R2 support 2025-09-13 21:38:54 +08:00
Dockerfile refactor: use multi-stage Dockerfile for smaller runtime image 2025-09-18 14:27:51 +08:00
MIT License.md Initial release: PostgreSQL backup bot with R2 support 2025-09-13 21:38:54 +08:00
README.md Update README.md 2025-09-13 22:12:51 +08:00
main.py Initial release: PostgreSQL backup bot with R2 support 2025-09-13 21:38:54 +08:00
requirements.txt chores: bump boto3 from 1.40.36 ti 1.40.37 2025-09-24 21:56:01 +08:00

README.md

Postgres-to-R2 Backup

A lightweight automation service that creates scheduled PostgreSQL backups and securely uploads them to Cloudflare R2 object storage.
Designed for Railway deployments, with built-in support for Docker and cron scheduling.


Features

  • 📦 Automated Backups — scheduled daily or hourly backups of your PostgreSQL database
  • 🔐 Optional Encryption — compress with gzip or encrypt with 7z and password-protection
  • ☁️ Cloudflare R2 Integration — seamless upload to your R2 bucket
  • 🧹 Retention Policy — keep a fixed number of backups, auto-clean old ones
  • 🔗 Flexible Database URL — supports both private and public PostgreSQL URLs
  • 🐳 Docker Ready — lightweight container for portable deployment

🚀 Deployment on Railway

  1. Fork this repository
  2. Create a new project on Railway
  3. Add environment variables in Railway dashboard:
DATABASE_URL=           # Your PostgreSQL database URL (private)
DATABASE_PUBLIC_URL=    # Public database URL (optional)
USE_PUBLIC_URL=false    # Set to true to use DATABASE_PUBLIC_URL
DUMP_FORMAT=dump        # Options: sql, plain, dump, custom, tar
FILENAME_PREFIX=backup  # Prefix for backup files
MAX_BACKUPS=7           # Number of backups to keep
R2_ACCESS_KEY=          # Cloudflare R2 access key
R2_SECRET_KEY=          # Cloudflare R2 secret key
R2_BUCKET_NAME=         # R2 bucket name
R2_ENDPOINT=            # R2 endpoint URL
BACKUP_PASSWORD=        # Optional: password for 7z encryption
BACKUP_TIME=00:00       # Daily backup time in UTC (HH:MM format)

Quick Deploy

Click the button below to deploy directly to Railway:

Deploy on Railway


Railway Cron Jobs

You can configure the backup schedule using Railway's built-in cron jobs in the dashboard:

  1. Go to your project settings
  2. Navigate to Deployments > Cron
  3. Add a new cron job pointing to your service

Common cron expressions:

Schedule Cron Expression Description
Hourly 0 * * * * Run once every hour
Daily (midnight) 0 0 * * * Run once per day at midnight
Twice Daily 0 */12 * * * Run every 12 hours
Weekly 0 0 * * 0 Run once per week (Sunday)
Monthly 0 0 1 * * Run once per month

Pro Tips:

  • Use crontab.guru to verify your cron expressions
  • All times are in UTC
  • Configure backup retention (MAX_BACKUPS) according to your schedule

📜 License

This project is open source under the MIT License.
You are free to use, modify, and distribute it with attribution.