Snapshots before migrations
A schema migration that goes wrong halfway is one of the most common ways to damage a
production database: a $set with the wrong filter, a dropped collection, a backfill
that writes garbage. The fix is cheap if you planned for it: take a snapshot right
before migrating, and roll back to it if the migration fails.
mongoshield snapshot v42-add-orders-index --include 'app.*'npx migrate-mongo up # or your migration tool# … it went wrong:mongoshield rollback v42-add-orders-indexWhat a snapshot is
Section titled “What a snapshot is”A snapshot is an ordinary MongoShield backup with the label
mongoshield.snapshot=<label>, taken with consistency auto (a single point in time on
replica sets and sharded clusters, see CONSISTENCY.md). It uses
your configured storage, compression and encryption, shows up in mongoshield list, and
is pruned by your retention policy like any other backup. Put a hold on it if you want to
keep it longer (mongoshield hold <id> --reason "before v42").
Labels are up to 128 letters, digits and . _ - : + @. Taking a snapshot with a label
that already exists is fine: rollback always uses the newest one.
What a rollback does
Section titled “What a rollback does”mongoshield rollback <label>:
- finds the newest complete snapshot with that label (or
--id <backup-id>); - shows the plan: each collection of the snapshot, its documents in the snapshot and in the live database, and what will happen to it;
- asks for confirmation (
--yesin scripts; without a terminal and without--yesit refuses with exit code 3); - takes a safety snapshot labelled
pre-rollback-<label>of the live collections it is about to replace or drop (skip it with--no-safety-snapshot), so the rollback itself can be undone withmongoshield rollback pre-rollback-<label>; - restores the snapshot over the live data in
dropmode: each collection is dropped and recreated with its documents, options and indexes exactly as snapshotted; - drops the collections (and views) the migration created: those in the
snapshot's databases that the snapshot does not hold. The plan lists them as
new … drop (created after the snapshot); - re-applies the GDPR erasure ledger, so nobody erased since the
snapshot comes back (
--skip-erasuresto opt out).
Which collections count as created by the migration: a snapshot records its
--include/--exclude patterns (labels mongoshield.snapshot.include and
.exclude), so a collection it deliberately left out (--exclude app.cache) is never
dropped, and a rollback's own --include/--exclude narrow the set further. A plain
backup rolled back to with --id (or a snapshot taken before the patterns were
recorded) counts as having captured its databases whole. Pass --keep-new to leave
the new collections alone. Use --include to roll back only some collections, and
--dry-run to see the plan without writing anything.
A rollback replaces data: writes your application made after the snapshot, to the collections being rolled back, are lost (they are in the safety snapshot). Stop writers, or put the application in maintenance mode, for the migration window.
From code
Section titled “From code”import { MongoShield, rollback, snapshot } from "mongoshield";
const shield = new MongoShield({ source: { uri: process.env.MONGODB_URI }, storage });
await snapshot(shield, { label: "v42", include: ["app.*"] });try { await migrate();} catch (error) { const result = await rollback(shield, { label: "v42" }); console.error(`Rolled back to ${result.snapshotId}; safety snapshot ${result.safetySnapshotId}`); throw error;}rollback(shield, options) takes label or backupId, include/exclude, mode
(default "drop"), dryRun, decryption (for encrypted snapshots), safetySnapshot
(default true), keepNew (default false: collections created since the snapshot are
dropped) and skipErasures. Its result lists those collections in newCollections
(dropsNew says whether they were dropped).
Recipes
Section titled “Recipes”migrate-mongo
Section titled “migrate-mongo”Wrap up in a script, so a failed migration rolls back automatically:
#!/usr/bin/env bashset -euo pipefaillabel="migrate-$(date -u +%Y%m%dT%H%M%SZ)"mongoshield snapshot "$label" --include 'app.*' --quietif ! npx migrate-mongo up; then echo "Migration failed; rolling back to $label" >&2 mongoshield rollback "$label" --yes exit 1fimigrate-mongo records applied migrations in its changelog collection. If that
collection is in the snapshot (it is with app.*), the rollback also restores the
changelog, so migrate-mongo status shows the failed migration as pending again.
Mongoose (and other in-app migration runners)
Section titled “Mongoose (and other in-app migration runners)”Migration runners that live in the application, such as ts-migrate-mongoose or a
hand-written migrations collection, run with the application's own connection. Take
the snapshot from the same process before the runner starts:
import mongoose from "mongoose";import { MongoShield, rollback, snapshot } from "mongoshield";
await mongoose.connect(process.env.MONGODB_URI!);const shield = new MongoShield({ // Reuse the driver client Mongoose already opened. source: { client: mongoose.connection.getClient() }, storage,});const label = `release-${process.env.GIT_SHA}`;await snapshot(shield, { label, include: ["app.*"] });try { await runMigrations(); // your Mongoose migration runner} catch (error) { await rollback(shield, { label }); throw error;}MongoShield never closes a client it did not create, so the Mongoose connection stays open.
CI / CD pipelines
Section titled “CI / CD pipelines”Snapshot as its own pipeline step, so a failed deploy can roll back from a later, manual step:
# GitHub Actions- name: Snapshot before migrating run: npx mongoshield snapshot "deploy-${{ github.run_id }}" --include 'app.*'- name: Migrate run: npx migrate-mongo up- name: Roll back on failure if: failure() run: npx mongoshield rollback "deploy-${{ github.run_id }}" --yesThe CI job needs the same configuration as your backups (storage, MONGODB_URI) and, for
encrypted snapshots, the decryption key in the rollback step only (--identity-file, or
decryption.identityFiles in the config): the snapshot step needs only the public
recipient.
MongoShield is MIT licensed and is not affiliated with, endorsed by or sponsored by MongoDB, Inc. MongoDB is a trademark of MongoDB, Inc.Privacy policy