How to Back Up MongoDB to Object Storage

How to BackUp MongoDB to Object Storage (head image)

If you’ve ended up here, I assume you already know the importance of regularly backing up MongoDB databases to prevent any loss of data. The main concern for many individuals is deciding where to keep these backups, especially when dealing with larger databases that need a lot of storage space. Object Storage is a highly recommended solution due to its scalability and cost-effectiveness. 

This article will demonstrate how to carry out backups of your MongoDB database and store them in Contabo S3-compatible Object Storage using the command line. The reason this is achievable is because Contabo Object Storage is compatible with the Amazon Web Services Command Line Interface (AWS CLI). 

You may notice that this guide is considerably shorter than other guides available online, but that is due to the ease of configuring Contabo Object Storage using AWS CLI. Additionally, the S3-compatibility allows for simple transfer of backup files from a Linux server to Object Storage. If you’re unfamiliar with Contabo Object Storage, you can find out more about it by clicking here

In order to perform a backup of your MongoDB Database, the following things are required: 


To carry out a backup of your MongoDB database, you’ll need the following:

  • An S3-compatible Object Storage
  • Your Access Key
  • Your Secret Key
  • Your S3 URL
  • AWS CLI already installed (and configured) on your server where MongoDB database is running

If you need assistance with installing and configuring AWS CLI on your server, you can refer to the documentation provided at the this link.

Creating a Backup of Your Database

To transfer your MongoDB database backup to your Object Storage, you must initially create a local backup. Fortunately, this can be achieved using a built-in command of MongoDB: 

mongodump --host <hostname> --port <port> --db <database_name> --out <output_directory>

Replace the placeholders with actual values:

  • <hostname>: The hostname or IP address of the MongoDB server. If it’s on the same server use “localhost”.
  • <port>: The port number on which MongoDB is running (default is 27017).
  • <database_name>: The name of the database you want to back up.
  • <output_directory>: The path to the directory where the backup files will be saved.

Uploading Your Database Backup

Use the subsequent command to transfer your locally-created database backup to Contabo Object Storage: 

aws --profile eu2 --region default --endpoint-url [your_s3_url] s3 cp [name_of_your_backup_file] s3://[bucket_name]

Scheduling Regular Database Backups with Cron

To set up regular backups for a specific database, you must complete two tasks:

1. Create a Shell Script

The initial step involves creating a shell script that generates a local backup of the specified database and subsequently transfers it to Contabo Object Storage using AWS CLI. 

To create this script, use the command below: 


And add the following content:

mongodump --host <hostname> --port <port> --db <database_name> --out <output_directory>

aws --profile eu2 --region default --endpoint-url [your_s3_url] s3 cp [name_of_your_backup_file] s3://[bucket_name]

Save the script with [CTRL]+O and exit the editor with [CTRL]+X  

Now make the script executable with this command: 

chmod +x

2. Edit the Crontab File

Now open the Crontab file where you can schedule the backups with this command:

crontab -e

And paste in the following content: 

0 3 * * * /[path_to_script]/

This script is programmed to execute a database backup on a daily basis at 3:00 AM. If you prefer a different schedule, you can modify the script accordingly. Additional information on the syntax of Cron can be found within the file itself.


In summary, backing up MongoDB databases to object storage with AWS CLI is a straightforward and effective method that adds an extra layer of security to valuable data. By utilizing the AWS CLI and the provided commands in this article, users can efficiently automate the backup process, schedule regular backups, and securely store the backups in object storage. 

Scroll to Top