Back Up and Restore MongoDB via SSH Tunnel

Need to back up a MongoDB database on Sliplane? This guide shows you how to securely create and restore backups using an SSH tunnel. This method keeps your database private while giving you full access without exposing it publicly.
What You'll Learn
In this guide, you'll:
- Set up an SSH tunnel service in Sliplane
- Create backups using mongodump over the tunnel
- Restore databases using mongorestore
- Handle different backup formats and restore options
Note: If your database is already public and your IP is whitelisted, you can connect directly without an SSH tunnel.
Prerequisites
Before you start, make sure you have:
- MongoDB client tools installed:
mongodump
,mongorestore
,mongosh
- Your Sliplane MongoDB database credentials
- A Sliplane project with a running MongoDB service
Step 1: Create an SSH Tunnel Service
First, create a dedicated SSH tunnel service in your Sliplane project:
- In your Sliplane dashboard, go to your project
- Click Add Service → Presets → SSH Tunnel
- Use these settings:
- Image:
ghcr.io/sliplane/docker-ssh:latest
- Environment Variables:
HOST=0.0.0.0
PORT=2222
ROOT_PASSWORD=
choose a strong password
- Image:
- Deploy the service and note its public domain (e.g.,
ssh-tunnel-123.sliplane.app
)
Step 2: Establish the SSH Tunnel
Open a terminal and create a local tunnel to your Sliplane database:
ssh -p 2222 \
[email protected] \
-L 27017:mongodb-service-name.internal:27017 \
-N
Replace mongodb-service-name.internal
with your MongoDB service's internal hostname. Keep this terminal window open during the backup/restore process.
Step 3: Test the Connection
Verify that the tunnel works before creating backups:
mongosh "mongodb://your-user:[email protected]:27017/your-database?authSource=your-database" \
--eval 'db.runCommand({ping: 1})'
If you see { ok: 1 }
, your tunnel is working correctly.
Step 4: Create a Backup
Now that your SSH tunnel is working, it's time to create a backup. MongoDB offers multiple backup options through mongodump, each with different use cases and advantages.
Full Database Backup (Recommended)
This format creates a complete binary backup of your database:
mongodump \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--out backup-$(date +%F)
Specific Database Backup
Backup only a specific database:
mongodump \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--db your-database \
--out backup-$(date +%F)
Specific Collection Backup
Backup only specific collections:
mongodump \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--db your-database \
--collection your-collection \
--out backup-$(date +%F)
Archive Format
Create a single archive file:
mongodump \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--archive=backup-$(date +%F).archive \
--gzip
Step 5: Restore the Backup
With your backup file ready, you can now restore it to your MongoDB database. The restore process depends on your backup format and whether you're restoring to an existing database or creating a new one.
Restore Full Database
From directory backup:
mongorestore \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--drop backup-YYYY-MM-DD
Restore Archive Format
From archive file:
mongorestore \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--archive=backup-YYYY-MM-DD.archive \
--gzip
Restore to Different Database
mongorestore \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--db target-database \
--drop backup-YYYY-MM-DD/your-database
Restore Specific Collection
mongorestore \
--host 127.0.0.1 --port 27017 \
--username your-user --password your-password \
--authenticationDatabase your-database \
--db your-database \
--collection target-collection \
--drop backup-YYYY-MM-DD/your-database/your-collection.bson
This method gives you secure, reliable access to your MongoDB database on Sliplane for backups and restores while keeping your infrastructure secure.