STATUS ÜBERPRÜFEN
I AM LISTENING TO
|

MongoDB Backup via SSH – Complete Guide

28. Oktober 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

Here’s the thing about the macOS menu bar: Apple gives you zero control over it. Your apps just pile in from the right, squeezing together like commuters on a rush-hour train, and you either live with it or you don’t. There’s no padding, no grouping, no breathing room. Just a wall of tiny icons staring […]

PDF operations are one of those recurring pain points that never fully go away. You need to fill a contract template, strip and rewrite document metadata before archiving, generate an invoice from an HTML template, and stamp every page with a branded header. The default answer is a SaaS API subscription that charges per document […]

Generating PDFs on a server is one of those tasks that sounds simple until you actually sit down to do it. HTML-to-PDF rendering drifts between browsers, LibreOffice headless mode is finicky to install, and most SaaS solutions charge per page once you hit volume. Gotenberg solves this cleanly: a single Docker container that bundles headless […]

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.