Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostgreSQL Backup to Cloudflare R2

Periodic PostgreSQL backups to S3, Cloudflare R2 or any S3 compatible storage with Telegram notifications.

Adapted from:

Backup Structure in R2

your-bucket/
└── postgres-backups/
    └── your-database/
        ├── daily/
        │   ├── yourdb_2026-06-05T02-00-00Z.sql.gz
        │   └── yourdb_2026-06-06T02-00-00Z.sql.gz
        ├── weekly/
        │   └── yourdb_2026-06-01T02-00-00Z.sql.gz  ← uploaded every Sunday
        └── monthly/
            └── yourdb_2026-06-01T02-00-00Z.sql.gz  ← uploaded on 1st of month

Setup

1. Cloudflare R2

  1. Go to Cloudflare Dashboard → R2 → Create Bucket
  2. Go to R2 → Manage R2 API Tokens → Create API Token
    • Permissions: Object Read & Write
    • Scope: your backup bucket
  3. Note your Account ID, Access Key ID, and Secret Access Key

2. Telegram Bot

  1. Message @BotFather → /newbot
  2. Copy the bot token
  3. Add the bot to your group
  4. Get the chat ID via @userinfobot

3. Deploy on Coolify

  1. Push this repo to a private GitHub repository
  2. In Coolify → New Resource → Docker Compose
  3. Point to your GitHub repo
  4. Add environment variables from .env.example
  5. Deploy

4. Test the backup

After deploying, trigger a manual backup:

docker exec pgphylax /bin/sh /usr/local/bin/backup.sh

Notifications

Event Telegram Sound
Backup started Silent
Backup succeeded Silent
Backup failed Audible alert

Restore

# List available backups
aws s3 ls s3://your-bucket/postgres-backups/your-db/daily/ \
  --endpoint-url https://your-account-id.r2.cloudflarestorage.com

# Download a backup
aws s3 cp s3://your-bucket/postgres-backups/your-db/daily/your-db_2026-06-05T02-00-00Z.gpg \
  ./restore.gpg \
  --endpoint-url https://your-account-id.r2.cloudflarestorage.com

# Restore
#### Create New Database (Skip if restoring to existing db)
psql -h <postgres-host> -U <postgres-user> -c "CREATE DATABASE <target_db_name>;"

#### Decrypt
gpg --batch --passphrase "your_secure_encryption_password" -d <path/to/backup.dump.gpg> > <path/to/decrypted_backup.dump>

#### Restore Dump File
pg_restore -h <postgres-host> -U <postgres-user> -d <target_db_name> -v <path/to/decrypted_backup.dump>

About

Periodic PostgreSQL backups to S3, Cloudflare R2 or any S3 compatible storage with Telegram notifications.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages