Skip to content
Dhruvgiri Goswami
All writing

MongoDB backups you have actually restored

Dhruvgiri Goswami3 min readMongoDBDevOps

A backup you have never restored is a hope, not a backup. I have seen backup scripts that ran every night for a year and produced empty files, because a password changed and nobody checked. This is the plan I use for MongoDB on a single server, small enough to understand in one read.

The rule of three

The classic 3-2-1 rule is still the right target:

  • 3 copies of the data (the live database plus two backups)
  • 2 different kinds of storage (the server’s disk and object storage)
  • 1 copy somewhere else entirely (a different provider or region)

A backup on the same disk as the database protects you from mistakes like a bad migration. It does nothing if the server itself is lost, so you always need the off-site copy too.

The backup script

mongodump with --archive and --gzip writes the whole database to one compressed file. If Mongo runs in Docker Compose, run the dump inside the container and stream the file out:

#!/usr/bin/env bash
# /srv/acme/backup.sh
set -euo pipefail

cd /srv/acme
STAMP=$(date +%F-%H%M)
FILE="backups/acme-$STAMP.archive.gz"

# Dump inside the container, write the file on the host.
docker compose exec -T mongo sh -c \
  'mongodump --username "$MONGO_INITDB_ROOT_USERNAME" --password "$MONGO_INITDB_ROOT_PASSWORD" \
   --authenticationDatabase admin --db acme --archive --gzip' > "$FILE"

# Fail loudly if the dump is suspiciously small.
if [ "$(stat -c%s "$FILE")" -lt 10000 ]; then
  echo "Backup $FILE is too small" >&2
  exit 1
fi

# Copy it off the server (see below), then keep 14 days locally.
rclone copy "$FILE" offsite:acme-backups/
find backups -name '*.archive.gz' -mtime +14 -delete

Make it executable with chmod +x backup.sh. The -T flag stops Docker from allocating a terminal, which would otherwise corrupt the binary output. set -euo pipefail makes the script stop at the first error instead of carrying on and deleting your old backups after a failed dump.

Getting a copy off the server

rclone copies files to almost any storage: S3, Cloudflare R2, Backblaze B2, Google Drive and many more. Run rclone config once to set up a remote (called offsite above) and the script takes care of the rest.

Two settings worth getting right on the storage side:

  • Use credentials that can upload but not delete, if your provider supports it. If the server is ever compromised, the attacker can’t wipe your backups too.
  • Set a lifecycle rule on the bucket to delete files older than, say, 90 days, so storage costs don’t creep up.

Run it every night

crontab -e
# Every night at 2:15, keep a log of the last run
15 2 * * * /srv/acme/backup.sh > /srv/acme/backups/last-run.log 2>&1

Pick a time when nobody uses the system. Then make sure you hear about failures. Cron failing silently is how backups stop without anyone noticing. A simple approach is a “dead man’s switch” monitor (Healthchecks.io and most uptime tools offer one): add a curl to its ping URL as the last line of the script, and it alerts you when the ping doesn’t arrive.

The step everyone skips: restore it

Once a month, restore last night’s backup into a separate database and check it. This takes five minutes and is the only way to know the backups work.

# Restore the dump into a throwaway database called acme_restore_test.
docker compose exec -T mongo sh -c \
  'mongorestore --username "$MONGO_INITDB_ROOT_USERNAME" --password "$MONGO_INITDB_ROOT_PASSWORD" \
   --authenticationDatabase admin --archive --gzip \
   --nsFrom="acme.*" --nsTo="acme_restore_test.*" --drop' < backups/acme-2026-08-20-0215.archive.gz

--nsFrom and --nsTo rename the database while restoring, so the live data is never touched. Then compare a few counts:

// In mongosh
const live = db.getSiblingDB('acme'), test = db.getSiblingDB('acme_restore_test');
['orders', 'customers', 'invoices'].forEach((c) =>
  print(c, live[c].countDocuments(), test[c].countDocuments()),
);
test.dropDatabase();

The counts should be close (the live one will have a few new records since last night). Write down how long the restore took. That number is how long your business would be down in a real recovery, and it is worth knowing before the day you need it.

When this isn’t enough

Nightly dumps mean you can lose up to a day of data. For most small business systems that is an acceptable trade-off, but ask the question explicitly. If losing a day of orders is not acceptable, the next steps are:

  • More frequent dumps (every few hours) for the busiest collections
  • A replica set, so a second server holds a live copy at all times
  • A managed database such as MongoDB Atlas, which offers continuous backups with point-in-time restore

And remember that the database is not everything: uploaded files, the .env file and the server configuration need copies too. Most of mine live in a private git repository, and uploads go to object storage from the start.

Summary

  • Dump nightly with mongodump --archive --gzip
  • Check the file isn’t empty, copy it off the server, delete old local copies
  • Get alerted when a backup doesn’t run
  • Restore into a test database every month and compare counts
  • Know how long a full restore takes