How to Create External Backup of Your Self-Hosted MongoDB Database

Creating external backups of your MongoDB database is essential for data safety. This tutorial demonstrates the process of backing up a self-hosted MongoDB database. The same backup commands will work with any MongoDB instance (including MongoDB Atlas) once you have the connection URL. The only difference is in how you get the connection URL, for MongoDB Atlas you can get it from their dashboard, while for self-hosted instances we need to set up remote access first.

Prerequisites

  • MongoDB Database Tools installed on your local machine
  • SSH access to your MongoDB server (for self-hosted instance to configure remote access)

Installing MongoDB Database Tools

First, check if you already have the tools installed:

sudo dpkg -l mongodb-database-tools

If not installed, follow these steps:

  1. Download the package from MongoDB Download Center or directly using wget ( for Ubuntu 22.04 ):
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2204-x86_64-100.10.0.deb
  1. Install the package:
sudo apt install ./mongodb-database-tools-*-100.10.0.deb

Note: These steps are based on Ubuntu 22.04. For other systems or versions, visit the official MongoDB documentation.

Setting Up Remote Access

To enable external backups, we need to configure MongoDB to accept remote connections:

1. Edit MongoDB configuration:

# Edit your MongoDB configuration file
sudo nano /etc/mongod.conf

# Add/modify these lines
net:
  bindIp: 127.0.0.1,your_server_ip  # Add your server's IP
  port: 27017

security:
  authorization: enabled

2. Create a backup user:

# Connect to MongoDB shell
mongosh

# Create the backup user
use admin
db.createUser({
  user: "backup_user",
  pwd: "your_secure_password",
  roles: [
    { role: "backup", db: "admin" },
    { role: "readAnyDatabase", db: "admin" }
  ]
})

3. Configure firewall access (to allowed remove):

sudo ufw allow from your_local_or_remote_ip to any port 27017

Creating External Backups

Now you can create backups from your local or remote machine. Here are different ways to do it:

1. Basic backup to directory:

# Backup all databases
mongodump --uri="mongodb://backup_user:your_secure_password@your_server_ip:27017" \
  --out ./mongodb_backup_$(date +%Y%m%d_%H%M%S)

# Backup specific database
mongodump --uri="mongodb://backup_user:your_secure_password@your_server_ip:27017" \
  --db your_database \
  --out ./mongodb_backup_$(date +%Y%m%d_%H%M%S)

2. Archive backup (single file):

# Archive all databases
mongodump --uri="mongodb://backup_user:your_secure_password@your_server_ip:27017" \
  --archive=./backup_$(date +%Y%m%d_%H%M%S)

# Archive specific database
mongodump --uri="mongodb://backup_user:your_secure_password@your_server_ip:27017" \
  --db your_database \
  --archive=./backup_$(date +%Y%m%d_%H%M%S)

3. Compressed archive (recommended for remote backups):

# Compressed archive of all databases
mongodump --uri="mongodb://backup_user:your_secure_password@your_server_ip:27017" \
  --archive=./backup_$(date +%Y%m%d_%H%M%S).gz --gzip

# Compressed archive of specific database
mongodump --uri="mongodb://backup_user:your_secure_password@your_server_ip:27017" \
  --db your_database \
  --archive=./backup_$(date +%Y%m%d_%H%M%S).gz --gzip

Note: If your password contains special characters, make sure to URL encode them. For example, replace @ with %40, # with %23, etc. So if your password is my@password, the connection URI should use my%40password instead.

That’s it! Make sure to store your backups in a safe place.