A MongoDB backup is only useful if you can restore it. On a VPS, the safest workflow is to create a dump with mongodump, store the backup outside the live database directory, copy it off the server, and run a restore test before you trust the process.
This guide is for small self-managed MongoDB deployments where command-line database tools are appropriate. Larger replica sets, sharded clusters, strict recovery point objectives, or compliance workloads may need snapshots, MongoDB Cloud Manager, Ops Manager, or a managed backup design. The practical goal here is simple: produce a backup file, protect it, and prove that it restores.
Key takeaways
- Use
mongodumpto create the backup andmongorestoreto test it. - Prefer a timestamped archive file so each run is easy to identify.
- Do not keep the only copy on the same VPS as the database.
- Avoid putting database passwords directly into shell history.
- Test a restore into a separate database or disposable environment before trusting automation.
- For the wider backup cluster, read the Virtarix guides to VPS backups, backup FAQs, systemctl basics, and VPS security.
Before you start
Confirm five things before running a production MongoDB backup:
- Which database or instance you need to back up.
- Which user has backup permissions.
- Where the temporary backup file will be written.
- Where the off-server copy will live.
- How you will test the restore.
Also check free disk space. A dump can be large, and compression is not a replacement for capacity planning. If the VPS is already low on disk space, copying a large database into the same filesystem can make the incident worse.
Step 1: Create a timestamped backup directory
Create a predictable place for the backup artifacts. The example uses a home-directory path so it does not depend on a distribution-specific backup layout.
mkdir -p ~/backups/mongodb
BACKUP_FILE=~/backups/mongodb/app-$(date +%F-%H%M).archive.gz
A timestamp prevents one backup from overwriting another. It also helps during incident review because you can match the file name to logs, deploys, and monitoring events.
Step 2: Run mongodump with archive and gzip
For a local MongoDB instance, use a connection string and a compressed archive:
mongodump --uri="mongodb://backup-user@localhost/app" --archive="$BACKUP_FILE" --gzip
Replace backup-user, localhost, and app with your real user, host, and database name. If your credentials require an authentication database, include it in the URI or add the appropriate authentication option from your MongoDB configuration.
The --archive option writes one archive file instead of a directory of BSON files. The --gzip option compresses the output. That combination is convenient for VPS backups because the result is a single file you can checksum, copy, rotate, and test.
Step 3: Verify the backup file exists
After the command finishes, check the file:
ls -lh "$BACKUP_FILE"
A file existing is not the same as a successful recovery, but it catches obvious failures such as a wrong path, permission problem, or empty variable. Record the size and timestamp in your maintenance notes.
You can also create a checksum:
sha256sum "$BACKUP_FILE" > "$BACKUP_FILE.sha256"
Keep the checksum with the backup so you can detect corruption after transfer.
Step 4: Copy the backup off the VPS
Do not leave the only MongoDB backup on the same server as the database. If the VPS disk fails, the account is compromised, or the filesystem is damaged, the local backup may disappear with the live data.
Copy the archive to another server, object storage bucket, backup service, or secure workstation. Use the storage location your organization already trusts. The exact transport can be scp, rsync, an object-storage CLI, or a backup agent, but the rule is the same: the recovery copy must survive the loss of the VPS.
Step 5: Restore-test the backup
A backup that has never been restored is a hope, not a recovery plan. Test with a separate database name or disposable environment.
mongorestore --uri="mongodb://restore-user@localhost/restore-test" --archive="$BACKUP_FILE" --gzip --nsFrom="app.*" --nsTo="restore-test.*"
The namespace mapping example restores documents into a different database name so you do not overwrite production data. Adjust it to match your database and test environment. Do not add destructive restore options unless you understand exactly what will be dropped or replaced.
After the restore, check collection counts, important indexes, application startup, and a few representative records. For production change control, record the restore command, result, test time, and cleanup steps.
Step 6: Automate only after the manual run works
Once the manual backup and restore test pass, automate the workflow. Automation should include:
- A dedicated backup user with the minimum required permissions.
- A timestamped filename.
- A destination outside the live database directory.
- Off-server transfer.
- Rotation or lifecycle rules.
- Logging for success and failure.
- Alerting when a backup is missing or too small.
- A scheduled restore test.
Do not automate a command you have not tested manually. Automation will faithfully repeat a bad path, missing credential, or broken restore process.
Credential safety
Avoid typing passwords directly into commands that will be saved in shell history. Prefer protected connection strings, environment handling, configuration files with tight permissions, or your existing secret-management workflow. If you must run an emergency command interactively, clean up history according to your company policy and rotate credentials if they may have been exposed.
Also restrict backup files. A database dump can contain customer data, API tokens, sessions, emails, billing records, or internal notes. Store it with the same care you apply to the live database.
Common MongoDB backup mistakes
Keeping only local backups
Local backups are convenient but fragile. Always move a copy away from the VPS.
Never testing restores
The most common backup failure is discovering too late that the file cannot restore, the credentials are missing, or the target version is incompatible.
Backing up during risky maintenance without notes
If a backup happens immediately before a migration, schema change, or import, note that context. Recovery decisions depend on knowing what state the backup represents.
Treating mongodump as the only production strategy
MongoDB documents mongodump and mongorestore as useful tools, especially for small deployments, but larger production systems may require a more robust backup architecture. Match the method to the recovery requirement.
MongoDB backup checklist for VPS admins
Before you call the job complete, confirm:
- The dump command completed successfully.
- The archive file has the expected timestamp and a plausible size.
- A checksum was created or another integrity check exists.
- A copy was stored off-server.
- A restore test ran in a safe target environment.
- The restore result was documented.
- Automation alerts when a backup fails or is missing.
This checklist is deliberately stricter than “run one command.” It protects you from the painful gap between having a backup file and having a working recovery process.
If you want a clean server for testing a MongoDB backup workflow, Virtarix Cloud VPS plans give you an isolated environment for practicing dumps, restore tests, off-server transfers, and automation before you touch production data.
FAQ
What command creates a MongoDB backup?
Use mongodump. For a single compressed archive, use mongodump --archive="file.archive.gz" --gzip with the correct connection details.
How do I restore a mongodump backup?
Use mongorestore with the archive and compression options that match the backup. Restore into a test database first when you are validating a backup.
Is a local VPS backup enough?
No. Keep an off-server copy. A local-only backup can be lost with the same disk, account, or server incident that damages the database.
Should I automate MongoDB backups immediately?
Automate after a manual backup and restore test works. Otherwise, automation may repeat an unproven or broken process.
Summary
A reliable MongoDB backup on a VPS has four parts: create the dump, protect the file, copy it off-server, and test the restore. mongodump creates the backup, but mongorestore proves whether the backup can become usable data again.
For small deployments, this command-line workflow is a practical starting point. For larger systems, stricter recovery objectives, or sharded production clusters, design a fuller backup strategy. In every case, the standard is the same: backups must be restorable, documented, and monitored.
Byline: Peter French — Updated 2026-05-18.