trieb-work

payload plugin backup mongodb

by trieb-work
Databases & data
v3
^3.0.0

Overview

Summary generated from the README

Backs up and restores MongoDB databases for Payload v3 projects, storing archives in Vercel Blob or AWS S3 and S3-compatible stores such as R2 and MinIO. It adds a Backups dashboard to the admin panel and custom REST endpoints for scheduled and manual backups. Requires the Payload MongoDB adapter, Node 20.9+, Next.js 15+ and React 19+.

Features

  • Scheduled cron backups with configurable retention and automatic pruning
  • Manual backups with per-collection selection and optional media bundled in a tar.gz archive
  • Restore with collection filters, merge upsert mode and a per-collection preview
  • Admin dashboard to list, search, download, restore and delete backups
  • Self-describing backup names, so no meta database is needed
  • Role-based access to the dashboard via environment variable or access function

Use cases

  • Schedule nightly MongoDB backups of a Payload site through Vercel Cron
  • Take a manual checkpoint before running a database migration
  • Clone production data into a staging environment with selective restore

From the author

Payload CMS plugin for MongoDB backup and recovery via Vercel Blob or AWS S3 (S3-compatible: R2, MinIO)

Installation

npm i @trieb.work/payload-plugin-backup-mongodb

8

Stars

0

Forks

1

Open Issues

2 months ago

Last Updated

84

Weekly Downloads

v0.2.0

npm Version

875.6 KB

Unpacked Size

4

Dependencies

GitHub stars

8+3 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20268
Dec 5, 20268
Dec 4, 20268
Dec 3, 20267
Dec 2, 20267
Dec 1, 20267
Nov 30, 20267
Nov 29, 20267
Nov 28, 20267
Nov 27, 20267
Nov 26, 20267
Nov 25, 20267
Nov 24, 20267
Nov 23, 20267
Nov 22, 20267
Nov 21, 20267
Nov 20, 20267
Nov 19, 20267
Nov 18, 20267
Nov 17, 20267
Nov 16, 20267
Nov 15, 20267
Nov 14, 20267
Nov 13, 20267
Nov 12, 20267
Nov 11, 20267
Nov 10, 20267
Nov 9, 20266
Nov 8, 20266
Nov 7, 20266
Nov 6, 20266
Nov 5, 20266
Nov 4, 20265
Nov 3, 20265
Nov 2, 20265
Nov 1, 20265
Oct 31, 20265
Oct 30, 20265
Oct 29, 20265
Oct 28, 20265
Oct 27, 20265
Oct 26, 20265
Oct 25, 20265
Oct 24, 20265
Oct 23, 20265
Oct 22, 20265
Oct 21, 20265
Oct 20, 20265
Oct 19, 20265
Oct 18, 20265
Oct 17, 20265
Oct 16, 20265
Oct 15, 20265
Oct 14, 20265
Oct 13, 20265
Oct 12, 20265
Oct 11, 20265
Oct 10, 20265
Oct 9, 20265
Oct 8, 20265
Oct 7, 20265
Oct 6, 20265
Oct 5, 20265
Oct 4, 20265
Oct 3, 20265
Oct 2, 20265
Oct 1, 20265
Sep 30, 20265
Sep 29, 20265
Sep 28, 20265
Sep 27, 20265
Sep 26, 20265
Sep 25, 20265
Sep 24, 20265
Sep 23, 20265
Sep 22, 20265
Sep 21, 20265
Sep 20, 20265
Sep 19, 20265
Sep 18, 20265
Sep 17, 20265
Sep 16, 20265
Sep 15, 20265
Sep 14, 20265
Sep 13, 20265
Sep 12, 20265
Sep 11, 20265
Sep 10, 20265
Sep 9, 20265
Sep 8, 20265
Sep 7, 20265
npm weekly downloads

105−204 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026105
Dec 5, 202682
Dec 4, 202690
Dec 3, 202693
Dec 2, 2026137
Dec 1, 2026162
Nov 30, 2026170
Nov 29, 2026190
Nov 28, 2026198
Nov 27, 2026279
Nov 26, 2026275
Nov 25, 2026237
Nov 24, 2026251
Nov 23, 2026267
Nov 22, 2026258
Nov 21, 2026253
Nov 20, 2026166
Nov 19, 2026213
Nov 18, 2026197
Nov 17, 2026182
Nov 16, 2026165
Nov 15, 2026157
Nov 14, 2026153
Nov 13, 2026148
Nov 12, 202696
Nov 11, 2026155
Nov 10, 2026166
Nov 9, 2026200
Nov 8, 2026175
Nov 7, 2026176
Nov 6, 2026171
Nov 5, 2026176
Nov 4, 2026134
Nov 3, 2026142
Nov 2, 2026122
Nov 1, 2026134
Oct 31, 2026164
Oct 30, 2026182
Oct 29, 2026172
Oct 28, 2026154
Oct 27, 2026118
Oct 26, 202693
Oct 25, 202686
Oct 24, 202658
Oct 23, 202634
Oct 22, 202672
Oct 21, 202681
Oct 20, 202676
Oct 19, 202676
Oct 18, 202672
Oct 17, 202670
Oct 16, 202679
Oct 15, 202655
Oct 14, 202662
Oct 13, 2026133
Oct 12, 2026166
Oct 11, 2026178
Oct 10, 2026175
Oct 9, 2026188
Oct 8, 2026179
Oct 7, 2026169
Oct 6, 202699
Oct 5, 2026104
Oct 4, 2026159
Oct 3, 2026185
Oct 2, 20262,305
Oct 1, 202612,475
Sep 30, 202618,800
Sep 29, 202626,373
Sep 28, 202634,246
Sep 27, 202641,837
Sep 26, 202645,495
Sep 25, 202643,426
Sep 24, 202633,433
Sep 23, 202627,104
Sep 22, 202619,557
Sep 21, 202611,669
Sep 20, 20264,028
Sep 19, 2026440
Sep 18, 2026511
Sep 17, 2026359
Sep 16, 2026442
Sep 15, 2026456
Sep 14, 2026534
Sep 13, 2026547
Sep 12, 2026479
Sep 11, 2026384
Sep 10, 2026452
Sep 9, 2026416
Sep 8, 2026410
Sep 7, 2026309

Each point is npm's rolling 7-day download count.

Health Score

54/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1219138434-root.svg)](https://payloaddirectory.dev/plugins/1219138434-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1219138434-root"><img src="https://payloaddirectory.dev/badge/1219138434-root.svg" alt="Payload Directory"></a>
Updated Sep 15, 2026Created Apr 23, 2026Published Sep 15, 2026

README

Experimental. Originated in the Payblocks template, now maintained as a standalone package. Running in production at 20+ projects from TRWK agency. Feedback and PRs welcome.

@trieb.work/payload-plugin-backup-mongodb

npm

A Payload CMS v3 plugin for MongoDB only — not Postgres, SQLite, or other database adapters. It handles Mongo + media backup, restore, and scheduled retention with zero meta-database and a built-in admin UI. Backups live in Vercel Blob Storage (default) or AWS S3 / S3-compatible stores (R2, MinIO, etc.), so a fresh install can list and restore any prior backup without bootstrapping a database first.

Backup dashboard embedded in the Payload admin panel

A first-class Backups section lives right below the Payload dashboard: browse every archive sorted by creation time, see at a glance which host and database it belongs to, whether media is bundled and how big it is, then download, restore or delete any backup with one click. Read more on how it originated at TRWK> Case Study.


Highlights

  • No meta database. Every backup is self-describing in its blob name (type---db---host---{collectionCount}-{timestampMs}.{ext}). A brand new project can list and restore any backup straight from blob storage.
  • Scheduled (cron) and on-demand backups. Wire /api/backup-mongodb/cron/run to Vercel Cron or any HTTP scheduler. Retention is configurable (N most recent cron backups are kept; older ones are pruned automatically).
  • Full database coverage. Dumps every MongoDB collection via the Payload mongoose adapter — including hidden system collections like users, payload-preferences, payload-migrations. Individual collections can be excluded per backup from the UI or via API.
  • Optional media bundling. Cron or manual backups can include Payload media blobs in a .tar.gz archive alongside the MongoDB dump.
  • Restore with filters. Restore any backup with an optional collection blacklist and mergeData upsert mode. Partial restores keep the running admin session / tasks collection intact.
  • Native admin dashboard. Adds a BackupDashboard widget to the Payload admin (afterDashboard): list / sort / filter / search backups, trigger manual backups or restores, configure retention and storage, and live-poll long-running tasks.
  • Payload REST, not Next.js route files. All endpoints are registered as Payload custom endpoints and served by the default /api/[...slug] handler — you do not add app/.../route.ts files for this plugin.
  • Pluggable blob storage. Uses BLOB_READ_WRITE_TOKEN by default (the same store you often use with @payloadcms/storage-vercel-blob), or point backups at a dedicated Vercel Blob store in the admin. Alternatively set BACKUP_STORAGE=s3 and use AWS S3, Cloudflare R2, MinIO, etc. Both public and private access stores are supported; a validation step detects which modes the store accepts.
  • Resumable long-running tasks. Manual backups and restores are tracked in a hidden backup-tasks collection (TTL, 30 min). The UI polls progress with a short-lived pollSecret so long jobs stay observable even across reloads.
  • Tested. Vitest covers archive, backup, restore, task progress, blob I/O, endpoint auth, cron parsing, and blob-name helpers.

Requirements

  • Node.js 20.9+ (Node 18 is no longer supported).
  • Payload v3+ (same major as your other @payloadcms/* packages).
  • MongoDB with Payload’s mongoose adapter (@payloadcms/db-mongodb).
  • MongoDB server (any version supported by that adapter).
  • A backup target:
    • Vercel Blob: BLOB_READ_WRITE_TOKEN (the default target). Vercel hosting is not required — any Node runtime that can reach Vercel Blob works.
    • S3: BACKUP_STORAGE=s3 plus BACKUP_S3_BUCKET (and optionally credentials). Requires Node 20+ for the optional AWS SDK peer packages. See Choosing a backup target.
  • Next.js 15+ and React 19+ (the usual Payload 3 + App Router stack).

Environment variables

VariableRequiredPurpose
MONGODB_URIyesMongoDB connection string (used by Payload and to label backups with the DB name).
BACKUP_STORAGEoptionalBackup target: vercel-blob (default) or s3. See Choosing a backup target.
BLOB_READ_WRITE_TOKENfor Vercel BlobDefault Vercel Blob store for backups and media. Required when BACKUP_STORAGE=vercel-blob (the default). Can be overridden in admin settings.
CRON_SECRETfor cronBearer token for every /api/backup-mongodb/cron/* call. Vercel Cron can supply this.
NEXT_PUBLIC_SERVER_URLoptionalUsed to label backups with the current host. Falls back to VERCEL_URL when set.
BACKUPS_TO_KEEPoptionalDefault retention for cron backups if the settings document has not been edited. Default 10.
PAYLOAD_BACKUP_ALLOWED_ROLESoptionalComma-separated role slugs that may see the Backups dashboard (case-insensitive). Use * to allow any authenticated user. When unset, the plugin falls back to requiring a role with slug admin, or allows everyone when the users collection has no roles field. Overridden by the access plugin option.

Choosing a backup target (Vercel Blob or S3)

Backups can be stored in Vercel Blob (default) or AWS S3. The S3 target also works with any S3-compatible store — Cloudflare R2, MinIO, etc. — because they share the same API; only the configuration differs, never the plugin code.

Select the target with BACKUP_STORAGE. When unset it stays vercel-blob, so existing deployments are unaffected.

Using S3

S3 is enabled when BACKUP_STORAGE=s3. Install the AWS SDK (declared as optional peer dependencies, so Vercel-only installs don't pull it in):

pnpm add @aws-sdk/client-s3 @aws-sdk/lib-storage @aws-sdk/s3-request-presigner
VariableRequiredPurpose
BACKUP_STORAGEyesSet to s3.
BACKUP_S3_BUCKETyesTarget bucket name.
BACKUP_S3_REGIONoptionalRegion. Falls back to AWS_REGION, then us-east-1.
BACKUP_S3_ENDPOINToptionalCustom endpoint for S3-compatible stores (R2, MinIO). Omit for AWS S3.
BACKUP_S3_FORCE_PATH_STYLEoptionalPath-style addressing. Defaults to true when an endpoint is set (needed for MinIO and some R2 setups).
BACKUP_S3_PREFIXoptionalKey namespace prepended to every object (for sharing a bucket). Backups still list/restore by their backups/… name.
BACKUP_S3_ACCESS_KEY_IDoptionalAccess key. Falls back to AWS_ACCESS_KEY_ID. Omit to use the AWS default credential chain (IAM roles, etc.).
BACKUP_S3_SECRET_ACCESS_KEYoptionalSecret key. Falls back to AWS_SECRET_ACCESS_KEY.
BACKUP_S3_SESSION_TOKENoptionalSession token for temporary credentials. Falls back to AWS_SESSION_TOKEN.

Credentials are optional: when omitted, the AWS SDK's default provider chain is used (environment, shared config, or instance/role credentials — recommended on AWS).

AWS S3

BACKUP_STORAGE=s3
BACKUP_S3_BUCKET=my-payload-backups
BACKUP_S3_REGION=eu-central-1
# Credentials via IAM role (recommended) or:
# BACKUP_S3_ACCESS_KEY_ID=...
# BACKUP_S3_SECRET_ACCESS_KEY=...

Cloudflare R2

BACKUP_STORAGE=s3
BACKUP_S3_BUCKET=my-payload-backups
BACKUP_S3_REGION=auto
BACKUP_S3_ENDPOINT=https://<account-id>.r2.cloudflarestorage.com
BACKUP_S3_FORCE_PATH_STYLE=true
BACKUP_S3_ACCESS_KEY_ID=<r2-access-key-id>
BACKUP_S3_SECRET_ACCESS_KEY=<r2-secret-access-key>

Backups are stored under the same backups/… keys (see Blob naming), so listing and restore work identically across targets. Downloads stream through the plugin's authenticated endpoint, so the bucket does not need to be public.

Note on media: the "include media" option bundles files from your Payload Vercel Blob upload store (BLOB_READ_WRITE_TOKEN). The configurable target above governs where backup archives live only; media is always read from and restored to the upload store, even when BACKUP_STORAGE=s3.


Installation and getting started

Use this flow when adding the plugin to an existing Payload 3 + Next.js app that already has MongoDB and a blob/S3 store configured.

1. Add the package

pnpm add @trieb.work/payload-plugin-backup-mongodb
# npm install @trieb.work/payload-plugin-backup-mongodb
# yarn add @trieb.work/payload-plugin-backup-mongodb

Peer dependencies: @payloadcms/db-mongodb, payload, @payloadcms/ui, next, and react. (all expected in a Payload 3 + MongoDB + Next.js app).

2. Register the plugin

// payload.config.ts
import { buildConfig } from 'payload'
import { backupMongodbPlugin } from '@trieb.work/payload-plugin-backup-mongodb'

export default buildConfig({
  // your db adapter, collections, etc.
  plugins: [
    backupMongodbPlugin({
      // all options optional — see "Plugin options"
    }),
  ],
})

The plugin will register the backup collections, mount the dashboard, and add /api/backup-mongodb/* endpoints. Import paths resolve to a published or linked dist/ build; after local changes to the package, run pnpm build in the plugin repo (or reinstall) so dist/ is up to date.

3. Regenerate types and the import map

So Payload picks up the new admin components and collections:

pnpm payload generate:types
pnpm payload generate:importmap

4. (Optional) Schedule cron backups

Vercel: add a cron in vercel.json. Path must match your API; default is under /api/…:

{
  "crons": [
    {
      "path": "/api/backup-mongodb/cron/run",
      "schedule": "0 3 * * *"
    }
  ]
}

Vercel Cron sends header Authorization: Bearer $CRON_SECRET. Other hosts (k8s, GitHub Actions, etc.): GET the same URL on your schedule with that same authorization header.

Checklist

  • Package installed
  • backupMongodbPlugin in plugins array
  • generate:types and generate:importmap run
  • MONGODB_URI and BLOB_READ_WRITE_TOKEN set in the environment
  • (Optional) CRON_SECRET and cron job (vercel.json) if you want scheduled backups

Start your dev server, open /admin, sign in as an admin, and you should see Backups below the dashboard.


Admin UI

After the first login as an admin user, a Backups section appears below the default Payload dashboard. Day-to-day tasks — create, download, restore, delete, schedule — all happen in the UI below.

Who can see it? The visibility rules, in precedence order:

  1. If the plugin is registered with an access function, it wins (see Plugin options).
  2. Otherwise, if PAYLOAD_BACKUP_ALLOWED_ROLES is set, it is used as a comma-separated allow-list of role slugs (case-insensitive). Use * for "any authenticated user". Example: PAYLOAD_BACKUP_ALLOWED_ROLES=admin,superadmin.
  3. Otherwise, the historical default applies: users with a role whose slug is admin see the dashboard; projects that don't use a roles field at all get the dashboard for any authenticated user.

Scheduled (cron) vs on-demand (manual) backups

  • Scheduled / cron backups follow shared settings: retention, media toggle, and collections to skip. Older cron archives are pruned when the keep-count is exceeded. They appear in the list as CRON BACKUP (or similar).
  • Manual / on-demand backups are configured per run — per-collection toggles, optional media in a .tar.gz archive. Manual backups are not auto-pruned; use them as checkpoints before migrations.

Manual backup dialog with per-collection selection and optional media bundling

Selective restore with per-collection preview

Restore opens a preview of the archive before any write. You can restore everything, cherry-pick collections, or skip media from .tar.gz when you only need database rows. Restoring from a different host or database is supported; filters help when cloning production into staging.

Restore dialog previewing document counts per collection

Scheduled backup settings

Retention, storage token, and cron collection skip list live in one modal:

Scheduled backup settings: cron schedule, retention, blob token, collection skip list

  • Schedule: when vercel.json is present, the plugin can show a human-readable cron description (via cronstrue).
  • Retention: how many cron archives to keep; manual backups are not pruned by this.
  • Dedicated backup storage (optional): a separate Vercel Blob read/write token (when BACKUP_STORAGE=vercel-blob) so backups can live in a different store than media. The UI can validate the token and optionally copy existing backups/* objects to the new store. For S3, configure the target bucket via environment variables.
  • Collection selection for cron: defaults to all collections, with per-collection opt-out.

Plugin options

type BackupPluginOptions = {
  /**
   * Set to `false` to disable the plugin (no collections, endpoints, or admin UI).
   * Omit or leave unset to keep the plugin active.
   */
  enabled?: boolean

  /** Default cron retention when no value is stored in settings. Falls back to `BACKUPS_TO_KEEP` or `10`. */
  backupsToKeep?: number

  /**
   * Custom access check for admin routes and the dashboard. Overrides the
   * `PAYLOAD_BACKUP_ALLOWED_ROLES` env var when provided.
   *
   * Default (env-based): see the `PAYLOAD_BACKUP_ALLOWED_ROLES` entry in the
   * environment variables table. When neither is configured, the check falls back
   * to a role with slug `admin`, or allows any authenticated user in projects
   * without a `roles` field.
   */
  access?: (user: Record<string, unknown> | null) => boolean
}

HTTP API (Payload REST)

All routes are served by Payload’s /api/[...slug] handler under /api/backup-mongodb/…. The backup-mongodb prefix avoids clashing with a collection slug. The admin UI uses the same URLs; use the exported backupPluginPublicApiPaths helper in client code.

Cron / external (Bearer CRON_SECRET)

GET  /api/backup-mongodb/cron/run        # enqueue a cron backup
GET  /api/backup-mongodb/cron/list     # list backups
POST /api/backup-mongodb/cron/restore  # body: { "url": "https://…" }
Authorization: Bearer <CRON_SECRET>
MethodPath
POST/api/backup-mongodb/admin/manual
POST/api/backup-mongodb/admin/restore
POST/api/backup-mongodb/admin/backup-preview
POST/api/backup-mongodb/admin/restore-preview
POST/api/backup-mongodb/admin/delete
GET/api/backup-mongodb/admin/backup-download
GET/api/backup-mongodb/admin/task/:id
GET / POST/api/backup-mongodb/admin/settings
POST/api/backup-mongodb/admin/validate-blob-token

Programmatic API

For scripts, hooks, and tests:

import {
  createBackup,
  listBackups,
  restoreBackup,
} from '@trieb.work/payload-plugin-backup-mongodb'

// Example: manual backup from a one-off script
await createBackup(payload, { cron: false, includeMedia: true })

// Example: restore from a URL, skipping `users`
await restoreBackup(payload, downloadUrl, ['users'], false)

// Example: list backup archives (token from backup-settings + env, same as create/restore)
const blobs = await listBackups(payload)

Blob storage model

Blob naming


backups/{type}---{dbName}---{hostname}---{collectionCount}-{timestampMs}.{json|tar.gz}

  • type: cron or manual (and legacy-style labels where applicable).
  • dbName / hostname: derived from your MongoDB URL and public server URL; URL-encoded in the name.
  • collectionCount + timestampMs: sort key for listing; legacy blob names are still supported.

Public vs private stores

A validation step probes the bucket and records whether the store behaves as public or private. New uploads use the right access mode. Restore and preview flows handle both.

Overriding the backup store

To keep backups in a different Vercel Blob project than media (when BACKUP_STORAGE=vercel-blob), open Backup settings in the admin, paste a dedicated BLOB_READ_WRITE_TOKEN, validate, and optionally migrate existing backups/* objects to the new store before switching. For S3, the target is controlled entirely by environment variables.

Removed demo seed API (intentional)

Earlier releases exposed an optional demo seed route (seedDemoDumpUrl, /backup-mongodb/admin/seed, and the restoreSeedMedia export). That was removed on purpose: a backup plugin should not ship a public “seed my database” API. Use your own migration/seed tooling instead.


Running the tests

pnpm test:int

E2E (Playwright)

End-to-end tests live in tests/e2e/ and target the dev/ Next + Payload app. Locally, start Mongo (or rely on the in-memory replica set when DATABASE_URL / MONGODB_URI are unset), then either run pnpm dev and pnpm test:e2e in another shell (Playwright reuses the server when not in CI), or run only pnpm test:e2e so Playwright starts pnpm dev for you.

CI (.github/workflows/e2e.yml, same pattern as the reference e2e.yml in executive-search: MongoDB 7 service, pnpm run build then pnpm run build:dev, then Playwright against next start). Optional secret BLOB_READ_WRITE_TOKEN for fuller blob behaviour.

Optional: deploy dev/ to Vercel

.github/workflows/deploy-dev-vercel.yml runs when dev/ (or related paths) change on main, or via workflow_dispatch, only if VERCEL_TOKEN, VERCEL_ORG_ID, and VERCEL_PROJECT_ID are set. Configure the Vercel project Root Directory to dev (dev/vercel.json wires install/build from the repo root).

The repo includes a dev/ Payload + Next app (MongoDB Memory Server when no URI is set): pnpm dev → http://localhost:3000/admin (login in dev/helpers/credentials.ts). In unit tests, external services like @vercel/blob and bson are often mocked with vi.mock().


Publishing (npm)

Versioning uses Changesets: add a file with pnpm changeset, open a PR to main, and merge. The Release workflow opens a “version packages” PR or runs pnpm run release (build + changeset publish) when the set of changesets is ready. PRs need a new .changeset/*.md unless you add the no-changeset label (e.g. docs-only).


Roadmap / ideas

  • Multipart / very large backup uploads (Vercel Blob supports large objects; may need to switch past single-part limits). (S3 uploads already use multipart.)
  • Additional storage adapters (filesystem, etc.). (AWS S3 and S3-compatible stores such as R2/MinIO are supported — see Choosing a backup target.)
  • Scheduler-agnostic display when not using Vercel (vercel.json is currently used for the schedule summary where available).
  • Streaming restore for very large databases.
  • Configurable backups/ prefix or bucket layout.
  • add support for tenant plugin and partial backups per tenant

License

MIT


Built and maintained by TRWK>, formerly trieb.work.

Similar plugins