payload-plugin-backup-postgres 0.0.0-stage → 0.2.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.
- package/LICENSE +22 -0
- package/README.md +355 -2
- package/dist/backup-dashboard.css +1149 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +3 -0
- package/dist/client.js.map +1 -0
- package/dist/collections/BackupSettings.d.ts +6 -0
- package/dist/collections/BackupSettings.js +91 -0
- package/dist/collections/BackupSettings.js.map +1 -0
- package/dist/collections/BackupTasks.d.ts +2 -0
- package/dist/collections/BackupTasks.js +63 -0
- package/dist/collections/BackupTasks.js.map +1 -0
- package/dist/components/BackupDashboard/BackupItemActions.client.d.ts +7 -0
- package/dist/components/BackupDashboard/BackupItemActions.client.js +90 -0
- package/dist/components/BackupDashboard/BackupItemActions.client.js.map +1 -0
- package/dist/components/BackupDashboard/BackupListCollapsible.client.d.ts +16 -0
- package/dist/components/BackupDashboard/BackupListCollapsible.client.js +597 -0
- package/dist/components/BackupDashboard/BackupListCollapsible.client.js.map +1 -0
- package/dist/components/BackupDashboard/BackupSettingsModal.client.d.ts +2 -0
- package/dist/components/BackupDashboard/BackupSettingsModal.client.js +1027 -0
- package/dist/components/BackupDashboard/BackupSettingsModal.client.js.map +1 -0
- package/dist/components/BackupDashboard/CollectionBackupPreviewBody.client.d.ts +18 -0
- package/dist/components/BackupDashboard/CollectionBackupPreviewBody.client.js +267 -0
- package/dist/components/BackupDashboard/CollectionBackupPreviewBody.client.js.map +1 -0
- package/dist/components/BackupDashboard/ManualBackupDialog.client.d.ts +1 -0
- package/dist/components/BackupDashboard/ManualBackupDialog.client.js +276 -0
- package/dist/components/BackupDashboard/ManualBackupDialog.client.js.map +1 -0
- package/dist/components/BackupDashboard/RestoreBackupDialog.client.d.ts +6 -0
- package/dist/components/BackupDashboard/RestoreBackupDialog.client.js +551 -0
- package/dist/components/BackupDashboard/RestoreBackupDialog.client.js.map +1 -0
- package/dist/components/BackupDashboard/TaskActionButton.client.d.ts +29 -0
- package/dist/components/BackupDashboard/TaskActionButton.client.js +273 -0
- package/dist/components/BackupDashboard/TaskActionButton.client.js.map +1 -0
- package/dist/components/BackupDashboard/backupDashboardInlineCss.d.ts +1 -0
- package/dist/components/BackupDashboard/backupDashboardInlineCss.js +3 -0
- package/dist/components/BackupDashboard/backupDashboardInlineCss.js.map +1 -0
- package/dist/components/BackupDashboard/index.client.d.ts +8 -0
- package/dist/components/BackupDashboard/index.client.js +9 -0
- package/dist/components/BackupDashboard/index.client.js.map +1 -0
- package/dist/components/BackupDashboard/index.d.ts +15 -0
- package/dist/components/BackupDashboard/index.js +246 -0
- package/dist/components/BackupDashboard/index.js.map +1 -0
- package/dist/core/archive.d.ts +8 -0
- package/dist/core/archive.js +47 -0
- package/dist/core/archive.js.map +1 -0
- package/dist/core/backup.d.ts +54 -0
- package/dist/core/backup.js +282 -0
- package/dist/core/backup.js.map +1 -0
- package/dist/core/backupArchiveRead.d.ts +15 -0
- package/dist/core/backupArchiveRead.js +30 -0
- package/dist/core/backupArchiveRead.js.map +1 -0
- package/dist/core/backupBlobIO.d.ts +54 -0
- package/dist/core/backupBlobIO.js +179 -0
- package/dist/core/backupBlobIO.js.map +1 -0
- package/dist/core/backupBlobTransfer.d.ts +29 -0
- package/dist/core/backupBlobTransfer.js +85 -0
- package/dist/core/backupBlobTransfer.js.map +1 -0
- package/dist/core/backupSettings.d.ts +35 -0
- package/dist/core/backupSettings.js +100 -0
- package/dist/core/backupSettings.js.map +1 -0
- package/dist/core/backupSourcePreview.d.ts +10 -0
- package/dist/core/backupSourcePreview.js +46 -0
- package/dist/core/backupSourcePreview.js.map +1 -0
- package/dist/core/blobTokenValidate.d.ts +14 -0
- package/dist/core/blobTokenValidate.js +75 -0
- package/dist/core/blobTokenValidate.js.map +1 -0
- package/dist/core/db.d.ts +56 -0
- package/dist/core/db.js +161 -0
- package/dist/core/db.js.map +1 -0
- package/dist/core/pgArchive.d.ts +37 -0
- package/dist/core/pgArchive.js +57 -0
- package/dist/core/pgArchive.js.map +1 -0
- package/dist/core/restore.d.ts +46 -0
- package/dist/core/restore.js +439 -0
- package/dist/core/restore.js.map +1 -0
- package/dist/core/restorePreview.d.ts +76 -0
- package/dist/core/restorePreview.js +222 -0
- package/dist/core/restorePreview.js.map +1 -0
- package/dist/core/restoreResult.d.ts +34 -0
- package/dist/core/restoreResult.js +45 -0
- package/dist/core/restoreResult.js.map +1 -0
- package/dist/core/storage/config.d.ts +31 -0
- package/dist/core/storage/config.js +60 -0
- package/dist/core/storage/config.js.map +1 -0
- package/dist/core/storage/index.d.ts +23 -0
- package/dist/core/storage/index.js +25 -0
- package/dist/core/storage/index.js.map +1 -0
- package/dist/core/storage/s3.d.ts +35 -0
- package/dist/core/storage/s3.js +200 -0
- package/dist/core/storage/s3.js.map +1 -0
- package/dist/core/storage/types.d.ts +60 -0
- package/dist/core/storage/types.js +6 -0
- package/dist/core/storage/types.js.map +1 -0
- package/dist/core/storage/vercelBlob.d.ts +25 -0
- package/dist/core/storage/vercelBlob.js +53 -0
- package/dist/core/storage/vercelBlob.js.map +1 -0
- package/dist/core/tableGroups.d.ts +25 -0
- package/dist/core/tableGroups.js +80 -0
- package/dist/core/tableGroups.js.map +1 -0
- package/dist/core/taskProgress.d.ts +39 -0
- package/dist/core/taskProgress.js +104 -0
- package/dist/core/taskProgress.js.map +1 -0
- package/dist/core/vercelBackupCron.d.ts +8 -0
- package/dist/core/vercelBackupCron.js +38 -0
- package/dist/core/vercelBackupCron.js.map +1 -0
- package/dist/endpoints/index.d.ts +3 -0
- package/dist/endpoints/index.js +29 -0
- package/dist/endpoints/index.js.map +1 -0
- package/dist/endpoints/paths/admin-backup-download.d.ts +8 -0
- package/dist/endpoints/paths/admin-backup-download.js +73 -0
- package/dist/endpoints/paths/admin-backup-download.js.map +1 -0
- package/dist/endpoints/paths/admin-delete.d.ts +3 -0
- package/dist/endpoints/paths/admin-delete.js +61 -0
- package/dist/endpoints/paths/admin-delete.js.map +1 -0
- package/dist/endpoints/paths/admin-manual.d.ts +3 -0
- package/dist/endpoints/paths/admin-manual.js +62 -0
- package/dist/endpoints/paths/admin-manual.js.map +1 -0
- package/dist/endpoints/paths/admin-preview.d.ts +6 -0
- package/dist/endpoints/paths/admin-preview.js +109 -0
- package/dist/endpoints/paths/admin-preview.js.map +1 -0
- package/dist/endpoints/paths/admin-restore.d.ts +3 -0
- package/dist/endpoints/paths/admin-restore.js +77 -0
- package/dist/endpoints/paths/admin-restore.js.map +1 -0
- package/dist/endpoints/paths/admin-settings.d.ts +3 -0
- package/dist/endpoints/paths/admin-settings.js +263 -0
- package/dist/endpoints/paths/admin-settings.js.map +1 -0
- package/dist/endpoints/paths/admin-task.d.ts +3 -0
- package/dist/endpoints/paths/admin-task.js +34 -0
- package/dist/endpoints/paths/admin-task.js.map +1 -0
- package/dist/endpoints/paths/admin-validate-blob-token.d.ts +8 -0
- package/dist/endpoints/paths/admin-validate-blob-token.js +26 -0
- package/dist/endpoints/paths/admin-validate-blob-token.js.map +1 -0
- package/dist/endpoints/paths/cron-list.d.ts +2 -0
- package/dist/endpoints/paths/cron-list.js +34 -0
- package/dist/endpoints/paths/cron-list.js.map +1 -0
- package/dist/endpoints/paths/cron-restore.d.ts +2 -0
- package/dist/endpoints/paths/cron-restore.js +71 -0
- package/dist/endpoints/paths/cron-restore.js.map +1 -0
- package/dist/endpoints/paths/cron-run.d.ts +3 -0
- package/dist/endpoints/paths/cron-run.js +46 -0
- package/dist/endpoints/paths/cron-run.js.map +1 -0
- package/dist/endpoints/shared.d.ts +12 -0
- package/dist/endpoints/shared.js +52 -0
- package/dist/endpoints/shared.js.map +1 -0
- package/dist/exports/client.d.ts +1 -0
- package/dist/exports/client.js +3 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/rsc.d.ts +1 -0
- package/dist/exports/rsc.js +3 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +3 -0
- package/dist/plugin.js +75 -0
- package/dist/plugin.js.map +1 -0
- package/dist/publicApiPaths.d.ts +18 -0
- package/dist/publicApiPaths.js +19 -0
- package/dist/publicApiPaths.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/backupSelection.d.ts +6 -0
- package/dist/utils/backupSelection.js +16 -0
- package/dist/utils/backupSelection.js.map +1 -0
- package/dist/utils/blobName.d.ts +32 -0
- package/dist/utils/blobName.js +99 -0
- package/dist/utils/blobName.js.map +1 -0
- package/dist/utils/dashboardRoleAccess.d.ts +23 -0
- package/dist/utils/dashboardRoleAccess.js +64 -0
- package/dist/utils/dashboardRoleAccess.js.map +1 -0
- package/dist/utils/dialogBackdrop.d.ts +9 -0
- package/dist/utils/dialogBackdrop.js +17 -0
- package/dist/utils/dialogBackdrop.js.map +1 -0
- package/dist/utils/formatBytes.d.ts +2 -0
- package/dist/utils/formatBytes.js +20 -0
- package/dist/utils/formatBytes.js.map +1 -0
- package/dist/utils/hostname.d.ts +4 -0
- package/dist/utils/hostname.js +32 -0
- package/dist/utils/hostname.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/maskBlobToken.d.ts +11 -0
- package/dist/utils/maskBlobToken.js +42 -0
- package/dist/utils/maskBlobToken.js.map +1 -0
- 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
|
-
#
|
|
1
|
+
# payload-plugin-backup-postgres
|
|
2
2
|
|
|
3
|
-
|
|
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, the backup dashboard is available at
|
|
192
|
+
`/admin/db-backups`. Day-to-day tasks — create, download, restore, delete,
|
|
193
|
+
schedule — are managed on this dedicated page.
|
|
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
|
+

|
|
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
|
+

|
|
226
|
+
|
|
227
|
+
### Scheduled backup settings
|
|
228
|
+
|
|
229
|
+
Retention, storage token, and cron collection skip list live in one modal:
|
|
230
|
+
|
|
231
|
+

|
|
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).
|