Post Date: February 6, 2021
Last Updated: November 26, 2026
Introduction
MongoDB backup to AWS S3 is an effective way to protect important database information and prevent data loss. Regular MongoDB database backups help ensure data availability and business continuity in situations such as unexpected server failures, accidental data deletion, hardware issues, or application errors.
MongoDB provides a command-line utility called mongodump that allows administrators to create database backups. These backup files can be compressed and securely uploaded to Amazon S3 (Simple Storage Service) using the AWS Command Line Interface (AWS CLI).
In this step-by-step guide, we will explain how to back up MongoDB to AWS S3 on Ubuntu. You will learn how to use mongodump to export MongoDB databases, compress backup files using tar, and upload the generated backup archives to an Amazon S3 bucket using AWS CLI.
Additionally, we will create an automated MongoDB backup script using Bash and configure a Linux cron job to schedule daily database backups. This approach helps simplify MongoDB backup automation, securely store database backups in AWS S3, and improve your overall database backup and recovery process.
Prerequisites
Before starting the MongoDB backup configuration, make sure you have the following:
- An Ubuntu server with MongoDB installed and running.
- Access to the MongoDB database.
- MongoDB Database Tools installed (
mongodump). - AWS CLI installed on the Ubuntu server.
- An AWS account with an S3 bucket.
- Appropriate AWS permissions to upload files to the S3 bucket.
- SSH access with a user account that can execute the backup script.
Step 1: Check MongoDB Service Status
First, connect to your Ubuntu server through SSH.
Check whether the MongoDB service is running:
sudo systemctl status mongod
If MongoDB is not running, start the service:
sudo systemctl start mongod
Enable MongoDB to start automatically after a server reboot:
sudo systemctl enable mongod
Step 2: Verify MongoDB Database Tools
MongoDB provides the mongodump utility through MongoDB Database Tools. This utility is used to export MongoDB database contents.
Check whether mongodump is installed:
mongodump --version
If the command is available, it will display the installed version.
If it is not installed, install MongoDB Database Tools using the official MongoDB installation
Step 3: Install and Configure AWS CLI
AWS CLI is required to upload backup files from the Ubuntu server to Amazon S3.
Check whether AWS CLI is installed:
aws --version
If AWS CLI is not installed, install it using:
sudo apt update sudo apt install awscli -y
After installation, configure AWS credentials:
aws configure
Enter the following details when prompted:
AWS Access Key ID: YOUR_ACCESS_KEY AWS Secret Access Key: YOUR_SECRET_KEY Default region name: ap-south-1 Default output format: json
Use credentials belonging to an IAM user or role with the required S3 permissions. Avoid using root account credentials.
Verify AWS CLI access:
aws sts get-caller-identity
If the command returns your AWS account and identity information, AWS CLI authentication is working.

Step 4: Create an S3 Bucket for MongoDB Backups
Log in to your AWS Management Console.
- Open the Amazon S3 service.
- Click Create bucket.
- Enter a unique bucket name.
- Select your preferred AWS region.
- Keep Block Public Access enabled.
- Enable default server-side encryption.
- Click Create bucket.
For this example, we will use the following S3 bucket path:
s3://my_bucket/db_backups/
Replace my_bucket with your actual S3 bucket name.
You can verify access to the bucket using:
aws s3 ls s3://my_bucket/
If the bucket is accessible, AWS CLI will display its contents.
Step 5: Create a MongoDB Backup Script
Now we will create a Bash script to automate MongoDB database backups and upload them to AWS S3.
First, create a directory for backup scripts:
mkdir -p ~/bin
Create the backup script:
nano ~/bin/db-backups.sh
Add the following script:
#!/bin/bash
# MongoDB Backup Configuration
BACKUP_DIR="$HOME/mongodb-backups"
S3_BUCKET="s3://my_bucket/db_backups"
# Generate timestamp
DATE=$(date +%Y-%m-%d_%H-%M-%S)
# Backup directory and archive name
BACKUP_NAME="mongodb-backup-$DATE"
BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"
ARCHIVE="$BACKUP_DIR/$BACKUP_NAME.tar.gz"
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Create MongoDB database dump
mongodump --out "$BACKUP_PATH"
# Check backup status
if [ $? -ne 0 ]; then
echo "MongoDB backup failed!"
exit 1
fi
# Compress backup directory
tar -czf "$ARCHIVE" -C "$BACKUP_DIR" "$BACKUP_NAME"
# Check compression status
if [ $? -ne 0 ]; then
echo "Backup compression failed!"
exit 1
fi
# Upload backup to AWS S3
aws s3 cp "$ARCHIVE" "$S3_BUCKET/"
# Check upload status
if [ $? -ne 0 ]; then
echo "AWS S3 upload failed!"
exit 1
fi
# Remove local temporary backup files after successful upload
rm -rf "$BACKUP_PATH"
rm -f "$ARCHIVE"
echo "MongoDB backup completed successfully!"
Note: Replace my_bucket with your actual S3 bucket name.
The above script performs the following operations:
- Creates a timestamp-based backup directory.
- Uses
mongodumpto export MongoDB database contents. - Compresses the backup directory into a
.tar.gzarchive. - Uploads the compressed backup file to the configured AWS S3 bucket.
- Removes the temporary local backup files after a successful upload.
The script stops if the backup, compression, or upload operation fails, helping prevent unsuccessful backups from being treated as completed.
If your MongoDB server requires authentication or uses a remote connection, configure the appropriate MongoDB connection URI and pass it to mongodump using the --uri option.
Our DevOps and Cloud infrastructure experts can help you automate MongoDB database backups, configure secure AWS S3 storage, implement reliable backup strategies, and manage your cloud infrastructure efficiently.
Explore our DevOps & Cloud Services.
Step 6: Give Execute Permission to the Script
After saving the script, provide execute permission:
chmod +x ~/bin/db-backups.sh
Verify the file permissions:
ls -l ~/bin/db-backups.sh
You should see executable permissions for the script.
Step 7: Configure Cron Job for Automatic Backups
The next step is to configure a cron job to execute the MongoDB backup script automatically every day.
Cron is a Linux scheduling utility that allows administrators to execute scripts and commands at specific times.
First, open the crontab configuration:
crontab -e
Add the following entry:
0 0 * * * /bin/bash /home/ubuntu/bin/db-backups.sh >> /home/ubuntu/logs/db_backups.log 2>&1
Before adding the cron job, create the log directory:
mkdir -p /home/ubuntu/logs
Understanding the cron schedule:
0 0 * * *
The above schedule represents:
- Minute: 0
- Hour: 0 (12:00 AM)
- Day of the month: Every day
- Month: Every month
- Day of the week: Every day
This configuration runs the MongoDB backup script every day at midnight.
Important: Replace /home/ubuntu with the actual home directory of the Linux user running the cron job. Also ensure the user has access to MongoDB, AWS credentials, and the required commands.
Step 8: Test the MongoDB Backup Script
Before waiting for the scheduled cron execution, manually run the backup script to verify that everything is working correctly.
Execute:
/bin/bash /home/ubuntu/bin/db-backups.sh
If the process completes successfully, you should see:
MongoDB backup completed successfully!
Next, check the backup location in your AWS account.
Open the Amazon S3 bucket and navigate to:
my_bucket/db_backups/
You should see a backup archive with a timestamp-based filename, similar to:
mongodb-backup-2026-11-26_00-00-00.tar.gz
You can also verify the uploaded backup using AWS CLI:
aws s3 ls s3://my_bucket/db_backups/
Step 9: Verify Cron Job Execution and Logs
After configuring the cron job, verify that the scheduled task is registered:
crontab -l
Check the backup log file:
cat /home/ubuntu/logs/db_backups.log
To monitor the logs continuously:
tail -f /home/ubuntu/logs/db_backups.log
You can also check the cron service status:
sudo systemctl status cron
If the cron service is not running, start and enable it:
sudo systemctl start cron sudo systemctl enable cron
Step 10: Restore MongoDB Backup from AWS S3
Having a backup is useful only when the database can be restored successfully. It is recommended to periodically test your MongoDB backup restoration process.
First, download the backup archive from AWS S3:
aws s3 cp s3://my_bucket/db_backups/mongodb-backup-2026-11-26_00-00-00.tar.gz /tmp/
Extract the backup archive:
tar -xzf /tmp/mongodb-backup-2026-11-26_00-00-00.tar.gz -C /tmp/
Restore the MongoDB database using:
mongorestore --drop /tmp/mongodb-backup-2026-11-26_00-00-00/
The --drop option removes existing collections before restoring them. Use it carefully, preferably on a test database or after taking a separate backup of the existing data.
For authenticated MongoDB connections, use the appropriate --uri option with mongorestore.
After restoration, connect to MongoDB and verify that the databases and collections have been restored successfully.
Best Practices for MongoDB Backup Management
Consider the following recommendations to improve your backup strategy:
- Schedule regular backups: Configure daily or more frequent backups depending on your application requirements.
- Secure AWS credentials: Use IAM roles or restricted IAM credentials instead of storing unrestricted access keys.
- Enable S3 encryption: Protect database backup archives using server-side encryption.
- Configure backup retention: Use Amazon S3 Lifecycle policies to manage older backup files and storage costs.
- Monitor backup logs: Regularly review backup logs to identify failed executions.
- Test database restoration: Periodically restore backups in a test environment to verify data integrity.
- Monitor available disk space: Ensure sufficient local storage is available while generating and compressing backup files.
Conclusion
Automating MongoDB backups and storing them in Amazon S3 is an effective way to protect important database information and improve disaster recovery readiness.
By combining mongodump, tar, AWS CLI, and Linux cron jobs, you can create a scheduled backup process on your Ubuntu server without manually performing the backup every day.
Once the script and cron job are configured, MongoDB backup files will be generated, compressed, and uploaded automatically to your configured S3 bucket.
For better backup management, regularly monitor the backup logs, verify uploaded files, configure an appropriate retention policy, and test the database restoration process. These practices help ensure that your MongoDB backups remain available when you need to recover your data.
Frequently Asked Questions (FAQs)
1. How do I back up a MongoDB database on Ubuntu?
You can use the mongodump command to export MongoDB databases on Ubuntu. The generated backup files can be compressed using tar and stored locally or uploaded to an AWS S3 bucket using AWS CLI.
2. How can I automatically upload MongoDB backups to AWS S3?
You can use the AWS CLI aws s3 cp command to upload MongoDB backup archives to an S3 bucket. By configuring a Linux cron job, you can automatically execute the backup script at a scheduled time every day.
3. How often should I back up my MongoDB database?
Backup frequency depends on your application’s data requirements and recovery objectives. Daily backups are a good starting point for many applications. However, business-critical databases may require more frequent backups to minimize potential data loss.
Related Articles
How to Create an AWS S3 Bucket Using Terraform
How to Mount Wasabi Storage Bucket on CentOS and Ubuntu 20 using S3FS
MongoDB Backup and AWS Cloud Solutions with Expert DevOps Support
Protecting critical database information and maintaining reliable backup systems are essential for business continuity and data security. At Pheonix Solutions, our DevOps experts help businesses manage MongoDB databases, automate database backups, configure secure AWS S3 storage, and maintain reliable cloud infrastructure.
We can help you with:
- DevOps and Cloud Services for MongoDB database management, AWS infrastructure, cloud automation, server administration, and backup configuration.
- Website Development to build secure, responsive, and high-performing websites integrated with reliable database solutions.
- Software Maintenance and Support to troubleshoot application issues, maintain database environments, optimize server performance, and ensure smooth application operations.
Need help automating MongoDB backups, configuring AWS S3 storage, implementing database backup strategies, or managing your cloud infrastructure? Talk to our DevOps and Cloud experts to discuss your requirements.
