pgbackup-r2 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. pgbackup_r2-0.4.0/.gitignore +27 -0
  2. pgbackup_r2-0.4.0/CHANGELOG.md +172 -0
  3. pgbackup_r2-0.4.0/Dockerfile +52 -0
  4. pgbackup_r2-0.4.0/LICENSE +21 -0
  5. pgbackup_r2-0.4.0/PKG-INFO +113 -0
  6. pgbackup_r2-0.4.0/README.md +80 -0
  7. pgbackup_r2-0.4.0/examples/drill/.env.example +33 -0
  8. pgbackup_r2-0.4.0/examples/drill/README.md +153 -0
  9. pgbackup_r2-0.4.0/examples/drill/compose.yml +38 -0
  10. pgbackup_r2-0.4.0/examples/drill/out/.gitkeep +0 -0
  11. pgbackup_r2-0.4.0/examples/monthly-drill.yml +62 -0
  12. pgbackup_r2-0.4.0/examples/nightly-backup.yml +67 -0
  13. pgbackup_r2-0.4.0/pyproject.toml +93 -0
  14. pgbackup_r2-0.4.0/scripts/install.ps1 +60 -0
  15. pgbackup_r2-0.4.0/scripts/install.sh +73 -0
  16. pgbackup_r2-0.4.0/scripts/pgb +711 -0
  17. pgbackup_r2-0.4.0/scripts/pgb.ps1 +706 -0
  18. pgbackup_r2-0.4.0/src/pgbackup_r2/__init__.py +3 -0
  19. pgbackup_r2-0.4.0/src/pgbackup_r2/__main__.py +5 -0
  20. pgbackup_r2-0.4.0/src/pgbackup_r2/agecrypt.py +183 -0
  21. pgbackup_r2-0.4.0/src/pgbackup_r2/checks.py +118 -0
  22. pgbackup_r2-0.4.0/src/pgbackup_r2/cli.py +354 -0
  23. pgbackup_r2-0.4.0/src/pgbackup_r2/commands.py +979 -0
  24. pgbackup_r2-0.4.0/src/pgbackup_r2/config.py +251 -0
  25. pgbackup_r2-0.4.0/src/pgbackup_r2/errors.py +23 -0
  26. pgbackup_r2-0.4.0/src/pgbackup_r2/heartbeat.py +42 -0
  27. pgbackup_r2-0.4.0/src/pgbackup_r2/pg.py +184 -0
  28. pgbackup_r2-0.4.0/src/pgbackup_r2/pgurl.py +140 -0
  29. pgbackup_r2-0.4.0/src/pgbackup_r2/retention.py +21 -0
  30. pgbackup_r2-0.4.0/src/pgbackup_r2/scheduling.py +69 -0
  31. pgbackup_r2-0.4.0/src/pgbackup_r2/stamps.py +58 -0
  32. pgbackup_r2-0.4.0/src/pgbackup_r2/storage.py +334 -0
  33. pgbackup_r2-0.4.0/src/pgbackup_r2/tiers.py +205 -0
  34. pgbackup_r2-0.4.0/src/pgbackup_r2/ui.py +329 -0
  35. pgbackup_r2-0.4.0/src/pgbackup_r2/util.py +89 -0
  36. pgbackup_r2-0.4.0/tests/test_defaults.py +115 -0
  37. pgbackup_r2-0.4.0/tests/test_e2e.py +469 -0
  38. pgbackup_r2-0.4.0/tests/test_pgb.py +1116 -0
  39. pgbackup_r2-0.4.0/tests/test_pgurl.py +96 -0
  40. pgbackup_r2-0.4.0/tests/test_tiers.py +570 -0
  41. pgbackup_r2-0.4.0/tests/test_units.py +309 -0
@@ -0,0 +1,27 @@
1
+ # Secrets and backups: never commit these
2
+ .env
3
+ *.env
4
+ !.env.example
5
+ key.txt
6
+ *.key
7
+ *.age
8
+ *.dump
9
+ *.dump.partial
10
+
11
+ # examples/drill output folder (keep the folder itself)
12
+ examples/drill/out/*
13
+ !examples/drill/out/.gitkeep
14
+
15
+ # Python / uv
16
+ __pycache__/
17
+ *.py[cod]
18
+ .venv/
19
+ dist/
20
+ build/
21
+ *.egg-info/
22
+ .pytest_cache/
23
+ .ruff_cache/
24
+ .coverage
25
+
26
+ # Windows download markers
27
+ *:Zone.Identifier
@@ -0,0 +1,172 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ## 0.4.0 (2026-11-24)
6
+
7
+ pgbackup-r2 is on PyPI now. Docker and `pgb` stay the main way to run it, and nothing about how it works has changed.
8
+
9
+ - **On PyPI:** `pip install pgbackup-r2`, or `uv tool install pgbackup-r2`, or try it with `uvx pgbackup-r2 --version`. You still need `pg_dump`, `pg_restore`, `psql` and `age` on your `PATH` for that route.
10
+ - **New guides:** [Neon](https://pgbackup-r2.iotabyt.com/guides/neon/), the setup pgbackup-r2 runs on in production (Postgres 18), and [Supabase](https://pgbackup-r2.iotabyt.com/guides/supabase/), which isn't tested yet and says so. Managed databases is now a short hub for both.
11
+ - **Docs, for people searching:**
12
+ - Every page has a clearer title and description.
13
+ - The homepage has its own title, a proper link preview and structured data.
14
+ - There's a `robots.txt` that points at the sitemap.
15
+ - Workers preview URLs are off, so only `pgbackup-r2.iotabyt.com` gets indexed.
16
+ - **Docs fixes:**
17
+ - GitHub's 60-day pause for scheduled workflows only applies to public repos, and the docs now say so.
18
+ - The Supabase connection advice is gentler: the direct host (IPv6, or IPv4 with the add-on) or the Session pooler, and the Transaction pooler isn't recommended.
19
+ - **Packaging:**
20
+ - Full PyPI metadata: keywords, classifiers and project links.
21
+ - The README uses absolute links, so it renders on PyPI.
22
+ - The Docker images carry OCI labels for title, description, url and documentation.
23
+
24
+ ### Upgrading from 0.3.2
25
+
26
+ Nothing to change. Re-run the install line, then `pgb update` in each backup folder.
27
+
28
+ ## 0.3.2 (2026-11-19)
29
+
30
+ - **`pgb` works with an older image.** Before using a feature the image may not have, `pgb` asks the image for its version (once per run, remembered by image ID) and compares it with the version that feature needs.
31
+ - **`pgb drill` with an image older than 0.3.1** skips the preflight with a note ("image 0.3.0 is older than pgb 0.3.2; run `pgb update`") and drills the old way, instead of failing with "unrecognized arguments".
32
+ - **Images older than 0.2.0 have no `doctor` or `detect`.** `pgb doctor` and `pgb init --detect` say so and point to `pgb update`.
33
+ - **`pgb doctor` compares versions:** ✓ when `pgb` and the image match, ⚠ with "run `pgb update`" when the image is older, and ⚠ with the install line when `pgb` is.
34
+
35
+ ### Upgrading from 0.3.1
36
+
37
+ Re-run the install line, then `pgb update` in each backup folder.
38
+
39
+ ## 0.3.1 (2026-11-19)
40
+
41
+ Polish found while backing up Labpadi for real.
42
+
43
+ - **`pgb drill` checks before starting Postgres.** It first runs the image's new `drill --preflight`: a private key is set, the bucket answers, the backup exists. A missing key or an empty bucket now fails in a second, with the same message as before and no container or network started.
44
+ - **Shorter database URLs in output.**
45
+ - A database shows as `host/dbname`, with `:port` if it isn't 5432: `ep-withered-cell-batk9adw.c-8.eu-central-1.aws.neon.tech/labpadi`.
46
+ - The user, password and query string (`sslmode`, `channel_binding`) never appear, in `restore`, `drill`, `doctor`, `backup` or the nightly log.
47
+ - `restore` names its target once, in the final `PASS` line.
48
+ - **`pgb nightly start` and `AGE_IDENTITY`:** the note now reads "note: .env has AGE_IDENTITY. That's fine on your own machine; on a server, leave it out (backups only need AGE_RECIPIENTS)."
49
+ - **Unknown commands stop before any container starts**, with the closest match: `pgb: unknown command "uninstal". Did you mean "uninstall"?` The exit code is 2.
50
+
51
+ ### Upgrading from 0.3.0
52
+
53
+ Re-run the install line, then `pgb update` in each backup folder.
54
+
55
+ ## 0.3.0 (2026-11-19)
56
+
57
+ - Short install URL: `curl -fsSL https://pgbackup-r2.iotabyt.com/install.sh | sh`. It redirects (302) to the installer on GitHub. The README and docs use it everywhere, and the Install page keeps the GitHub URLs for networks that block redirects.
58
+ - PowerShell has an installer script too: `irm https://pgbackup-r2.iotabyt.com/install.ps1 | iex`. It writes the `pgb` function into `$PROFILE` between marker comments, and running it again leaves `$PROFILE` alone.
59
+ - The installers fetch `pgb` from the latest release (via GitHub's API, falling back to the highest `vX.Y.Z` tag), never from `main`. `PGB_VERSION=v0.2.0` installs a specific release. If the API can't be reached, the installer stops with a clear message instead of falling back to `main`.
60
+ - `pgb uninstall` removes pgb, and in PowerShell its `~\.pgb` folder and only the installer's lines in `$PROFILE`.
61
+ - **`--all`:** also removes the `pgb nightly` containers and the `ghcr.io/iotabyt/pgbackup-r2` images, but only those carrying pgbackup-r2's image label.
62
+ - **Asks first:** it lists what it will remove before asking. `--yes` skips the question, and without a terminal and `--yes` it refuses.
63
+ - **Never touched:** backups, `.env` files, `out/` folders or keys.
64
+ - **Twice is safe:** a second run says `pgb is not installed`.
65
+
66
+ ### Upgrading from 0.2.x
67
+
68
+ Re-run the install line to get `pgb uninstall`, then run `pgb update` in each backup folder.
69
+
70
+ - **bash, zsh, fish:** `curl -fsSL https://pgbackup-r2.iotabyt.com/install.sh | sh`
71
+ - **PowerShell:** `irm https://pgbackup-r2.iotabyt.com/install.ps1 | iex`. Your existing `function pgb` line in `$PROFILE` is kept, and `pgb uninstall` recognises it.
72
+
73
+ ## 0.2.0 (2026-11-18)
74
+
75
+ `pgb` is now the main way to use pgbackup-r2. It's still a thin wrapper around Docker, with no Python on the host, and the same in bash (`scripts/pgb`) and PowerShell (`scripts/pgb.ps1`).
76
+
77
+ - `pgb init` writes a commented `.env` (never overwriting one), makes it readable only by you on Linux and macOS, creates `out/`, and prints the next steps.
78
+ - `pgb doctor`, and `doctor` in the image, check:
79
+ - that Docker is running, and that `.env` exists with every setting a backup needs
80
+ - that the database is reachable and its major version matches `PG_MAJOR`
81
+ - that the bucket is writable (a test file is put, then deleted)
82
+ - that `AGE_IDENTITY` opens what's encrypted to `AGE_RECIPIENTS`
83
+
84
+ Each check prints ✓ or ✗ with a fix, and `doctor` exits 1 if anything fails.
85
+ - `pgb drill` runs a full drill with no compose file:
86
+ 1. It starts a throwaway `postgres:<PG_MAJOR>` on a private network, with a random password and in-memory storage.
87
+ 2. It waits for the database to be ready (60s at most), then runs the drill.
88
+ 3. It always removes the database and the network afterwards, after a failure or Ctrl+C too.
89
+
90
+ The exit code is the drill's.
91
+ - `pgb nightly start|stop|status|logs` runs scheduled backups in a background container named `pgbackup-<S3_PREFIX>`, with `--restart unless-stopped`.
92
+ - `pgb update` pulls the image and shows the old and new versions. `pgb version` and `pgb help` are new too.
93
+ - Database networking:
94
+ - A `localhost` database URL works. `pgb` uses `--network host` on Linux, or `host.docker.internal` on Docker Desktop.
95
+ - `PGB_NETWORK` joins an existing Docker network, such as a Compose project's `myapp_default`.
96
+ - When a host name can't be resolved, the error explains why and gives both fixes.
97
+ - `scripts/install.sh`: a one-line installer. It detects your shell and prints the `PATH` line for it, without editing any files. `pgb` runs on bash 3.2 (macOS), and CI checks this.
98
+ - `keygen`: both key panels have the same width. In a terminal too narrow for them, the keys print as whole lines instead of being cut short.
99
+ - README:
100
+ - Quick start uses `pgb` throughout.
101
+ - New sections:
102
+ - "Without pgb", with the plain Docker commands
103
+ - "Run it from GitHub Actions", with `examples/nightly-backup.yml`
104
+ - Two walkthroughs: a VPS with Compose, and Neon or Supabase with GitHub Actions
105
+ - Troubleshooting
106
+ - Step-by-step R2 setup, and notes for AWS S3, Backblaze B2 and MinIO.
107
+ - Retention can now live in the bucket. Set `TIERS="daily=daily/ monthly=monthly/"` to use your own tier folders under the prefix.
108
+ - **Tiers and copies:** the tiers are `hourly`, `daily`, `weekly`, `monthly` and `yearly`, in folders with any names. The most frequent tier gets every upload. Each other tier gets a server-side copy (metadata kept, nothing re-uploaded) of the first backup of its period.
109
+ - **Coverage is read from the bucket,** never from local state.
110
+ - **`PRUNE` defaults to false with TIERS,** so the bucket's lifecycle rules do the deleting. With `PRUNE=true`, only the most frequent tier is pruned.
111
+ - **Bad TIERS values** are a config error, with an example.
112
+ - **Without TIERS,** retention is tool-managed, as before.
113
+ - `pgb init --detect`, `detect` and `doctor` look at the bucket. They list the folders under the prefix and read the lifecycle rules (skipped quietly if not allowed), then suggest a `TIERS` line. `init --detect` writes it into `.env` only if you confirm (or pass `--yes`).
114
+ - Reading across tiers:
115
+ - `list` shows a tier column.
116
+ - `latest` is the newest backup in any tier.
117
+ - `verify`, `drill`, `decrypt` and `restore` accept `<tier>/<stamp>`.
118
+ - Files that aren't pgbackup-r2 backups are never touched, and `list` ignores them. `doctor` counts them.
119
+ - A delete refused by a lock or retention rule is a warning ("locked, skipped"), not a failure.
120
+ - Docs: a "Use your existing folders" section, with example R2 lifecycle and bucket lock rules. The R2 EU-jurisdiction endpoint (`https://<account-id>.eu.r2.cloudflarestorage.com`) is in the README, `.env.example` and the `pgb init` template.
121
+ - Drill tracking:
122
+ - Every drill, pass or fail, writes `<prefix>/.drills/<time>.json`: the backup tested, PASS or FAIL, tables, rows, duration and tool version. It holds counts only, so it isn't encrypted. `list`, `prune` and detection skip it.
123
+ - `list` ends with `Last drill: PASS, 12 days ago`.
124
+ - `doctor` shows ✓ for a successful drill within 35 days. It shows ⚠ when the last one is older, when none has passed, or when the most recent drill failed.
125
+ - Documentation moved to its own site, [pgbackup-r2.iotabyt.com](https://pgbackup-r2.iotabyt.com): guides, concepts, a full reference and troubleshooting. It's built from `docs/` with Astro Starlight. The README is now short and links into it. pgbackup-r2 also has its own logo (`.github/assets/`).
126
+ - `examples/drill/compose.yml` stays, for people who prefer Compose.
127
+ - Polish:
128
+ - **`pgb init`'s `.env`:** the 7 settings a backup needs come first, and everything else is commented out under "Optional", at its default.
129
+ - **Typos:** `doctor` warns about settings it doesn't know, with a "did you mean" (`S3_BUKET → S3_BUCKET?`).
130
+ - **Help:** every command's `--help` shows a one-line summary, the usage and examples. `pgb help <command>` does the same.
131
+ - **Errors:** every error ends with the next thing to run or the setting to fix.
132
+ - **One set of defaults** in the code, and a test that the template, the docs and `--help` agree with it.
133
+ - **Docs:** a "Why pgbackup-r2" page.
134
+ - **Repo:** issue templates, `SECURITY.md` and `CONTRIBUTING.md`.
135
+
136
+ ### Upgrading from 0.1.x
137
+
138
+ - **`pgb`:** run the install line again to get the new commands (`init`, `doctor`, `drill`, `nightly`, `update`, `help`).
139
+ - **Image:** `pgb update` pulls 0.2.0. The tags (`pg16`, `pg17`, `pg18`) are unchanged.
140
+ - **`.env`:** nothing to change. No setting was renamed or removed, and defaults are unchanged. Without `TIERS`, pruning works as before. The new settings (`TIERS`, `PRUNE`, `PGB_NETWORK`) are all optional.
141
+ - **Drill records:** drills now write a small record to `<prefix>/.drills/`, which needs write access. With a read-only token, drills still run, with a warning, but `list` and `doctor` won't know about them.
142
+ - **Scripts that read `list`:** its output now ends with a `Last drill: …` line. Skip lines that don't start with a stamp.
143
+ - **`examples/drill`:** nothing to do. `pgb drill` does the same job without the compose file.
144
+ - **`PGB_DOCKER_ARGS` is gone.** `pgb` now handles a database on `localhost` by itself. For a Compose network, use `PGB_NETWORK`.
145
+ - **`PGBACKUP_IMAGE` in `examples/drill/compose.yml`** is now `PGB_IMAGE`, the same name `pgb` uses. It's for testing local builds only.
146
+ - Tests: both `pgb` scripts are tested against a fake `docker`, and the drill cleanup against real Docker. The scripts are also run under bash 3.2 and checked with shellcheck.
147
+
148
+ ## 0.1.1 (unreleased)
149
+
150
+ - `restore`: the target can come from `RESTORE_DATABASE_URL`, so the password stays out of the process list. `--target` still works and overrides it. With neither set, `restore` fails with a clear message.
151
+ - `examples/drill`: `PG_MAJOR` in `.env` picks both images (`postgres:${PG_MAJOR}` and `pgbackup-r2:pg${PG_MAJOR}`, default 16). The README explains why it must match the server's major version.
152
+ - `examples/drill`: the same folder and `.env` can now take backups too (`docker compose run --rm drill backup`). `.env.example` has `DATABASE_URL`, which must be a direct, non-pooled connection, and `AGE_RECIPIENTS`.
153
+ - `pgb`: a short wrapper around `docker run` (`scripts/pgb` for bash, `scripts/pgb.ps1` for PowerShell). It uses `./.env`, takes the image tag from `PG_MAJOR`, mounts `./out` at `/out`, adds `-it` at a terminal, and runs as you on Linux. `PGB_DOCKER_ARGS` passes extra Docker options. The README now uses `pgb` in its examples, with one-line installs.
154
+ - CI: `actions/checkout@v5` and `astral-sh/setup-uv@v7`, with monthly Dependabot updates for GitHub Actions and uv.
155
+
156
+ ### Upgrading from 0.1.0
157
+
158
+ Nothing to do. `restore --target` still works. To keep the password out of `ps`, move the URL into `RESTORE_DATABASE_URL` in `.env`. In `examples/drill`, `PG_MAJOR` defaults to 16; set it if your server is newer.
159
+
160
+ ## 0.1.0 (unreleased)
161
+
162
+ First release.
163
+
164
+ - `backup`: `pg_dump -Fc` piped into `age`, uploaded to R2/S3 with a SHA-256 in the object metadata, plus an encrypted manifest of every table's row count. Refuses to run without `AGE_RECIPIENTS`.
165
+ - `drill`: restores into a scratch database and compares row counts against the manifest. Reports missing tables, extra tables and drifted counts.
166
+ - `verify`: checksum, decrypt and table of contents, with no database.
167
+ - `decrypt`: writes a plain `pg_dump` file, from the bucket or from a local `.dump.age` (`--file`).
168
+ - `restore`: restores into `--target URL`. Refuses the live database unless `--to-production` is passed and the database name is typed to confirm.
169
+ - `list`, `prune` (keeps the newest `MIN_KEEP`), `keygen`, and `schedule`, which is the container default and needs no cron.
170
+ - Heartbeat pings on success and failure. A failed ping never fails a backup.
171
+ - Interactive terminal output with rich (spinners, tables, PASS/FAIL), and plain log lines everywhere else (`--plain`, `NO_COLOR`).
172
+ - Docker images for Postgres 16, 17 and 18 (`ghcr.io/iotabyt/pgbackup-r2:pg16` and so on), plus a Docker-only drill kit in `examples/drill`.
@@ -0,0 +1,52 @@
1
+ # syntax=docker/dockerfile:1
2
+
3
+ # --- build: make the wheel with uv -------------------------------------------------------
4
+ FROM python:3.12-slim-bookworm AS build
5
+ COPY --from=ghcr.io/astral-sh/uv:0.11 /uv /usr/local/bin/uv
6
+ WORKDIR /src
7
+ COPY pyproject.toml uv.lock README.md LICENSE ./
8
+ COPY src ./src
9
+ RUN uv build --wheel --out-dir /dist
10
+
11
+ # --- runtime: Python + age + the matching Postgres client ---------------------------------
12
+ FROM python:3.12-slim-bookworm
13
+
14
+ # The client must match your server's major version: build pg16, pg17 and pg18 images.
15
+ ARG PG_MAJOR=16
16
+
17
+ ENV PYTHONDONTWRITEBYTECODE=1 \
18
+ PYTHONUNBUFFERED=1 \
19
+ PIP_NO_CACHE_DIR=1 \
20
+ PIP_DISABLE_PIP_VERSION_CHECK=1 \
21
+ PATH=/usr/lib/postgresql/${PG_MAJOR}/bin:$PATH
22
+
23
+ RUN set -eux; \
24
+ apt-get update; \
25
+ apt-get install -y --no-install-recommends ca-certificates curl gnupg age; \
26
+ curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc \
27
+ | gpg --dearmor -o /usr/share/keyrings/pgdg.gpg; \
28
+ echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] https://apt.postgresql.org/pub/repos/apt bookworm-pgdg main" \
29
+ > /etc/apt/sources.list.d/pgdg.list; \
30
+ apt-get update; \
31
+ apt-get install -y --no-install-recommends "postgresql-client-${PG_MAJOR}"; \
32
+ apt-get purge -y --auto-remove curl gnupg; \
33
+ rm -rf /var/lib/apt/lists/*; \
34
+ pg_dump --version; age --version
35
+
36
+ COPY --from=build /dist/*.whl /tmp/
37
+ RUN pip install /tmp/*.whl && rm /tmp/*.whl \
38
+ && useradd --uid 10001 --create-home --shell /usr/sbin/nologin pgbackup
39
+
40
+ USER 10001
41
+ WORKDIR /home/pgbackup
42
+
43
+ # image.source must stay exactly this: `pgb uninstall --all` finds our containers and images by it.
44
+ LABEL org.opencontainers.image.title="pgbackup-r2" \
45
+ org.opencontainers.image.description="Encrypted Postgres backups to Cloudflare R2 or any S3, with restore drills that prove they work" \
46
+ org.opencontainers.image.url="https://pgbackup-r2.iotabyt.com" \
47
+ org.opencontainers.image.documentation="https://pgbackup-r2.iotabyt.com" \
48
+ org.opencontainers.image.source="https://github.com/iotabyt/pgbackup-r2" \
49
+ org.opencontainers.image.licenses="MIT"
50
+
51
+ ENTRYPOINT ["pgbackup-r2"]
52
+ CMD ["schedule"]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Frank Owusu Boakye
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,113 @@
1
+ Metadata-Version: 2.5
2
+ Name: pgbackup-r2
3
+ Version: 0.4.0
4
+ Summary: Encrypted Postgres backups to Cloudflare R2 (or any S3), with restore drills that prove they work.
5
+ Project-URL: Homepage, https://pgbackup-r2.iotabyt.com
6
+ Project-URL: Documentation, https://pgbackup-r2.iotabyt.com
7
+ Project-URL: Repository, https://github.com/iotabyt/pgbackup-r2
8
+ Project-URL: Changelog, https://pgbackup-r2.iotabyt.com/changelog/
9
+ Project-URL: Issues, https://github.com/iotabyt/pgbackup-r2/issues
10
+ Author: Frank Owusu Boakye
11
+ Author-email: Iotabyt <hello@iotabyt.com>
12
+ License-Expression: MIT
13
+ License-File: LICENSE
14
+ Keywords: age,backup,cloudflare-r2,encryption,pg_dump,postgres,postgresql,restore,s3
15
+ Classifier: Development Status :: 4 - Beta
16
+ Classifier: Environment :: Console
17
+ Classifier: Intended Audience :: Developers
18
+ Classifier: Intended Audience :: System Administrators
19
+ Classifier: Operating System :: POSIX
20
+ Classifier: Programming Language :: Python :: 3
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Programming Language :: Python :: 3.13
24
+ Classifier: Programming Language :: Python :: 3.14
25
+ Classifier: Topic :: Database
26
+ Classifier: Topic :: System :: Archiving :: Backup
27
+ Classifier: Topic :: System :: Systems Administration
28
+ Requires-Python: >=3.11
29
+ Requires-Dist: boto3>=1.36
30
+ Requires-Dist: rich-argparse>=1.5
31
+ Requires-Dist: rich>=13.7
32
+ Description-Content-Type: text/markdown
33
+
34
+ <picture>
35
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/iotabyt/pgbackup-r2/main/.github/assets/mark-reversed.svg">
36
+ <img src="https://raw.githubusercontent.com/iotabyt/pgbackup-r2/main/.github/assets/mark.svg" width="64" height="64" alt="pgbackup-r2 logo">
37
+ </picture>
38
+
39
+ # pgbackup-r2
40
+
41
+ Encrypted Postgres backups to Cloudflare R2 or any S3, with restore drills that prove they work.
42
+
43
+ Built and used in production by [Iotabyt](https://www.iotabyt.com).
44
+
45
+ **[Read the pgbackup-r2 documentation](https://pgbackup-r2.iotabyt.com)**: the quick start, guides for Cloudflare R2, Neon, Docker Compose and Kamal, the full reference, and troubleshooting.
46
+
47
+ ```
48
+ PASS shop/2026-11-18T020000Z.dump.age (48.2 MB, restored in 21.4s)
49
+ 37 tables and 1,284,903 rows match the manifest
50
+ ```
51
+
52
+ `pg_dump` goes straight into [age](https://age-encryption.org), and the result goes to your bucket with a checksum and an encrypted row-count manifest. A drill restores a backup into a throwaway Postgres and checks every table against that manifest. All you need is Docker.
53
+
54
+ ## Quick start
55
+
56
+ ```bash
57
+ curl -fsSL https://pgbackup-r2.iotabyt.com/install.sh | sh
58
+ mkdir acme-backups && cd acme-backups && pgb init # writes a commented .env
59
+ pgb keygen # then fill in .env: bucket, token, DATABASE_URL and both keys
60
+ pgb doctor # checks Docker, the database, the bucket and the keys
61
+ pgb backup && pgb drill
62
+ pgb nightly start # back up every night at 02:00 UTC
63
+ ```
64
+
65
+ The [Quick start](https://pgbackup-r2.iotabyt.com/start/quick-start/) explains each step. Other starting points:
66
+
67
+ - [Install `pgb`](https://pgbackup-r2.iotabyt.com/start/install/), for bash, zsh, fish and PowerShell
68
+ - [Set up R2](https://pgbackup-r2.iotabyt.com/guides/r2/), or [other storage](https://pgbackup-r2.iotabyt.com/guides/other-storage/)
69
+ - [An app on a VPS](https://pgbackup-r2.iotabyt.com/guides/vps/), or a managed database with GitHub Actions: [Neon](https://pgbackup-r2.iotabyt.com/guides/neon/) or [Supabase](https://pgbackup-r2.iotabyt.com/guides/supabase/)
70
+ - [Use your existing folders](https://pgbackup-r2.iotabyt.com/guides/existing-folders/): `daily/` and `monthly/`, lifecycle rules and bucket locks
71
+ - [Restoring on a bad day](https://pgbackup-r2.iotabyt.com/guides/restoring/)
72
+ - [Reference](https://pgbackup-r2.iotabyt.com/reference/pgb/) and [Troubleshooting](https://pgbackup-r2.iotabyt.com/troubleshooting/)
73
+
74
+ ## Uninstall
75
+
76
+ ```bash
77
+ pgb uninstall # pgb itself
78
+ pgb uninstall --all # also the pgb nightly containers and the pgbackup-r2 images
79
+ ```
80
+
81
+ It shows what it will remove and asks first. Your backups, `.env` files and keys are never touched. If you've already deleted the script, see [Uninstalling](https://pgbackup-r2.iotabyt.com/start/install/#uninstalling) for the manual steps.
82
+
83
+ ## Development
84
+
85
+ Contributions are welcome: see [`CONTRIBUTING.md`](https://github.com/iotabyt/pgbackup-r2/blob/main/CONTRIBUTING.md). In short:
86
+
87
+ ```bash
88
+ uv sync
89
+ uv run ruff check && uv run ruff format --check
90
+ uv run shellcheck scripts/pgb scripts/install.sh
91
+ uv run pytest
92
+ ```
93
+
94
+ The `pgb` tests run both `scripts/pgb` and `scripts/pgb.ps1`. They need `pwsh` for the PowerShell half, and Docker for the drill-cleanup and bash 3.2 tests. Each part is skipped if its tool is missing.
95
+
96
+ The end-to-end tests need a real Postgres, plus `age` and Postgres client tools of the same major version, on `PATH`. In WSL or Ubuntu:
97
+
98
+ ```bash
99
+ sudo apt install age postgresql-client-16
100
+ docker run -d --name pgbackup-test -e POSTGRES_PASSWORD=pw -p 5432:5432 postgres:16
101
+ PATH=/usr/lib/postgresql/16/bin:$PATH \
102
+ PGBACKUP_TEST_URL=postgresql://postgres:pw@localhost:5432/postgres uv run pytest -v
103
+ ```
104
+
105
+ S3 is mocked with moto. For a manual run against a fake S3, start `uv run moto_server -p 5055` and set `S3_ENDPOINT=http://localhost:5055`.
106
+
107
+ To build an image: `docker build --build-arg PG_MAJOR=16 -t pgbackup-r2:pg16 .`
108
+
109
+ The documentation site lives in [`docs/`](https://github.com/iotabyt/pgbackup-r2/tree/main/docs): an Astro Starlight project with its own `package.json`. See [`docs/README.md`](https://github.com/iotabyt/pgbackup-r2/blob/main/docs/README.md) to run it locally.
110
+
111
+ ## License
112
+
113
+ MIT. Copyright (c) 2026 Frank Owusu Boakye.
@@ -0,0 +1,80 @@
1
+ <picture>
2
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/iotabyt/pgbackup-r2/main/.github/assets/mark-reversed.svg">
3
+ <img src="https://raw.githubusercontent.com/iotabyt/pgbackup-r2/main/.github/assets/mark.svg" width="64" height="64" alt="pgbackup-r2 logo">
4
+ </picture>
5
+
6
+ # pgbackup-r2
7
+
8
+ Encrypted Postgres backups to Cloudflare R2 or any S3, with restore drills that prove they work.
9
+
10
+ Built and used in production by [Iotabyt](https://www.iotabyt.com).
11
+
12
+ **[Read the pgbackup-r2 documentation](https://pgbackup-r2.iotabyt.com)**: the quick start, guides for Cloudflare R2, Neon, Docker Compose and Kamal, the full reference, and troubleshooting.
13
+
14
+ ```
15
+ PASS shop/2026-11-18T020000Z.dump.age (48.2 MB, restored in 21.4s)
16
+ 37 tables and 1,284,903 rows match the manifest
17
+ ```
18
+
19
+ `pg_dump` goes straight into [age](https://age-encryption.org), and the result goes to your bucket with a checksum and an encrypted row-count manifest. A drill restores a backup into a throwaway Postgres and checks every table against that manifest. All you need is Docker.
20
+
21
+ ## Quick start
22
+
23
+ ```bash
24
+ curl -fsSL https://pgbackup-r2.iotabyt.com/install.sh | sh
25
+ mkdir acme-backups && cd acme-backups && pgb init # writes a commented .env
26
+ pgb keygen # then fill in .env: bucket, token, DATABASE_URL and both keys
27
+ pgb doctor # checks Docker, the database, the bucket and the keys
28
+ pgb backup && pgb drill
29
+ pgb nightly start # back up every night at 02:00 UTC
30
+ ```
31
+
32
+ The [Quick start](https://pgbackup-r2.iotabyt.com/start/quick-start/) explains each step. Other starting points:
33
+
34
+ - [Install `pgb`](https://pgbackup-r2.iotabyt.com/start/install/), for bash, zsh, fish and PowerShell
35
+ - [Set up R2](https://pgbackup-r2.iotabyt.com/guides/r2/), or [other storage](https://pgbackup-r2.iotabyt.com/guides/other-storage/)
36
+ - [An app on a VPS](https://pgbackup-r2.iotabyt.com/guides/vps/), or a managed database with GitHub Actions: [Neon](https://pgbackup-r2.iotabyt.com/guides/neon/) or [Supabase](https://pgbackup-r2.iotabyt.com/guides/supabase/)
37
+ - [Use your existing folders](https://pgbackup-r2.iotabyt.com/guides/existing-folders/): `daily/` and `monthly/`, lifecycle rules and bucket locks
38
+ - [Restoring on a bad day](https://pgbackup-r2.iotabyt.com/guides/restoring/)
39
+ - [Reference](https://pgbackup-r2.iotabyt.com/reference/pgb/) and [Troubleshooting](https://pgbackup-r2.iotabyt.com/troubleshooting/)
40
+
41
+ ## Uninstall
42
+
43
+ ```bash
44
+ pgb uninstall # pgb itself
45
+ pgb uninstall --all # also the pgb nightly containers and the pgbackup-r2 images
46
+ ```
47
+
48
+ It shows what it will remove and asks first. Your backups, `.env` files and keys are never touched. If you've already deleted the script, see [Uninstalling](https://pgbackup-r2.iotabyt.com/start/install/#uninstalling) for the manual steps.
49
+
50
+ ## Development
51
+
52
+ Contributions are welcome: see [`CONTRIBUTING.md`](https://github.com/iotabyt/pgbackup-r2/blob/main/CONTRIBUTING.md). In short:
53
+
54
+ ```bash
55
+ uv sync
56
+ uv run ruff check && uv run ruff format --check
57
+ uv run shellcheck scripts/pgb scripts/install.sh
58
+ uv run pytest
59
+ ```
60
+
61
+ The `pgb` tests run both `scripts/pgb` and `scripts/pgb.ps1`. They need `pwsh` for the PowerShell half, and Docker for the drill-cleanup and bash 3.2 tests. Each part is skipped if its tool is missing.
62
+
63
+ The end-to-end tests need a real Postgres, plus `age` and Postgres client tools of the same major version, on `PATH`. In WSL or Ubuntu:
64
+
65
+ ```bash
66
+ sudo apt install age postgresql-client-16
67
+ docker run -d --name pgbackup-test -e POSTGRES_PASSWORD=pw -p 5432:5432 postgres:16
68
+ PATH=/usr/lib/postgresql/16/bin:$PATH \
69
+ PGBACKUP_TEST_URL=postgresql://postgres:pw@localhost:5432/postgres uv run pytest -v
70
+ ```
71
+
72
+ S3 is mocked with moto. For a manual run against a fake S3, start `uv run moto_server -p 5055` and set `S3_ENDPOINT=http://localhost:5055`.
73
+
74
+ To build an image: `docker build --build-arg PG_MAJOR=16 -t pgbackup-r2:pg16 .`
75
+
76
+ The documentation site lives in [`docs/`](https://github.com/iotabyt/pgbackup-r2/tree/main/docs): an Astro Starlight project with its own `package.json`. See [`docs/README.md`](https://github.com/iotabyt/pgbackup-r2/blob/main/docs/README.md) to run it locally.
77
+
78
+ ## License
79
+
80
+ MIT. Copyright (c) 2026 Frank Owusu Boakye.
@@ -0,0 +1,33 @@
1
+ # Copy this file to .env and fill it in. NEVER commit .env: it holds your private key.
2
+ # No quotes around values.
3
+
4
+ # Postgres major version: must match your server's major version (run SHOW server_version;)
5
+ # It picks both images: postgres:16 for the scratch database and pgbackup-r2:pg16.
6
+ PG_MAJOR=16
7
+
8
+ # Your R2 endpoint: Cloudflare dashboard > R2 > Overview > "S3 API" / account details.
9
+ # For a bucket in R2's EU jurisdiction: https://<account-id>.eu.r2.cloudflarestorage.com
10
+ S3_ENDPOINT=https://<account-id>.r2.cloudflarestorage.com
11
+ # The bucket, and the app's prefix inside it (the same S3_PREFIX the server uses)
12
+ S3_BUCKET=my-backups
13
+ S3_PREFIX=labpadi
14
+
15
+ # An R2 API token's keys. Read access is enough for drills; backups need Read & Write.
16
+ AWS_ACCESS_KEY_ID=
17
+ AWS_SECRET_ACCESS_KEY=
18
+
19
+ # The PRIVATE key from `keygen` (the AGE-SECRET-KEY-1... line). Keep it secret.
20
+ # Needed for drill, verify, decrypt and restore.
21
+ AGE_IDENTITY=
22
+
23
+ # Optional: only to run `backup` from this folder.
24
+ # DATABASE_URL must be a direct (non-pooled) connection: pg_dump doesn't work through a
25
+ # transaction-mode pooler. For Neon, use the host without "-pooler".
26
+ DATABASE_URL=
27
+ # The PUBLIC key from `keygen` (age1...). Backups are encrypted to it.
28
+ AGE_RECIPIENTS=
29
+
30
+ # Optional: keep using tier folders you already have (see
31
+ # https://pgbackup-r2.iotabyt.com/guides/existing-folders/). Old backups are then deleted by the bucket's lifecycle rules.
32
+ # TIERS="daily=daily/ monthly=monthly/"
33
+ # PRUNE=false
@@ -0,0 +1,153 @@
1
+ # Restore drills with Docker Compose
2
+
3
+ > **Most people should use `pgb drill` instead.** It does the same thing with no compose file: see the [Quick start](https://pgbackup-r2.iotabyt.com/start/quick-start/). This folder is for people who'd rather use plain `docker compose`.
4
+
5
+ This folder restores your latest backup into a throwaway Postgres and checks that every table has the row count recorded at backup time. It works the same on Windows, macOS and Linux. You don't need to install `age`, `pg_dump` or Python.
6
+
7
+ ## 1. Install Docker Desktop
8
+
9
+ Get it from <https://www.docker.com/products/docker-desktop/> and start it. On Linux, Docker Engine with the compose plugin is enough.
10
+
11
+ ## 2. Copy `.env.example` to `.env` and fill it in
12
+
13
+ PowerShell:
14
+
15
+ ```powershell
16
+ cd examples\drill
17
+ Copy-Item .env.example .env
18
+ notepad .env
19
+ ```
20
+
21
+ bash:
22
+
23
+ ```bash
24
+ cd examples/drill
25
+ cp .env.example .env
26
+ nano .env
27
+ ```
28
+
29
+ You need:
30
+
31
+ - `PG_MAJOR`, which must match your server's major version (see below)
32
+ - the R2 endpoint, bucket and prefix
33
+ - an API token's two keys
34
+ - the private key (`AGE_IDENTITY=AGE-SECRET-KEY-1…`) you saved when you ran `keygen`
35
+
36
+ `.env` holds your private key, so never commit it. The repo's `.gitignore` already excludes it.
37
+
38
+ ### Set `PG_MAJOR` to your server's version
39
+
40
+ `PG_MAJOR` picks both images: the scratch database (`postgres:16`) and the tool (`pgbackup-r2:pg16`). It must match the **major** version of the server your backups come from. To check, run this against your database:
41
+
42
+ ```sql
43
+ SHOW server_version;
44
+ ```
45
+
46
+ If the server says `17.4`, set `PG_MAJOR=17`. If you leave `PG_MAJOR` empty, it defaults to 16.
47
+
48
+ Why it matters: a dump made from a newer major version won't restore into an older one. A PG 17 dump restored into a PG 16 scratch database fails. Tools from 17 onwards also send `SET transaction_timeout`, which a 16 server rejects. A matching pair always works.
49
+
50
+ ## 3. Run the drill
51
+
52
+ These commands are the same in PowerShell and bash:
53
+
54
+ ```
55
+ docker compose run --rm drill
56
+ docker compose down -v
57
+ ```
58
+
59
+ The first command starts a scratch Postgres, restores the latest backup into it, and compares row counts against the backup's manifest. The second throws the scratch database away.
60
+
61
+ A pass looks like this:
62
+
63
+ ```
64
+ ✓ Downloaded labpadi/2026-11-18T020000Z.dump.age (48.2 MB) 3.1s
65
+ ✓ Checksum matches 0.2s
66
+ ✓ Decrypted, and it's a pg_dump archive 0.4s
67
+ ✓ Restored into postgresql://***@scratch:5432/drill 21.4s
68
+ ✓ Read the manifest 0.1s
69
+ ✓ Counted restored rows 1.2s
70
+
71
+ table expected restored
72
+ ───────────────────────────────────────────
73
+ public."Readings" 1,204,511 1,204,511 ok
74
+ public.schools 250 250 ok
75
+ …
76
+
77
+ ╭──────────────────────────────────────────────────────────╮
78
+ │ │
79
+ │ PASS labpadi/2026-11-18T020000Z.dump.age │
80
+ │ (48.2 MB, restored in 21.4s) │
81
+ │ 37 tables and 1,284,903 rows match the manifest │
82
+ │ │
83
+ ╰──────────────────────────────────────────────────────────╯
84
+ ```
85
+
86
+ In CI or with `--plain` you get the plain version:
87
+
88
+ ```
89
+ PASS labpadi/2026-11-18T020000Z.dump.age (48.2 MB, restored in 21.4s)
90
+ 37 tables and 1,284,903 rows match the manifest
91
+ ```
92
+
93
+ A failure prints `FAIL` and one line per problem, and exits with code 1.
94
+
95
+ To drill an older backup, give its stamp, which you can find with `pgb list`:
96
+
97
+ ```
98
+ pgb list
99
+ docker compose run --rm drill drill 2026-11-01T020000Z
100
+ ```
101
+
102
+ ## Other things this folder can do
103
+
104
+ These use `pgb`, the short wrapper from [Install pgb](https://pgbackup-r2.iotabyt.com/start/install/). Run them from this folder so `pgb` picks up its `.env`. `pgb drill` works here too: it starts its own throwaway database and ignores `compose.yml`.
105
+
106
+ If you haven't installed `pgb`, `docker compose run --rm drill <command>` does the same. On Linux, add `--user "$(id -u):$(id -g)"` for `decrypt`, so you can write to `./out`.
107
+
108
+ ### Take a backup from your laptop
109
+
110
+ Add two more lines to `.env`, and the same folder can back up too:
111
+
112
+ ```bash
113
+ DATABASE_URL=postgresql://user:password@host:5432/app?sslmode=require
114
+ AGE_RECIPIENTS=age1... # the PUBLIC key from keygen
115
+ ```
116
+
117
+ Then:
118
+
119
+ ```
120
+ pgb backup
121
+ pgb list
122
+ ```
123
+
124
+ `DATABASE_URL` must be a **direct** connection, not one that goes through a pooler. `pg_dump` needs a real session, so a transaction-mode pooler (PgBouncer, Supavisor) breaks it. On Neon, use the connection string whose host doesn't contain `-pooler`.
125
+
126
+ Backing up and drilling from the same `.env` is safe. The drill only ever restores into the scratch database, and it refuses to run if `DRILL_DATABASE_URL` is the same database as `DATABASE_URL`.
127
+
128
+ For nightly backups, run `pgb nightly start` on a server that's always on, or one of the other setups in the [guides](https://pgbackup-r2.iotabyt.com/guides/vps/). A laptop that's asleep at 02:00 won't take them.
129
+
130
+ ### Check a backup without a database
131
+
132
+ Quick check, with no database: does the backup download, decrypt, and contain a real dump?
133
+
134
+ ```
135
+ pgb verify
136
+ ```
137
+
138
+ ### Get a plain dump file
139
+
140
+ Write a plain `pg_dump` file into `./out`, to restore somewhere else or hand to someone:
141
+
142
+ ```
143
+ pgb decrypt latest --out /out/backup.dump
144
+ ```
145
+
146
+ The file appears as `out/backup.dump` next to this README, owned by you. Restore it with `pg_restore --no-owner --no-acl -d <db> out/backup.dump`.
147
+
148
+ To check a `.dump.age` you already downloaded, put it in `./out` and point at it there: `pgb verify --file /out/2026-11-18T020000Z.dump.age`.
149
+
150
+ ## Notes
151
+
152
+ - Change `PG_MAJOR` in `.env` when you upgrade Postgres. Once you upgrade, new backups come from the new major version, and they need the matching images to restore.
153
+ - The scratch database has no published ports, and `down -v` deletes it along with all the restored data.