CHECKING STATUS
I AM LISTENING TO
|

MongoDB Backup via SSH – Complete Guide

28. October 2025
.SHARE

Table of Contents

So you need to backup your MongoDB database over SSH? You’re in the right place! We’ll go through a bunch of different ways to do this, from quick one-liners to fully automated scripts. Pick whatever works best for your situation.

Whether you’re managing a single server or dealing with a whole fleet of databases, having a solid backup strategy is absolutely crucial. I mean, we’ve all heard those horror stories about lost data, right? The good news is that MongoDB makes it pretty straightforward to create backups, and when you combine it with SSH, you can automate the whole thing and sleep better at night.

In this guide, we’ll cover everything from the simplest possible backup command to full-blown automated scripts with rotation policies. We’ll look at different approaches depending on your needs – maybe you just want to quickly grab a copy of your database, or maybe you need nightly automated backups with verification and cloud storage. Whatever your situation, we’ve got you covered.

The cool thing about doing backups via SSH is that you can manage everything remotely. No need to log into each server manually – you can trigger backups from your laptop, automate them with cron jobs, or even stream the data directly to your local machine without using any storage on the remote server. Pretty neat, right?

Before we dive in, if you want to go deeper into MongoDB’s backup concepts or need the official documentation, here are some great resources:

Alright, let’s get into it!

Method 1: Simple mongodump via SSH

The Basic Approach

This is the straightforward way – SSH into your server, dump the database, compress it, and copy it back to your local machine:

If You Need Authentication

Got username/password protection on your MongoDB? No problem, just add the auth params:

Method 2: Direct Backup to Local Machine

Stream it directly (my favorite!)

Why store the backup on the remote server at all? Just pipe it straight to your local machine. This is super clean and doesn’t eat up space on your server:

Just want one database?

Easy, just specify which one:

Method 3: Automated Backup Script

Set it and forget it

Alright, let’s get fancy. Here’s a proper backup script you can throw on your server. It’ll handle everything – compression, cleanup of old backups, the works.

Save this as /home/user/backup_mongodb.sh:

Make it executable:

Schedule it with cron

Want this to run every night at 2 AM? Add this to your crontab:

Method 4: Local Script to Pull Backups

Here’s a script for your local machine that’ll trigger the remote backup and then pull it down. Pretty handy if you want to control everything from your laptop.

Save this as pull_mongodb_backup.sh on your local machine:

Method 5: Using SSH Keys for Automation

Ditch the passwords

If you haven’t set up SSH keys yet, do it now. You’ll thank me later when you’re not typing passwords all the time:

Copy it to your server

Now it’s smooth sailing

Backups without password prompts? Yes please:

Method 6: Backup Specific Collections

Don’t need the whole database? Just grab what you need:

Method 7: Incremental Backup using Oplog

Got a replica set? You can do point-in-time backups with the oplog. This is pretty advanced stuff, but super useful:

Restore Examples

Okay, so you’ve got your backup. Now how do you actually use it? Here are the most common scenarios:

Basic restore

Just restore one database

Push it to a remote server

Best Practices

Look, I know best practices can sound boring, but trust me – follow these and you’ll avoid a lot of headaches:

1. Security

  • Use SSH keys instead of passwords
  • Store credentials in environment variables or secure vaults
  • Encrypt backups at rest
  • Use strong authentication for MongoDB

2. Storage

  • Keep backups on separate physical/virtual infrastructure
  • Implement 3-2-1 backup rule (3 copies, 2 different media, 1 offsite)
  • Monitor backup storage capacity

3. Testing

  • Regularly test restore procedures
  • Verify backup integrity
  • Document restore time objectives (RTO)

4. Monitoring

  • Log all backup operations
  • Set up alerts for failed backups
  • Track backup sizes and durations

5. Performance

  • Schedule backups during low-usage periods
  • Consider using secondary replica set members for backups
  • Use compression to reduce transfer time and storage

Verification Script

Always verify your backups! Here’s a quick script to check if your backup file is actually good:

Troubleshooting

Running into issues? Here are some common problems and how to fix them:

Connection Issues

Permission Issues

Getting “permission denied” errors? Check these:

Large Database Backups

Got a huge database? Use nice to keep your server from getting hammered during backup:

Example Complete Workflow

Here’s everything put together – backup, verify, and optionally upload to cloud storage. Copy this and customize it for your needs:

Let’s Talk!

Looking for a reliable partner to bring your project to the next level? Whether it’s development, design, security, or ongoing support—I’d love to chat and see how I can help.

Get in touch,
and let’s create something amazing together!

RELATED POSTS

mosparo’s rule packages are a powerful way to protect your forms from spam without relying on external services. As an open-source spam protection solution, mosparo puts you in control of your form security through customizable rule sets that analyze submitted content in real-time. One of mosparo’s standout features is its rule package system, which not […]

YOURLS is a free, open-source set of PHP scripts that lets you run your own URL shortening service. Released under the MIT license, it gives you complete control over your links, detailed analytics, and the freedom to customize everything. The latest version, YOURLS 1.10.2, was released in July 2025 and comes with improvements to the […]

Or: How I Learned to Stop Worrying and Love the Underscore Remember when you could just tell your computer what to do, in plain English, and it would actually do it? No? Well, grab your DeLorean, because we’re going back to the future with _hyperscript (yes, that underscore is part of the name, and yes, […]

Alexander

I am a full-stack developer. My expertise include:

  • Server, Network and Hosting Environments
  • Data Modeling / Import / Export
  • Business Logic
  • API Layer / Action layer / MVC
  • User Interfaces
  • User Experience
  • Understand what the customer and the business needs


I have a deep passion for programming, design, and server architecture—each of these fuels my creativity, and I wouldn’t feel complete without them.

With a broad range of interests, I’m always exploring new technologies and expanding my knowledge wherever needed. The tech world evolves rapidly, and I love staying ahead by embracing the latest innovations.

Beyond technology, I value peace and surround myself with like-minded individuals.

I firmly believe in the principle: Help others, and help will find its way back to you when you need it.