Skip to content

MongoDB Docker Backup to a Workstation

First PublishedByAtif Alam

This runbook covers backing up MongoDB that runs inside Docker on a remote Linux VPS and moving the data to a Mac workstation — either as a dated archive file or directly into a local MongoDB instance. It uses mongodump over SSH (stream or scp). For SSH keys and host aliases, see OpenSSH Client Keys. For backup theory and restore drills, see RDBMS reliability and on-call.

Use placeholders below — never paste production hostnames, container names, or passwords into tickets or docs.

  • Remote: MongoDB in a Docker container on a Linux VPS you reach over SSH.
  • Local: Your Mac — save a .gz backup file or restore into a local mongod.
  • Method: docker exec … mongodump on the VPS, streamed or copied to the Mac.
GoalOptionBest when
Restore now into local MongoDBA — SSH pipeLocal mongod is running; you do not need a saved file
Archive a backup on disk (recommended)B — .gz fileYou want a dated file for drills or later restore
Pipe fails or you want files on the VPS firstC — dump + scpDebugging remote dump, very large data, or unstable SSH stream

On the Mac:

Terminal window
brew install mongodb-database-tools mongosh
# Optional: local MongoDB server (required for Option A)
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community # before Option A only

Keep Database Tools close to the remote MongoDB major version (for example both on 7.x).

On the VPS: Docker running; you can docker exec into the MongoDB container.

SSH: Access to the VPS as a non-root user (or your team’s standard deploy user). See OpenSSH Client Keys for keys and ~/.ssh/config host aliases.

Disk space: Check before large dumps:

Terminal window
df -h ~ # Mac destination (Options B/C)
ssh [email protected] 'df -h /tmp' # VPS staging (Option C)

Set these once per session (adjust to your environment):

Terminal window
REMOTE_HOST=vps.example.com
SSH_USER=deploy
CONTAINER_NAME=myapp-mongo

Use a Host alias from ~/.ssh/config instead of SSH_USER@REMOTE_HOST if you prefer (for example ssh my-vps).

On the VPS (after ssh ${SSH_USER}@${REMOTE_HOST}), inspect the container environment:

Terminal window
docker inspect "${CONTAINER_NAME}" | grep -A 20 '"Env"'

Common variables on the official MongoDB image:

  • MONGO_INITDB_ROOT_USERNAME
  • MONGO_INITDB_ROOT_PASSWORD

Or read your project compose file:

Terminal window
cat ~/app/docker-compose.yml # path on the VPS

Do not rely on docker inspect for routine production credential lookup — use your secrets store or compose secrets. Never commit passwords to git.

No-auth dev containers: omit --username, --password, and --authenticationDatabase.

Single database only: add --db=mydb to mongodump to shrink the backup.

Pipes the archive from the container to local mongorestoreno file is saved.

Requires local mongod running. This does not create a backup file on disk.

Terminal window
ssh "${SSH_USER}@${REMOTE_HOST}" \
"docker exec ${CONTAINER_NAME} mongodump \
--username=YOUR_USER \
--password=YOUR_PASSWORD \
--authenticationDatabase=admin \
--archive --gzip" \
| mongorestore --archive --gzip

Custom SSH key or port (same pattern for Options B and C):

Terminal window
ssh -i ~/.ssh/id_ed25519_deploy -p 22 "${SSH_USER}@${REMOTE_HOST}" ...

Restoring may merge with or duplicate databases already on your Mac. Use —drop on mongorestore only when you intend to replace existing local databases.

Section titled “Option B — Save a .gz Archive on Mac (Recommended)”
Terminal window
ssh "${SSH_USER}@${REMOTE_HOST}" \
"docker exec ${CONTAINER_NAME} mongodump \
--username=YOUR_USER \
--password=YOUR_PASSWORD \
--authenticationDatabase=admin \
--archive --gzip" \
> ~/mongo-backup-$(date +%Y%m%d).gz

Creates a file like ~/mongo-backup-20260616.gz.

Terminal window
mongorestore --archive=~/mongo-backup-20260616.gz --gzip

Use when piping fails or you want to inspect the dump on the server first.

Terminal window
ssh "${SSH_USER}@${REMOTE_HOST}"
docker exec "${CONTAINER_NAME}" mongodump \
--username=YOUR_USER \
--password=YOUR_PASSWORD \
--authenticationDatabase=admin \
--out=/dump
Terminal window
docker cp "${CONTAINER_NAME}:/dump" /tmp/mongo-backup
Terminal window
scp -r "${SSH_USER}@${REMOTE_HOST}:/tmp/mongo-backup" ~/mongo-backup

With a custom key or port:

Terminal window
scp -r -i ~/.ssh/id_ed25519_deploy -P 22 \
"${SSH_USER}@${REMOTE_HOST}:/tmp/mongo-backup" ~/mongo-backup

4. Restore from the directory (on the Mac)

Section titled “4. Restore from the directory (on the Mac)”

Option C produces a folder dump, not a gzip archive:

Terminal window
mongorestore ~/mongo-backup
Terminal window
rm -rf /tmp/mongo-backup
docker exec "${CONTAINER_NAME}" rm -rf /dump
Terminal window
mongosh

Inside the shell:

show dbs
use mydb
db.stats()

You should see your restored database names listed.

ErrorCauseFix
Command listDatabases requires authenticationMissing credentials on mongodumpAdd --username, --password, --authenticationDatabase=admin
I/O failure reading beginning of archive: EOFmongodump failed on the VPS; empty pipeRun the remote command alone; fix errors before piping
0 documents restoredEmpty or failed dumpCheck VPS-side mongodump output
Wire version / tool version errorsDatabase Tools vs server mismatchAlign mongodb-database-tools major version with remote MongoDB
GoalCommand
Backup to local MongoDBssh … docker exec … mongodump --archive --gzip | mongorestore --archive --gzip
Backup to .gz on MacSame remote dump; redirect with > ~/mongo-backup-$(date +%Y%m%d).gz
Restore from .gzmongorestore --archive=~/mongo-backup-YYYYMMDD.gz --gzip
Restore from directory (Option C)mongorestore ~/mongo-backup
Verifymongoshshow dbs
Find credentials (dev)docker inspect CONTAINER | grep -A 20 '"Env"'

mongodump is a standard logical backup for ops and migration. It is not a substitute for managed PITR, replica-set oplog backups, or vendor snapshot policies — see Backup, restore, and restore drills.