payload-plugin-backup-postgres 0.0.0-stage → 0.1.0

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 (187) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +355 -2
  3. package/dist/backup-dashboard.css +1149 -0
  4. package/dist/client.d.ts +3 -0
  5. package/dist/client.js +3 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/collections/BackupSettings.d.ts +6 -0
  8. package/dist/collections/BackupSettings.js +91 -0
  9. package/dist/collections/BackupSettings.js.map +1 -0
  10. package/dist/collections/BackupTasks.d.ts +2 -0
  11. package/dist/collections/BackupTasks.js +63 -0
  12. package/dist/collections/BackupTasks.js.map +1 -0
  13. package/dist/components/BackupDashboard/BackupItemActions.client.d.ts +7 -0
  14. package/dist/components/BackupDashboard/BackupItemActions.client.js +90 -0
  15. package/dist/components/BackupDashboard/BackupItemActions.client.js.map +1 -0
  16. package/dist/components/BackupDashboard/BackupListCollapsible.client.d.ts +16 -0
  17. package/dist/components/BackupDashboard/BackupListCollapsible.client.js +597 -0
  18. package/dist/components/BackupDashboard/BackupListCollapsible.client.js.map +1 -0
  19. package/dist/components/BackupDashboard/BackupSettingsModal.client.d.ts +2 -0
  20. package/dist/components/BackupDashboard/BackupSettingsModal.client.js +1027 -0
  21. package/dist/components/BackupDashboard/BackupSettingsModal.client.js.map +1 -0
  22. package/dist/components/BackupDashboard/CollectionBackupPreviewBody.client.d.ts +18 -0
  23. package/dist/components/BackupDashboard/CollectionBackupPreviewBody.client.js +267 -0
  24. package/dist/components/BackupDashboard/CollectionBackupPreviewBody.client.js.map +1 -0
  25. package/dist/components/BackupDashboard/ManualBackupDialog.client.d.ts +1 -0
  26. package/dist/components/BackupDashboard/ManualBackupDialog.client.js +276 -0
  27. package/dist/components/BackupDashboard/ManualBackupDialog.client.js.map +1 -0
  28. package/dist/components/BackupDashboard/RestoreBackupDialog.client.d.ts +6 -0
  29. package/dist/components/BackupDashboard/RestoreBackupDialog.client.js +551 -0
  30. package/dist/components/BackupDashboard/RestoreBackupDialog.client.js.map +1 -0
  31. package/dist/components/BackupDashboard/TaskActionButton.client.d.ts +29 -0
  32. package/dist/components/BackupDashboard/TaskActionButton.client.js +273 -0
  33. package/dist/components/BackupDashboard/TaskActionButton.client.js.map +1 -0
  34. package/dist/components/BackupDashboard/backupDashboardInlineCss.d.ts +1 -0
  35. package/dist/components/BackupDashboard/backupDashboardInlineCss.js +3 -0
  36. package/dist/components/BackupDashboard/backupDashboardInlineCss.js.map +1 -0
  37. package/dist/components/BackupDashboard/index.client.d.ts +8 -0
  38. package/dist/components/BackupDashboard/index.client.js +9 -0
  39. package/dist/components/BackupDashboard/index.client.js.map +1 -0
  40. package/dist/components/BackupDashboard/index.d.ts +15 -0
  41. package/dist/components/BackupDashboard/index.js +246 -0
  42. package/dist/components/BackupDashboard/index.js.map +1 -0
  43. package/dist/core/archive.d.ts +8 -0
  44. package/dist/core/archive.js +47 -0
  45. package/dist/core/archive.js.map +1 -0
  46. package/dist/core/backup.d.ts +54 -0
  47. package/dist/core/backup.js +282 -0
  48. package/dist/core/backup.js.map +1 -0
  49. package/dist/core/backupArchiveRead.d.ts +15 -0
  50. package/dist/core/backupArchiveRead.js +30 -0
  51. package/dist/core/backupArchiveRead.js.map +1 -0
  52. package/dist/core/backupBlobIO.d.ts +54 -0
  53. package/dist/core/backupBlobIO.js +179 -0
  54. package/dist/core/backupBlobIO.js.map +1 -0
  55. package/dist/core/backupBlobTransfer.d.ts +29 -0
  56. package/dist/core/backupBlobTransfer.js +85 -0
  57. package/dist/core/backupBlobTransfer.js.map +1 -0
  58. package/dist/core/backupSettings.d.ts +35 -0
  59. package/dist/core/backupSettings.js +100 -0
  60. package/dist/core/backupSettings.js.map +1 -0
  61. package/dist/core/backupSourcePreview.d.ts +10 -0
  62. package/dist/core/backupSourcePreview.js +46 -0
  63. package/dist/core/backupSourcePreview.js.map +1 -0
  64. package/dist/core/blobTokenValidate.d.ts +14 -0
  65. package/dist/core/blobTokenValidate.js +75 -0
  66. package/dist/core/blobTokenValidate.js.map +1 -0
  67. package/dist/core/db.d.ts +56 -0
  68. package/dist/core/db.js +161 -0
  69. package/dist/core/db.js.map +1 -0
  70. package/dist/core/pgArchive.d.ts +37 -0
  71. package/dist/core/pgArchive.js +57 -0
  72. package/dist/core/pgArchive.js.map +1 -0
  73. package/dist/core/restore.d.ts +46 -0
  74. package/dist/core/restore.js +439 -0
  75. package/dist/core/restore.js.map +1 -0
  76. package/dist/core/restorePreview.d.ts +76 -0
  77. package/dist/core/restorePreview.js +222 -0
  78. package/dist/core/restorePreview.js.map +1 -0
  79. package/dist/core/restoreResult.d.ts +34 -0
  80. package/dist/core/restoreResult.js +45 -0
  81. package/dist/core/restoreResult.js.map +1 -0
  82. package/dist/core/storage/config.d.ts +31 -0
  83. package/dist/core/storage/config.js +60 -0
  84. package/dist/core/storage/config.js.map +1 -0
  85. package/dist/core/storage/index.d.ts +23 -0
  86. package/dist/core/storage/index.js +25 -0
  87. package/dist/core/storage/index.js.map +1 -0
  88. package/dist/core/storage/s3.d.ts +35 -0
  89. package/dist/core/storage/s3.js +200 -0
  90. package/dist/core/storage/s3.js.map +1 -0
  91. package/dist/core/storage/types.d.ts +60 -0
  92. package/dist/core/storage/types.js +6 -0
  93. package/dist/core/storage/types.js.map +1 -0
  94. package/dist/core/storage/vercelBlob.d.ts +25 -0
  95. package/dist/core/storage/vercelBlob.js +53 -0
  96. package/dist/core/storage/vercelBlob.js.map +1 -0
  97. package/dist/core/tableGroups.d.ts +25 -0
  98. package/dist/core/tableGroups.js +80 -0
  99. package/dist/core/tableGroups.js.map +1 -0
  100. package/dist/core/taskProgress.d.ts +39 -0
  101. package/dist/core/taskProgress.js +104 -0
  102. package/dist/core/taskProgress.js.map +1 -0
  103. package/dist/core/vercelBackupCron.d.ts +8 -0
  104. package/dist/core/vercelBackupCron.js +38 -0
  105. package/dist/core/vercelBackupCron.js.map +1 -0
  106. package/dist/endpoints/index.d.ts +3 -0
  107. package/dist/endpoints/index.js +29 -0
  108. package/dist/endpoints/index.js.map +1 -0
  109. package/dist/endpoints/paths/admin-backup-download.d.ts +8 -0
  110. package/dist/endpoints/paths/admin-backup-download.js +73 -0
  111. package/dist/endpoints/paths/admin-backup-download.js.map +1 -0
  112. package/dist/endpoints/paths/admin-delete.d.ts +3 -0
  113. package/dist/endpoints/paths/admin-delete.js +61 -0
  114. package/dist/endpoints/paths/admin-delete.js.map +1 -0
  115. package/dist/endpoints/paths/admin-manual.d.ts +3 -0
  116. package/dist/endpoints/paths/admin-manual.js +62 -0
  117. package/dist/endpoints/paths/admin-manual.js.map +1 -0
  118. package/dist/endpoints/paths/admin-preview.d.ts +6 -0
  119. package/dist/endpoints/paths/admin-preview.js +109 -0
  120. package/dist/endpoints/paths/admin-preview.js.map +1 -0
  121. package/dist/endpoints/paths/admin-restore.d.ts +3 -0
  122. package/dist/endpoints/paths/admin-restore.js +77 -0
  123. package/dist/endpoints/paths/admin-restore.js.map +1 -0
  124. package/dist/endpoints/paths/admin-settings.d.ts +3 -0
  125. package/dist/endpoints/paths/admin-settings.js +263 -0
  126. package/dist/endpoints/paths/admin-settings.js.map +1 -0
  127. package/dist/endpoints/paths/admin-task.d.ts +3 -0
  128. package/dist/endpoints/paths/admin-task.js +34 -0
  129. package/dist/endpoints/paths/admin-task.js.map +1 -0
  130. package/dist/endpoints/paths/admin-validate-blob-token.d.ts +8 -0
  131. package/dist/endpoints/paths/admin-validate-blob-token.js +26 -0
  132. package/dist/endpoints/paths/admin-validate-blob-token.js.map +1 -0
  133. package/dist/endpoints/paths/cron-list.d.ts +2 -0
  134. package/dist/endpoints/paths/cron-list.js +34 -0
  135. package/dist/endpoints/paths/cron-list.js.map +1 -0
  136. package/dist/endpoints/paths/cron-restore.d.ts +2 -0
  137. package/dist/endpoints/paths/cron-restore.js +71 -0
  138. package/dist/endpoints/paths/cron-restore.js.map +1 -0
  139. package/dist/endpoints/paths/cron-run.d.ts +3 -0
  140. package/dist/endpoints/paths/cron-run.js +46 -0
  141. package/dist/endpoints/paths/cron-run.js.map +1 -0
  142. package/dist/endpoints/shared.d.ts +12 -0
  143. package/dist/endpoints/shared.js +52 -0
  144. package/dist/endpoints/shared.js.map +1 -0
  145. package/dist/exports/client.d.ts +1 -0
  146. package/dist/exports/client.js +3 -0
  147. package/dist/exports/client.js.map +1 -0
  148. package/dist/exports/rsc.d.ts +1 -0
  149. package/dist/exports/rsc.js +3 -0
  150. package/dist/exports/rsc.js.map +1 -0
  151. package/dist/index.d.ts +25 -0
  152. package/dist/index.js +18 -0
  153. package/dist/index.js.map +1 -0
  154. package/dist/plugin.d.ts +3 -0
  155. package/dist/plugin.js +69 -0
  156. package/dist/plugin.js.map +1 -0
  157. package/dist/publicApiPaths.d.ts +18 -0
  158. package/dist/publicApiPaths.js +19 -0
  159. package/dist/publicApiPaths.js.map +1 -0
  160. package/dist/types.d.ts +16 -0
  161. package/dist/types.js +3 -0
  162. package/dist/types.js.map +1 -0
  163. package/dist/utils/backupSelection.d.ts +6 -0
  164. package/dist/utils/backupSelection.js +16 -0
  165. package/dist/utils/backupSelection.js.map +1 -0
  166. package/dist/utils/blobName.d.ts +32 -0
  167. package/dist/utils/blobName.js +99 -0
  168. package/dist/utils/blobName.js.map +1 -0
  169. package/dist/utils/dashboardRoleAccess.d.ts +23 -0
  170. package/dist/utils/dashboardRoleAccess.js +64 -0
  171. package/dist/utils/dashboardRoleAccess.js.map +1 -0
  172. package/dist/utils/dialogBackdrop.d.ts +9 -0
  173. package/dist/utils/dialogBackdrop.js +17 -0
  174. package/dist/utils/dialogBackdrop.js.map +1 -0
  175. package/dist/utils/formatBytes.d.ts +2 -0
  176. package/dist/utils/formatBytes.js +20 -0
  177. package/dist/utils/formatBytes.js.map +1 -0
  178. package/dist/utils/hostname.d.ts +4 -0
  179. package/dist/utils/hostname.js +32 -0
  180. package/dist/utils/hostname.js.map +1 -0
  181. package/dist/utils/index.d.ts +5 -0
  182. package/dist/utils/index.js +7 -0
  183. package/dist/utils/index.js.map +1 -0
  184. package/dist/utils/maskBlobToken.d.ts +11 -0
  185. package/dist/utils/maskBlobToken.js +42 -0
  186. package/dist/utils/maskBlobToken.js.map +1 -0
  187. package/package.json +140 -3
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kfir
4
+ Portions copyright (c) trieb.work (@trieb.work/payload-plugin-backup-mongodb, MIT)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,356 @@
1
- # Temporary Holding Version
1
+ # payload-plugin-backup-postgres
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Backup and restore for **Payload CMS 3 on PostgreSQL**: scheduled (cron) and
4
+ on-demand backups to **Vercel Blob** or **S3-compatible storage** (AWS S3,
5
+ Cloudflare R2, MinIO), an admin dashboard with per-collection preview, selective
6
+ restore, and optional media bundling.
7
+
8
+ This is a Postgres port of
9
+ [`@trieb.work/payload-plugin-backup-mongodb`](https://github.com/trieb-work/payload-plugin-backup-mongodb)
10
+ (MIT). The admin UI, endpoints, storage layer and task/progress model are kept;
11
+ the database engine was rewritten for Payload's Drizzle/Postgres adapter.
12
+
13
+ ## How it differs from the MongoDB plugin
14
+
15
+ | | MongoDB plugin | This plugin |
16
+ | -------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
17
+ | Adapter | `@payloadcms/db-mongodb` | `@payloadcms/db-postgres`, `@payloadcms/db-vercel-postgres` |
18
+ | Unit of backup | Mongo collection | Postgres table, grouped per Payload collection/global in the UI (`posts`, `posts_locales`, `posts_rels`, `_posts_v`, … toggle together) |
19
+ | Snapshot | collection by collection | **one `REPEATABLE READ READ ONLY` transaction** – all tables from the same instant |
20
+ | Serialization | EJSON | rows serialized by Postgres (`json_agg`) and restored with `json_populate_recordset`; `numeric`, `bigint`, `jsonb`, arrays, `bytea`, timestamps round-trip exactly |
21
+ | Restore | per collection, best effort | **atomic by default** (single transaction, rolled back if any table fails); FK-aware ordering, cycles and self references handled, sequences reset |
22
+ | Merge restore | upsert by `_id` / unique fields | per **document**: rows of documents in the backup replace the live ones (incl. their locales, arrays, blocks, rels); other documents are kept |
23
+ | Progress-row cleanup | TTL index | pruned on start-up and on each new task (> 30 min) |
24
+ | Settings field | `skipMongoCollections` | `skipTables` |
25
+ | Routes | `/api/backup-mongodb/...` | `/api/backup-postgres/...` |
26
+
27
+ Backups are **not** interchangeable between the two plugins.
28
+
29
+ ## Requirements
30
+
31
+ - Payload `^3`, Next.js 15/16, React 19, Node `>=20.9`
32
+ - `@payloadcms/db-postgres` or `@payloadcms/db-vercel-postgres` (the plugin uses
33
+ the adapter's `pool` and `schemaName`)
34
+ - A backup target: Vercel Blob (`BLOB_READ_WRITE_TOKEN`) or S3
35
+ (`BACKUP_STORAGE=s3`)
36
+
37
+ ### Environment variables
38
+
39
+ | Variable | Purpose |
40
+ | ------------------------------------------------ | ------------------------------------------------------------------------------ |
41
+ | `BLOB_READ_WRITE_TOKEN` | Vercel Blob token (backup target and/or media source) |
42
+ | `CRON_SECRET` | Bearer secret for `/api/backup-postgres/cron/*` |
43
+ | `BACKUPS_TO_KEEP` | Cron retention (default 10) |
44
+ | `BACKUP_STORAGE`, `BACKUP_S3_*` | S3 target, see below |
45
+ | `PAYLOAD_BACKUP_ALLOWED_ROLES` | Comma-separated roles (or `*`) that see the dashboard |
46
+ | `DATABASE_URI` / `DATABASE_URL` / `POSTGRES_URL` | Only used to label backups with `host/dbname` (credentials are never included) |
47
+
48
+ ## Installation
49
+
50
+ ```bash
51
+ pnpm add payload-plugin-backup-postgres
52
+ ```
53
+
54
+ ```ts
55
+ // payload.config.ts
56
+ import { postgresAdapter } from '@payloadcms/db-postgres'
57
+ import { backupPostgresPlugin } from 'payload-plugin-backup-postgres'
58
+
59
+ export default buildConfig({
60
+ db: postgresAdapter({ pool: { connectionString: process.env.DATABASE_URI } }),
61
+ plugins: [
62
+ backupPostgresPlugin({
63
+ // backupsToKeep: 14,
64
+ // access: (user) => user?.role === 'admin',
65
+ }),
66
+ ],
67
+ })
68
+ ```
69
+
70
+ Then:
71
+
72
+ 1. **Create a migration** if you run with migrations (`push: false`, i.e.
73
+ production): the plugin adds the `backup-tasks` and `backup-settings`
74
+ collections (`backup_tasks`, `backup_settings`,
75
+ `backup_settings_skip_tables`).
76
+ `pnpm payload migrate:create add-backup-plugin && pnpm payload migrate`
77
+ 2. Regenerate the import map: `pnpm payload generate:importmap`
78
+ 3. Optional cron (Vercel):
79
+
80
+ ```json
81
+ {
82
+ "crons": [
83
+ { "path": "/api/backup-postgres/cron/run", "schedule": "0 3 * * *" }
84
+ ]
85
+ }
86
+ ```
87
+
88
+ ## Restore semantics (Postgres)
89
+
90
+ - **Replace (default):** for each restored table, rows not present in the backup
91
+ are deleted (children first), then backup rows are upserted (parents first).
92
+ - **Merge:** documents present in the backup are reset to their backed-up state
93
+ (their child rows in `<table>_locales`, `_rels`, arrays and blocks are
94
+ replaced); documents created after the backup are kept. Live rows that collide
95
+ with a backup row on a unique key under a different id (e.g. a user re-created
96
+ with the same email) are evicted.
97
+ - **Ordering:** tables are ordered from the live foreign-key graph. Self
98
+ references (nested categories/folders) and cycles (e.g.
99
+ `users.avatar → media`, `media.owner → users`) are written as `NULL` first and
100
+ linked in a second pass.
101
+ - **Schema drift:** only columns present in both the backup and the current
102
+ table are written; new columns get their defaults. Tables that no longer exist
103
+ are reported as warnings without aborting. Run your migrations before
104
+ restoring an older backup.
105
+ - **Sequences** owned by restored tables are set to `max(id)`.
106
+ - **Atomic:** any SQL failure rolls the whole database restore back (reported
107
+ per table). Programmatic callers can pass `{ atomic: false }` to keep the
108
+ tables that succeeded.
109
+ - `backup-tasks` rows are never replaced while a tracked restore is running.
110
+
111
+ Archive format: `.json` (no media) or `.tar.gz` (`database.json` +
112
+ `media/<pathname>`), where `database.json` is
113
+ `{ format: "payload-plugin-backup-postgres", version: 1, schema, serverVersion, createdAt, tables: { [name]: { rowCount, rows } } }`
114
+ and `rows` is the JSON text produced by Postgres.
115
+
116
+ Media bundling reads `filename`, `sizes_*_filename` (and `prefix`, if present)
117
+ from every upload-enabled collection, fetched from the Vercel Blob upload store.
118
+
119
+ ## Choosing a backup target (Vercel Blob or S3)
120
+
121
+ Backups can be stored in **Vercel Blob** (default) or **AWS S3**. The S3 target
122
+ also works with any S3-compatible store — **Cloudflare R2**, **MinIO**, etc. —
123
+ because they share the same API; only the configuration differs, never the
124
+ plugin code.
125
+
126
+ Select the target with `BACKUP_STORAGE`. When unset it stays `vercel-blob`, so
127
+ existing deployments are unaffected.
128
+
129
+ ### Using S3
130
+
131
+ S3 is enabled when `BACKUP_STORAGE=s3`. Install the AWS SDK (declared as
132
+ optional peer dependencies, so Vercel-only installs don't pull it in):
133
+
134
+ ```bash
135
+ pnpm add @aws-sdk/client-s3 @aws-sdk/lib-storage @aws-sdk/s3-request-presigner
136
+ ```
137
+
138
+ | Variable | Required | Purpose |
139
+ | ----------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------- |
140
+ | `BACKUP_STORAGE` | yes | Set to `s3`. |
141
+ | `BACKUP_S3_BUCKET` | yes | Target bucket name. |
142
+ | `BACKUP_S3_REGION` | optional | Region. Falls back to `AWS_REGION`, then `us-east-1`. |
143
+ | `BACKUP_S3_ENDPOINT` | optional | Custom endpoint for S3-compatible stores (R2, MinIO). Omit for AWS S3. |
144
+ | `BACKUP_S3_FORCE_PATH_STYLE` | optional | Path-style addressing. Defaults to `true` when an endpoint is set (needed for MinIO and some R2 setups). |
145
+ | `BACKUP_S3_PREFIX` | optional | Key namespace prepended to every object (for sharing a bucket). Backups still list/restore by their `backups/…` name. |
146
+ | `BACKUP_S3_ACCESS_KEY_ID` | optional | Access key. Falls back to `AWS_ACCESS_KEY_ID`. Omit to use the AWS default credential chain (IAM roles, etc.). |
147
+ | `BACKUP_S3_SECRET_ACCESS_KEY` | optional | Secret key. Falls back to `AWS_SECRET_ACCESS_KEY`. |
148
+ | `BACKUP_S3_SESSION_TOKEN` | optional | Session token for temporary credentials. Falls back to `AWS_SESSION_TOKEN`. |
149
+
150
+ Credentials are optional: when omitted, the AWS SDK's default provider chain is
151
+ used (environment, shared config, or instance/role credentials — recommended on
152
+ AWS).
153
+
154
+ **AWS S3**
155
+
156
+ ```bash
157
+ BACKUP_STORAGE=s3
158
+ BACKUP_S3_BUCKET=my-payload-backups
159
+ BACKUP_S3_REGION=eu-central-1
160
+ # Credentials via IAM role (recommended) or:
161
+ # BACKUP_S3_ACCESS_KEY_ID=...
162
+ # BACKUP_S3_SECRET_ACCESS_KEY=...
163
+ ```
164
+
165
+ **Cloudflare R2**
166
+
167
+ ```bash
168
+ BACKUP_STORAGE=s3
169
+ BACKUP_S3_BUCKET=my-payload-backups
170
+ BACKUP_S3_REGION=auto
171
+ BACKUP_S3_ENDPOINT=https://<account-id>.r2.cloudflarestorage.com
172
+ BACKUP_S3_FORCE_PATH_STYLE=true
173
+ BACKUP_S3_ACCESS_KEY_ID=<r2-access-key-id>
174
+ BACKUP_S3_SECRET_ACCESS_KEY=<r2-secret-access-key>
175
+ ```
176
+
177
+ Backups are stored under the same `backups/…` keys (see
178
+ [Blob naming](#blob-naming)), so listing and restore work identically across
179
+ targets. Downloads stream through the plugin's authenticated endpoint, so the
180
+ bucket does **not** need to be public.
181
+
182
+ > **Note on media:** the "include media" option bundles files from your Payload
183
+ > **Vercel Blob upload store** (`BLOB_READ_WRITE_TOKEN`). The configurable
184
+ > target above governs where backup **archives** live only; media is always read
185
+ > from and restored to the upload store, even when `BACKUP_STORAGE=s3`.
186
+
187
+ ---
188
+
189
+ ## Admin UI
190
+
191
+ After the first login as an admin user, a **Backups** section appears below the
192
+ default Payload dashboard. Day-to-day tasks — create, download, restore, delete,
193
+ schedule — all happen in the UI below.
194
+
195
+ **Who can see it?** The visibility rules, in precedence order:
196
+
197
+ 1. If the plugin is registered with an `access` function, it wins (see
198
+ [Plugin options](#plugin-options)).
199
+ 2. Otherwise, if `PAYLOAD_BACKUP_ALLOWED_ROLES` is set, it is used as a
200
+ comma-separated allow-list of role slugs (case-insensitive). Use `*` for "any
201
+ authenticated user". Example:
202
+ `PAYLOAD_BACKUP_ALLOWED_ROLES=admin,superadmin`.
203
+ 3. Otherwise, the historical default applies: users with a `role` whose slug is
204
+ `admin` see the dashboard; projects that don't use a `roles` field at all get
205
+ the dashboard for any authenticated user.
206
+
207
+ ### Scheduled (cron) vs on-demand (manual) backups
208
+
209
+ - **Scheduled / cron** backups follow shared settings: retention, media toggle,
210
+ and collections to skip. Older cron archives are pruned when the keep-count is
211
+ exceeded. They appear in the list as **CRON BACKUP** (or similar).
212
+ - **Manual / on-demand** backups are configured **per run** — per-collection
213
+ toggles, optional media in a `.tar.gz` archive. Manual backups are not
214
+ auto-pruned; use them as checkpoints before migrations.
215
+
216
+ ![Manual backup dialog with per-collection selection and optional media bundling](./docs/screenshots/manual-backup.png)
217
+
218
+ ### Selective restore with per-collection preview
219
+
220
+ **Restore** opens a preview of the archive before any write. You can restore
221
+ everything, cherry-pick collections, or skip media from `.tar.gz` when you only
222
+ need database rows. Restoring from a different host or database is supported;
223
+ filters help when cloning production into staging.
224
+
225
+ ![Restore dialog previewing document counts per collection](./docs/screenshots/restore.png)
226
+
227
+ ### Scheduled backup settings
228
+
229
+ Retention, storage token, and cron collection skip list live in one modal:
230
+
231
+ ![Scheduled backup settings: cron schedule, retention, blob token, collection skip list](./docs/screenshots/settings.png)
232
+
233
+ - **Schedule:** when `vercel.json` is present, the plugin can show a
234
+ human-readable cron description (via
235
+ [cronstrue](https://github.com/bradymholt/cronstrue)).
236
+ - **Retention:** how many **cron** archives to keep; manual backups are not
237
+ pruned by this.
238
+ - **Dedicated backup storage (optional):** a separate Vercel Blob read/write
239
+ token (when `BACKUP_STORAGE=vercel-blob`) so backups can live in a different
240
+ store than media. The UI can validate the token and optionally copy existing
241
+ `backups/*` objects to the new store. For S3, configure the target bucket via
242
+ environment variables.
243
+ - **Collection selection for cron:** defaults to all collections, with
244
+ per-collection opt-out.
245
+
246
+ ---
247
+
248
+ ## Plugin options
249
+
250
+ ```ts
251
+ type BackupPluginOptions = {
252
+ /**
253
+ * Set to `false` to disable the plugin (no collections, endpoints, or admin UI).
254
+ * Omit or leave unset to keep the plugin active.
255
+ */
256
+ enabled?: boolean
257
+
258
+ /** Default cron retention when no value is stored in settings. Falls back to `BACKUPS_TO_KEEP` or `10`. */
259
+ backupsToKeep?: number
260
+
261
+ /**
262
+ * Custom access check for admin routes and the dashboard. Overrides the
263
+ * `PAYLOAD_BACKUP_ALLOWED_ROLES` env var when provided.
264
+ *
265
+ * Default (env-based): see the `PAYLOAD_BACKUP_ALLOWED_ROLES` entry in the
266
+ * environment variables table. When neither is configured, the check falls back
267
+ * to a role with slug `admin`, or allows any authenticated user in projects
268
+ * without a `roles` field.
269
+ */
270
+ access?: (user: Record<string, unknown> | null) => boolean
271
+ }
272
+ ```
273
+
274
+ ---
275
+
276
+ ## HTTP API (Payload REST)
277
+
278
+ All routes are served by Payload’s `/api/[...slug]` handler under
279
+ **`/api/backup-postgres/…`**. The `backup-postgres` prefix avoids clashing with
280
+ a collection slug. The admin UI uses the same URLs; use the exported
281
+ `backupPluginPublicApiPaths` helper in client code.
282
+
283
+ ### Cron / external (Bearer `CRON_SECRET`)
284
+
285
+ ```http
286
+ GET /api/backup-postgres/cron/run # enqueue a cron backup
287
+ GET /api/backup-postgres/cron/list # list backups
288
+ POST /api/backup-postgres/cron/restore # body: { "url": "https://…" }
289
+ Authorization: Bearer <CRON_SECRET>
290
+ ```
291
+
292
+ ### Admin (session cookie, or `pollSecret` for `/task/:id` where applicable)
293
+
294
+ | Method | Path |
295
+ | -------------- | ------------------------------------------------ |
296
+ | `POST` | `/api/backup-postgres/admin/manual` |
297
+ | `POST` | `/api/backup-postgres/admin/restore` |
298
+ | `POST` | `/api/backup-postgres/admin/backup-preview` |
299
+ | `POST` | `/api/backup-postgres/admin/restore-preview` |
300
+ | `POST` | `/api/backup-postgres/admin/delete` |
301
+ | `GET` | `/api/backup-postgres/admin/backup-download` |
302
+ | `GET` | `/api/backup-postgres/admin/task/:id` |
303
+ | `GET` / `POST` | `/api/backup-postgres/admin/settings` |
304
+ | `POST` | `/api/backup-postgres/admin/validate-blob-token` |
305
+
306
+ ---
307
+
308
+ ## Blob storage model
309
+
310
+ ### Blob naming
311
+
312
+ ```
313
+
314
+ backups/{type}---{dbName}---{hostname}---{collectionCount}-{timestampMs}.{json|tar.gz}
315
+
316
+ ```
317
+
318
+ - `type`: `cron` or `manual` (and legacy-style labels where applicable).
319
+ - `dbName` / `hostname`: derived from your Postgres connection string
320
+ (`host/dbname`) and public server URL; URL-encoded in the name.
321
+ - `collectionCount` + `timestampMs`: sort key for listing; legacy blob names are
322
+ still supported.
323
+
324
+ ### Public vs private stores
325
+
326
+ A validation step probes the bucket and records whether the store behaves as
327
+ public or private. New uploads use the right access mode. Restore and preview
328
+ flows handle both.
329
+
330
+ ### Overriding the backup store
331
+
332
+ To keep backups in a **different** Vercel Blob project than media (when
333
+ `BACKUP_STORAGE=vercel-blob`), open **Backup settings** in the admin, paste a
334
+ dedicated `BLOB_READ_WRITE_TOKEN`, validate, and optionally migrate existing
335
+ `backups/*` objects to the new store before switching. For S3, the target is
336
+ controlled entirely by environment variables.
337
+
338
+ ## Running the tests
339
+
340
+ The integration suite boots a real Payload instance on Postgres (localized
341
+ fields, drafts/versions, blocks, arrays, hasMany relationships, a
342
+ self-referencing FK, a `users ↔ media` FK cycle, a global, and a `numeric`
343
+ beyond double precision) and runs backup → mutate → restore round-trips against
344
+ an in-memory storage adapter.
345
+
346
+ ```bash
347
+ createdb payload_test # or: TEST_DATABASE_URL=postgres://... pnpm test
348
+ pnpm test
349
+ ```
350
+
351
+ The test drops and recreates the `public` schema of the target database. Point
352
+ it at a throwaway database.
353
+
354
+ ## License
355
+
356
+ MIT. Based on `@trieb.work/payload-plugin-backup-mongodb` by trieb.work (MIT).