Litestream — Next Steps & External Setup
Everything you need to do outside the codebase to enable Litestream SQLite replication.
1. Rails Credentials
Run bin/rails credentials:edit and add:
litestream:
replica_bucket: "your-litestream-bucket" # Dedicated R2 bucket for DB backups
replica_key_id: "your-r2-access-key-id" # Can reuse Cloudflare R2 keys
replica_access_key: "your-r2-secret-key" # Can reuse Cloudflare R2 keys
r2_account_id: "your-cloudflare-account-id" # Can reuse Cloudflare account ID
dashboard_username: "admin"
dashboard_password: "a-secure-dashboard-password"
Note: Litestream credentials can share the same R2 access keys as Cloudflare storage. The initializer falls back to
cloudflarecredentials iflitestream-specific keys aren't set. However, using a dedicated bucket is recommended to separate file uploads from DB replicas.
2. Create a Dedicated R2 Bucket
- Go to Cloudflare Dashboard → R2 Object Storage
- Click Create bucket
- Name:
yourapp-litestream(keep it separate from your file uploads bucket) - Location: choose a region close to your server
- No public access needed — Litestream only needs API access
3. API Credentials
You can either:
- Reuse the same R2 API token created for Cloudflare R2 file storage
- Create a dedicated token scoped only to the litestream bucket for better security
If creating a dedicated token:
- Go to R2 → Manage R2 API Tokens → Create API Token
- Name:
yourapp-litestream - Permissions: Object Read & Write
- Specify bucket: Select your litestream bucket only
- Copy the Access Key ID and Secret Access Key
4. Verification
- Check replication is running: After deploying, look for Litestream log messages in Puma output:
litestream: replicating storage/production.sqlite3 litestream: replicating storage/production_cache.sqlite3 ... - Check R2 bucket: In Cloudflare Dashboard → R2 → your litestream bucket → you should see replica files appearing
- Visit the dashboard: Navigate to
/litestreamas an admin user → see replication status for all 4 databases - Test restore: (on a non-production copy)
bin/rails litestream:restore -- --database storage/production.sqlite3
5. Restore in Disaster Recovery
To restore from a backup:
# List available generations (snapshots)
bin/rails litestream:generations -- --database storage/production.sqlite3
# Restore latest snapshot
bin/rails litestream:restore -- --database storage/production.sqlite3
# Restore to a specific point in time
bin/rails litestream:restore -- --database storage/production.sqlite3 --timestamp 2025-01-15T00:00:00Z
# Restore all 4 databases
for db in production production_cache production_queue production_cable; do
bin/rails litestream:restore -- --database storage/${db}.sqlite3
done
6. Deployment Checklist
- Dedicated R2 bucket created for DB replicas (
yourapp-litestream) - Litestream credentials added to Rails credentials
LITESTREAM_IN_PUMA: trueinconfig/deploy.yml- After deploy: check Puma logs for Litestream replication messages
- R2 bucket shows replica files for all 4 databases
- Visit
/litestreamas admin → dashboard loads and shows replication status - Test restore on a non-production copy