Set up R2
R2 has no egress fees, so downloading backups for drills and restores costs nothing. The free tier (10 GB) is enough for most small databases. You need a Cloudflare account, and R2 asks for a payment method even on the free tier.
Create the bucket
Section titled “Create the bucket”In the Cloudflare dashboard:
-
Click R2 Object Storage in the sidebar. If this is your first time, accept the R2 terms.
ScreenshotThe dashboard sidebar with R2 Object Storage highlightedsrc/assets/screenshots/r2-1-sidebar.png -
Click Create bucket. Name it, for example
acme-db-backups. Leave Location on Automatic and Default storage class on Standard, then click Create bucket.To keep the data in the EU, choose the European Union (EU) jurisdiction here. That bucket gets its own endpoint (see below).
ScreenshotThe Create bucket form, filled insrc/assets/screenshots/r2-2-create-bucket.png -
Leave Public access off. Backups are encrypted anyway, but nobody needs to read them over the web.
Create an API token
Section titled “Create an API token”-
Back on the R2 overview page, click Manage API tokens (under Account details → API Tokens).
ScreenshotThe R2 overview page, with Manage API tokens circledsrc/assets/screenshots/r2-3-manage-tokens.png -
Click Create Account API token. (A User API token works too.)
-
Give it a name like
pgbackup-acme. -
Under Permissions, choose Object Read & Write.
prunedeletes old backups, and this permission includes that. -
Under Specify bucket(s), choose Apply to specific buckets only and pick your bucket.
ScreenshotThe token form: Object Read & Write, one bucket selectedsrc/assets/screenshots/r2-4-token-form.png -
Leave TTL on Forever, or set an expiry and a reminder to rotate the token. Click Create API Token.
-
Copy the Access Key ID and the Secret Access Key. The secret is shown only once. Also copy the endpoint shown for S3 clients.
ScreenshotThe created token, keys blurred, endpoint visiblesrc/assets/screenshots/r2-5-token-created.png
Put them in .env
Section titled “Put them in .env”S3_ENDPOINT=https://<account-id>.r2.cloudflarestorage.comS3_BUCKET=acme-db-backupsS3_PREFIX=acme # a short name for the app; one bucket can hold many appsAWS_ACCESS_KEY_ID=<Access Key ID>AWS_SECRET_ACCESS_KEY=<Secret Access Key>Leave S3_REGION unset: auto is right for R2.
EU jurisdiction buckets use a different endpoint:
S3_ENDPOINT=https://<account-id>.eu.r2.cloudflarestorage.comThen check it all with pgb doctor. It writes a tiny test file under the prefix and deletes it again.
A token for your laptop
Section titled “A token for your laptop”Drills and restores only read backups, so a token with Object Read only works for them. But every drill also writes a small record to <prefix>/.drills/, which is how pgb list and pgb doctor know when you last drilled. A read-only token can’t write it. The drill still runs, with a warning, but it isn’t remembered.
To keep the records, give the laptop token Object Read & Write too, limited to the same bucket.
Keys and security: where the public and private keys live, and what happens if you lose one.
Also: keep using folders you already have, with lifecycle and lock rules (Use your existing folders), or use something other than R2 (Other storage).
pgbackup-r2 is an open-source tool from Iotabyt. MIT licensed.