How to Automate MySQLDump Backup from Server to Amazon S3

automate MySQLDump database backups from Ubuntu server to Amazon S3 cloud storage

Post Date: January 30, 2021
Last Updated:
November 24, 2026

Introduction

MySQLDump backup to Amazon S3 is an effective way to protect critical database information and prevent unexpected data loss. Regular MySQL database backups help maintain application availability and ensure important customer records, business transactions, and application data can be recovered when needed.

Using an automated MySQL backup on Ubuntu, administrators can create database backup files and store them securely in Amazon S3 cloud storage. By combining AWS CLI, S3FS, MySQLDump backup scripts, and Cron jobs, you can simplify database backup management and schedule daily backups without manual intervention.

In this step-by-step guide, you will learn how to configure AWS S3 backup storage, install AWS CLI on Ubuntu, connect an S3 bucket, create a MySQLDump backup script, schedule automatic database backups using Cron jobs, and verify uploaded backup files in Amazon S3.

Prerequisites

Before configuring automated MySQL backups to Amazon S3, make sure you have:

  • An Ubuntu server with MySQL installed.
  • Access to the MySQL database and its credentials.
  • An AWS account.
  • An Amazon S3 bucket for storing database backups.
  • An IAM user or IAM role with the required S3 permissions.
  • Root or sudo access to the Ubuntu server.

Step 1: Create an IAM User in AWS

First, log in to the AWS Management Console and create an IAM user for accessing Amazon S3.

  1. Open the AWS Management Console.
  2. Navigate to IAM (Identity and Access Management).
  3. Create a new IAM user.
  4. Generate the required access credentials.
  5. Attach an appropriate S3 permissions policy to the user.

For production environments, use a least-privilege IAM policy that allows access only to the required backup bucket instead of granting full S3 access.

Keep the access key ID and secret access key secure.

Step 2: Install and Configure AWS CLI

Install AWS CLI on your Ubuntu server using the following command:

sudo apt update
sudo apt install awscli -y
MySQLDump backup to Amazon S3 – installing AWS CLI on Ubuntu server using apt commands

After installation, configure AWS CLI:

aws configure

Enter the following details when prompted:

Configure AWS CLI on Ubuntu using IAM access key, secret access key, AWS region, and JSON output format
  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name
  • Default output format

Provide the credentials generated during IAM user creation.

Step 3: Verify AWS S3 Access

After configuring AWS CLI, verify whether your server can access Amazon S3.

Run:

aws s3 ls

This command lists the S3 buckets accessible to the configured AWS identity.

To check a specific bucket, use:

aws s3 ls s3://your-bucket-name/

If the command displays the bucket contents without permission errors, AWS CLI connectivity is working.

Step 4: Install S3FS on Ubuntu

S3FS allows you to mount an Amazon S3 bucket as a local filesystem on your Ubuntu server.

Install S3FS using:

sudo apt install s3fs -y

Next, create a credentials file:

nano /home/ubuntu/.s3fs-creds

Add your AWS credentials in the following format:

ACCESS_KEY_ID:SECRET_ACCESS_KEY

Save and close the file.

Set the appropriate file permissions to protect your credentials:

chmod 600 /home/ubuntu/.s3fs-creds
Install S3FS on Ubuntu and configure AWS credentials file with secure permissions for MySQL database backup to Amazon S3

Step 5: Create a Local Directory and Mount the S3 Bucket

Create a directory where the S3 bucket will be mounted:

mkdir -p /home/ubuntu/s3_uploads

Mount your Amazon S3 bucket using:

s3fs your-bucket-name /home/ubuntu/s3_uploads \
-o passwd_file=/home/ubuntu/.s3fs-creds

Replace your-bucket-name with your actual Amazon S3 bucket name.

Verify the mounted directory:

df -h

You can also check the mounted directory:

ls -lah /home/ubuntu/s3_uploads
Mount Amazon S3 bucket on Ubuntu using S3FS and verify the mounted directory with df -h and ls -lah commands

Our DevOps experts can help you configure Amazon S3 storage, automate MySQL backups, and manage secure cloud infrastructure.

Explore our DevOps & Cloud Services.

Step 6: Create a MySQL Configuration File

To avoid entering MySQL credentials every time the backup script runs, create a separate MySQL configuration file.

Create the file:

nano /home/ubuntu/.config.cnf

Add the following configuration:

[client]
user=dbuser
password=dbpassword
host=localhost

Replace dbuser and dbpassword with your actual MySQL username and password.

Save and close the file.

Set secure permissions:

chmod 600 /home/ubuntu/.config.cnf

This configuration allows MySQLDump to authenticate with the database without exposing credentials directly in the backup command.

Create MySQL configuration file on Ubuntu with database username password and secure chmod 600 permissions for automated MySQLDump backup

Step 7: Create an Automated MySQLDump Backup Script

Now, create a shell script to generate MySQL database backups automatically.

Create the script:

nano /home/ubuntu/mysqldump.sh

Add the following script:

#!/bin/bash

DBNAME="yourdbname"
BACKUP="/home/ubuntu/s3_uploads"

# Generate backup timestamp
DATE=$(date +'%Y%m%d%H%M%S')

# Create MySQL database backup
mysqldump \
--defaults-extra-file=/home/ubuntu/.config.cnf \
"$DBNAME" > "$BACKUP/mysqldump_$DATE.sql"

# Check backup status
if [ $? -eq 0 ]; then
    echo "MySQL backup completed successfully: mysqldump_$DATE.sql"
else
    echo "MySQL backup failed"
    exit 1
fi

Replace yourdbname with the actual MySQL database name.

Save and close the file.

The script performs the following operations:

  • Defines the MySQL database name.
  • Specifies the mounted S3 backup directory.
  • Generates a timestamp for each backup.
  • Executes MySQLDump to export the database.
  • Saves the backup file in the mounted S3 directory.
  • Checks whether the backup command completed successfully.

Step 8: Make the Backup Script Executable

Before running the script, provide executable permissions:

chmod +x /home/ubuntu/mysqldump.sh

This allows the script to execute directly from the command line.

Step 9: Configure Cron Jobs for Automatic Backups

To automate database backups, configure a Cron job on your Ubuntu server.

Open the Cron configuration:

crontab -e

If this is your first time opening the Cron editor, select your preferred text editor.

Add the following entry at the bottom:

@daily /home/ubuntu/mysqldump.sh

This configuration executes the MySQL backup script once every day.

Save and close the file.

Verify the configured Cron jobs:

crontab -l

Your MySQL database backup will now run automatically every day.

Step 10: Test the MySQLDump Backup Script

Before depending on the scheduled backup, manually execute the script to verify its functionality.

Run:

cd /home/ubuntu/
./mysqldump.sh

If the backup is successful, the script will display a completion message.

Step 11: Verify Backup Files in the Mounted S3 Directory

Navigate to the mounted S3 directory:

cd /home/ubuntu/s3_uploads

List the available backup files:

ls -lah

You should see the generated MySQL backup files with timestamp-based filenames.

For example:

mysqldump_20261124120000.sql
mysqldump_20261125120000.sql

These files are stored in the mounted S3 bucket.

Step 12: Verify Backup Files in the Amazon S3 Console

Finally, log in to the AWS Management Console and follow these steps:

  1. Open the Amazon S3 service.
  2. Navigate to your backup bucket.
  3. Open the bucket used for MySQL backups.
  4. Verify that the generated SQL backup files are available.
  5. Check the file names and upload timestamps.

You can also verify the uploaded files using AWS CLI:

aws s3 ls s3://your-bucket-name/

If the backup files appear in the S3 bucket, the backup storage configuration is working.

Best Practices for MySQL Backups to Amazon S3

For a reliable production backup strategy, consider the following recommendations:

  • Secure AWS credentials: Never expose AWS access keys or database passwords in publicly accessible files.
  • Use least-privilege permissions: Restrict IAM access to the required S3 bucket and operations.
  • Enable encryption: Protect database backup files using encryption at rest.
  • Configure backup retention: Define a retention policy to remove outdated backups safely.
  • Monitor backup execution: Check Cron logs and maintain backup execution logs.
  • Test database restoration: Regularly restore backup files in a test environment to confirm their integrity.
  • Monitor storage usage: Review S3 storage consumption and lifecycle policies periodically.

Conclusion

Automating MySQLDump backups from an Ubuntu server to Amazon S3 is an effective way to improve database protection and simplify backup management. By integrating AWS CLI, S3FS, MySQLDump, and Cron jobs, you can create a scheduled backup process that stores database backup files in cloud storage.

Following this guide helps reduce manual backup operations, maintain regular database backups, and improve data recovery readiness in case of unexpected server failures or accidental data loss.

For production environments, always protect AWS credentials, enable encryption, implement backup retention policies, and regularly test database restoration. A properly configured backup strategy helps maintain business continuity and protects critical application data.

Frequently Asked Questions (FAQs)

1. How do I automate MySQL database backups to Amazon S3 on Ubuntu?

You can automate MySQL database backups to Amazon S3 on Ubuntu using MySQLDump, AWS CLI, S3FS, and Cron jobs. Configure AWS credentials, mount the S3 bucket, create a shell script to export the MySQL database, and schedule the script using Cron to execute daily.

2. How can I verify that MySQL backups are successfully uploaded to Amazon S3?

You can verify MySQL backup uploads by checking the mounted S3 directory using the ls -lah command. You can also execute aws s3 ls s3://your-bucket-name/ to list backup files directly from Amazon S3. For additional reliability, periodically restore a backup in a test environment to confirm that the database can be recovered.

AWS Cloud Migration Checklist: A Complete Step-by-Step Guide
AWS EC2 Troubleshooting Guide: Common Issues and Solutions

Talk to DevOps and Cloud Experts

Maintaining reliable database backups is essential for protecting business data and ensuring application availability. At Pheonix Solutions, our team helps businesses manage cloud infrastructure, automate database backups, configure servers, and maintain secure application environments.

We can help you with:

Need help configuring Amazon S3 backups, automating MySQLDump backups, managing Ubuntu servers, or maintaining your cloud infrastructure? Talk to our DevOps and Cloud experts to discuss your requirements.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top