{"id":6110,"date":"2021-02-06T18:30:09","date_gmt":"2021-02-06T13:00:09","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=6110"},"modified":"2026-11-29T14:33:35","modified_gmt":"2026-11-29T09:03:35","slug":"how-to-backup-our-mongodb-and-push-the-file-into-aws-s3-in-ubuntu","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-backup-our-mongodb-and-push-the-file-into-aws-s3-in-ubuntu\/","title":{"rendered":"How to Backup MongoDB to AWS S3 on Ubuntu"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong><strong>Post Date:<\/strong><\/strong> February 6, 2021<strong><br><strong>Last Updated:<\/strong> <\/strong>November 26, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/mongodb.com\/docs\/atlas\/backup\/cloud-backup\/export\/aws-s3\/\" target=\"_blank\" rel=\"noopener\">MongoDB backup to AWS S3<\/a><\/strong> 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB provides a command-line utility called <a href=\"https:\/\/www.mongodb.com\/docs\/database-tools\/mongodump\/\" target=\"_blank\" rel=\"noopener\"><code>mongodump<\/code><\/a> that allows administrators to create database backups. These backup files can be compressed and securely uploaded to <strong>Amazon S3 (Simple Storage Service)<\/strong> using the AWS Command Line Interface (AWS CLI).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this step-by-step guide, we will explain <strong>how to back up MongoDB to AWS S3 on Ubuntu<\/strong>. You will learn how to use <code>mongodump<\/code> to export MongoDB databases, compress backup files using <code>tar<\/code>, and upload the generated backup archives to an Amazon S3 bucket using AWS CLI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 <strong>MongoDB backup automation<\/strong>, securely store database backups in AWS S3, and improve your overall database backup and recovery process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before starting the MongoDB backup configuration, make sure you have the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An Ubuntu server with MongoDB installed and running.<\/li>\n\n\n\n<li>Access to the MongoDB database.<\/li>\n\n\n\n<li>MongoDB Database Tools installed (<code>mongodump<\/code>).<\/li>\n\n\n\n<li>AWS CLI installed on the Ubuntu server.<\/li>\n\n\n\n<li>An AWS account with an S3 bucket.<\/li>\n\n\n\n<li>Appropriate AWS permissions to upload files to the S3 bucket.<\/li>\n\n\n\n<li>SSH access with a user account that can execute the backup script.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Check MongoDB Service Status<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, connect to your Ubuntu server through SSH.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check whether the MongoDB service is running:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl status mongod<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If MongoDB is not running, start the service:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl start mongod<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Enable MongoDB to start automatically after a server reboot:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl enable mongod<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Verify MongoDB Database Tools<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB provides the <code>mongodump<\/code> utility through MongoDB Database Tools. This utility is used to export MongoDB database contents.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check whether <code>mongodump<\/code> is installed:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongodump --version<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the command is available, it will display the installed version.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If it is not installed, install MongoDB Database Tools using the <a href=\"https:\/\/www.mongodb.com\/docs\/database-tools\/installation\/installation\" target=\"_blank\" rel=\"noopener\">official MongoDB installation <\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Install and Configure AWS CLI<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">AWS CLI is required to upload backup files from the Ubuntu server to Amazon S3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check whether AWS CLI is installed:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">aws --version<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If AWS CLI is not installed, install it using:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt update\nsudo apt install awscli -y<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After installation, configure AWS credentials:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">aws configure<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Enter the following details when prompted:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">AWS Access Key ID: YOUR_ACCESS_KEY\nAWS Secret Access Key: YOUR_SECRET_KEY\nDefault region name: ap-south-1\nDefault output format: json<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use credentials belonging to an IAM user or role with the required S3 permissions. Avoid using root account credentials.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Verify AWS CLI access:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">aws sts get-caller-identity<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the command returns your AWS account and identity information, AWS CLI authentication is working.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/AWS-CLI-Setup-and-Identity-Verification.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"341\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/AWS-CLI-Setup-and-Identity-Verification-1024x341.png\" alt=\"AWS CLI installation and configuration commands on Ubuntu for MongoDB backup to AWS S3.\" class=\"wp-image-13947\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/AWS-CLI-Setup-and-Identity-Verification-1024x341.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/AWS-CLI-Setup-and-Identity-Verification-300x100.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/AWS-CLI-Setup-and-Identity-Verification-768x256.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/AWS-CLI-Setup-and-Identity-Verification-1536x512.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/AWS-CLI-Setup-and-Identity-Verification-2048x683.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Create an S3 Bucket for MongoDB Backups<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Log in to your AWS Management Console.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Open the Amazon S3 service.<\/li>\n\n\n\n<li>Click Create bucket.<\/li>\n\n\n\n<li>Enter a unique bucket name.<\/li>\n\n\n\n<li>Select your preferred AWS region.<\/li>\n\n\n\n<li>Keep Block Public Access enabled.<\/li>\n\n\n\n<li>Enable default server-side encryption.<\/li>\n\n\n\n<li>Click Create bucket.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">For this example, we will use the following S3 bucket path:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">s3:\/\/my_bucket\/db_backups\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>my_bucket<\/code> with your actual S3 bucket name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can verify access to the bucket using:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">aws s3 ls s3:\/\/my_bucket\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the bucket is accessible, AWS CLI will display its contents.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Create a MongoDB Backup Script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now we will create a Bash script to automate MongoDB database backups and upload them to AWS S3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, create a directory for backup scripts:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir -p ~\/bin<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create the backup script:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nano ~\/bin\/db-backups.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following script:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\n# MongoDB Backup Configuration\n\nBACKUP_DIR=\"$HOME\/mongodb-backups\"\nS3_BUCKET=\"s3:\/\/my_bucket\/db_backups\"\n\n# Generate timestamp\nDATE=$(date +%Y-%m-%d_%H-%M-%S)\n\n# Backup directory and archive name\nBACKUP_NAME=\"mongodb-backup-$DATE\"\nBACKUP_PATH=\"$BACKUP_DIR\/$BACKUP_NAME\"\nARCHIVE=\"$BACKUP_DIR\/$BACKUP_NAME.tar.gz\"\n\n# Create backup directory\nmkdir -p \"$BACKUP_DIR\"\n\n# Create MongoDB database dump\nmongodump --out \"$BACKUP_PATH\"\n\n# Check backup status\nif [ $? -ne 0 ]; then\n    echo \"MongoDB backup failed!\"\n    exit 1\nfi\n\n# Compress backup directory\ntar -czf \"$ARCHIVE\" -C \"$BACKUP_DIR\" \"$BACKUP_NAME\"\n\n# Check compression status\nif [ $? -ne 0 ]; then\n    echo \"Backup compression failed!\"\n    exit 1\nfi\n\n# Upload backup to AWS S3\naws s3 cp \"$ARCHIVE\" \"$S3_BUCKET\/\"\n\n# Check upload status\nif [ $? -ne 0 ]; then\n    echo \"AWS S3 upload failed!\"\n    exit 1\nfi\n\n# Remove local temporary backup files after successful upload\nrm -rf \"$BACKUP_PATH\"\nrm -f \"$ARCHIVE\"\n\necho \"MongoDB backup completed successfully!\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Replace <code>my_bucket<\/code> with your actual S3 bucket name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above script performs the following operations:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Creates a timestamp-based backup directory.<\/li>\n\n\n\n<li>Uses <code>mongodump<\/code> to export MongoDB database contents.<\/li>\n\n\n\n<li>Compresses the backup directory into a <code>.tar.gz<\/code> archive.<\/li>\n\n\n\n<li>Uploads the compressed backup file to the configured AWS S3 bucket.<\/li>\n\n\n\n<li>Removes the temporary local backup files after a successful upload.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The script stops if the backup, compression, or upload operation fails, helping prevent unsuccessful backups from being treated as completed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your MongoDB server requires authentication or uses a remote connection, configure the appropriate MongoDB connection URI and pass it to <code>mongodump<\/code> using the <code>--uri<\/code> option.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">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.<br><a href=\"https:\/\/pheonixsolutions.com\/devops-cloud\/\">Explore our DevOps &amp; Cloud Services<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Give Execute Permission to the Script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After saving the script, provide execute permission:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">chmod +x ~\/bin\/db-backups.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Verify the file permissions:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ls -l ~\/bin\/db-backups.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see executable permissions for the script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 7: Configure Cron Job for Automatic Backups<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The next step is to configure a cron job to execute the MongoDB backup script automatically every day.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cron is a Linux scheduling utility that allows administrators to execute scripts and commands at specific times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, open the crontab configuration:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">crontab -e<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following entry:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">0 0 * * * \/bin\/bash \/home\/ubuntu\/bin\/db-backups.sh >> \/home\/ubuntu\/logs\/db_backups.log 2>&amp;1<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Before adding the cron job, create the log directory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir -p \/home\/ubuntu\/logs<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Understanding the cron schedule:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">0 0 * * *<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above schedule represents:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Minute: 0<\/li>\n\n\n\n<li>Hour: 0 (12:00 AM)<\/li>\n\n\n\n<li>Day of the month: Every day<\/li>\n\n\n\n<li>Month: Every month<\/li>\n\n\n\n<li>Day of the week: Every day<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This configuration runs the MongoDB backup script every day at midnight.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important:<\/strong> Replace <code>\/home\/ubuntu<\/code> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 8: Test the MongoDB Backup Script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before waiting for the scheduled cron execution, manually run the backup script to verify that everything is working correctly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Execute:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/bin\/bash \/home\/ubuntu\/bin\/db-backups.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the process completes successfully, you should see:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MongoDB backup completed successfully!<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, check the backup location in your AWS account.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open the Amazon S3 bucket and navigate to:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_bucket\/db_backups\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see a backup archive with a timestamp-based filename, similar to:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongodb-backup-2026-11-26_00-00-00.tar.gz<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also verify the uploaded backup using AWS CLI:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">aws s3 ls s3:\/\/my_bucket\/db_backups\/<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 9: Verify Cron Job Execution and Logs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After configuring the cron job, verify that the scheduled task is registered:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">crontab -l<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check the backup log file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cat \/home\/ubuntu\/logs\/db_backups.log<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To monitor the logs continuously:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">tail -f \/home\/ubuntu\/logs\/db_backups.log<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also check the cron service status:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl status cron<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the cron service is not running, start and enable it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl start cron\nsudo systemctl enable cron<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 10: Restore MongoDB Backup from AWS S3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Having a backup is useful only when the database can be restored successfully. It is recommended to periodically test your MongoDB backup restoration process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, download the backup archive from AWS S3:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">aws s3 cp s3:\/\/my_bucket\/db_backups\/mongodb-backup-2026-11-26_00-00-00.tar.gz \/tmp\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Extract the backup archive:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">tar -xzf \/tmp\/mongodb-backup-2026-11-26_00-00-00.tar.gz -C \/tmp\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Restore the MongoDB database using:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mongorestore --drop \/tmp\/mongodb-backup-2026-11-26_00-00-00\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>--drop<\/code> 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For authenticated MongoDB connections, use the appropriate <code>--uri<\/code> option with <code>mongorestore<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After restoration, connect to MongoDB and verify that the databases and collections have been restored successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for MongoDB Backup Management<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Consider the following recommendations to improve your backup strategy:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Schedule regular backups:<\/strong> Configure daily or more frequent backups depending on your application requirements.<\/li>\n\n\n\n<li><strong>Secure AWS credentials:<\/strong> Use IAM roles or restricted IAM credentials instead of storing unrestricted access keys.<\/li>\n\n\n\n<li><strong>Enable S3 encryption:<\/strong> Protect database backup archives using server-side encryption.<\/li>\n\n\n\n<li><strong>Configure backup retention:<\/strong> Use Amazon S3 Lifecycle policies to manage older backup files and storage costs.<\/li>\n\n\n\n<li><strong>Monitor backup logs:<\/strong> Regularly review backup logs to identify failed executions.<\/li>\n\n\n\n<li><strong>Test database restoration:<\/strong> Periodically restore backups in a test environment to verify data integrity.<\/li>\n\n\n\n<li><strong>Monitor available disk space:<\/strong> Ensure sufficient local storage is available while generating and compressing backup files.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Automating MongoDB backups and storing them in Amazon S3 is an effective way to protect important database information and improve disaster recovery readiness.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining <code>mongodump<\/code>, <code>tar<\/code>, AWS CLI, and Linux cron jobs, you can create a scheduled backup process on your Ubuntu server without manually performing the backup every day.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the script and cron job are configured, MongoDB backup files will be generated, compressed, and uploaded automatically to your configured S3 bucket.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions (FAQs)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. How do I back up a MongoDB database on Ubuntu?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the <code>mongodump<\/code> command to export MongoDB databases on Ubuntu. The generated backup files can be compressed using <code>tar<\/code> and stored locally or uploaded to an AWS S3 bucket using AWS CLI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. How can I automatically upload MongoDB backups to AWS S3?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the AWS CLI <code>aws s3 cp<\/code> 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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. How often should I back up my MongoDB database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Backup frequency depends on your application&#8217;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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-create-an-aws-s3-bucket-using-terraform\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create an AWS S3 Bucket Using Terraform<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-mount-wasabi-storage-bucket-on-centos-and-ubuntu-20-using-s3fs\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Mount Wasabi Storage Bucket on CentOS and Ubuntu 20 using S3FS<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MongoDB Backup and AWS Cloud Solutions with Expert DevOps Support<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Protecting critical database information and maintaining reliable backup systems are essential for business continuity and data security. At <a href=\"https:\/\/pheonixsolutions.com\/devops-cloud\/\">Pheonix Solutions<\/a>, our DevOps experts help businesses manage MongoDB databases, automate database backups, configure secure AWS S3 storage, and maintain reliable cloud infrastructure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can help you with:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/devops-cloud\/\">DevOps and Cloud Services<\/a> for MongoDB database management, AWS infrastructure, cloud automation, server administration, and backup configuration.<\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/web-development\/\">Website Development<\/a> to build secure, responsive, and high-performing websites integrated with reliable database solutions.<\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/software-maintenance-and-support\/\">Software Maintenance and Support<\/a> to troubleshoot application issues, maintain database environments, optimize server performance, and ensure smooth application operations.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Need help automating MongoDB backups, configuring AWS S3 storage, implementing database backup strategies, or managing your cloud infrastructure? <a href=\"https:\/\/pheonixsolutions.com\/contact\/\">Talk to our DevOps and Cloud experts<\/a> to discuss your requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: February 6, 2021Last Updated: November 26, 2026 Introduction MongoDB backup to AWS S3 is an effective way to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13949,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1019],"tags":[804,182,817,816],"class_list":["post-6110","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-aws","tag-aws-s3","tag-backup","tag-backup-mongodb-and-push-file-into-aws-s3","tag-mongodb","psol-cat-cloud-aws"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Ay","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/MongoDB-to-AWS-S3-Backup-Guide.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=6110"}],"version-history":[{"count":4,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6110\/revisions"}],"predecessor-version":[{"id":14136,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6110\/revisions\/14136"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/13949"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=6110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=6110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=6110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}