AWS

How to Automate MySQL Database Backups on EC2 to Amazon S3

If you're running MySQL on EC2, you need automated backups. A single disk failure or accidental DROP TABLE can wipe out your data. This…

How to Automate MySQL Database Backups on EC2 to Amazon S3
Share:

How to Automate MySQL Database Backups on EC2 to Amazon S3

5 min read
1.2K views

If you’re running MySQL on EC2, you need automated backups. A single disk failure or accidental DROP TABLE can wipe out your data. This guide shows you how to automate MySQL database backups on EC2 to Amazon S3 using mysqldump, gzip compression, and a cron job. Backups upload directly to S3 using the AWS CLI — no s3fs mounts or access keys required.

Prerequisites

Step 1: Create an IAM Role for EC2

Instead of using access keys, attach an IAM instance profile to your EC2 instance. This gives the instance permission to upload to S3 without storing any credentials on disk.

Advertisement

Create a policy that grants write access to your backup bucket. Save this as s3-backup-policy.json:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::my-db-backups",
                "arn:aws:s3:::my-db-backups/*"
            ]
        }
    ]
}

Create the IAM role and attach the policy:

aws iam create-role \
  --role-name EC2-S3-Backup \
  --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}'

aws iam put-role-policy \
  --role-name EC2-S3-Backup \
  --policy-name S3BackupAccess \
  --policy-document file://s3-backup-policy.json

aws iam create-instance-profile \
  --instance-profile-name EC2-S3-Backup

aws iam add-role-to-instance-profile \
  --instance-profile-name EC2-S3-Backup \
  --role-name EC2-S3-Backup

Attach the instance profile to your EC2 instance (you can also do this in the EC2 console under Actions > Security > Modify IAM role):

Advertisement
aws ec2 associate-iam-instance-profile \
  --instance-id i-0123456789abcdef0 \
  --iam-instance-profile Name=EC2-S3-Backup

Verify it works from the EC2 instance:

aws s3 ls s3://my-db-backups

If this runs without errors, the instance profile is working.

Step 2: Store MySQL Credentials Securely

First, create a dedicated MySQL user with only the privileges mysqldump needs. On Ubuntu, sudo mysql logs you in as the MySQL root user:

sudo mysql
CREATE USER 'backup_user'@'localhost' IDENTIFIED BY 'YOUR_SECURE_PASSWORD';
GRANT SELECT, SHOW VIEW, TRIGGER, EVENT ON *.* TO 'backup_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
  • SELECT — read table data (granted globally, it also covers stored routines for --routines)
  • SHOW VIEW, TRIGGER, EVENT — dump views, triggers and scheduled events
  • No LOCK TABLES or PROCESS needed, because the script uses --single-transaction and --no-tablespaces

Never put MySQL passwords directly in a backup script. Use a MySQL option file instead. Create /root/.my.cnf:

sudo nano /root/.my.cnf

Add your MySQL credentials:

[mysqldump]
user=backup_user
password=YOUR_SECURE_PASSWORD

Lock down the permissions:

sudo chmod 600 /root/.my.cnf

With this in place, mysqldump picks up the credentials automatically when run as root — no -u or -p flags needed in the script.

Step 3: Create the Backup Script

Create the script at /usr/local/bin/mysql-backup-s3.sh:

sudo nano /usr/local/bin/mysql-backup-s3.sh

Add the following:

#!/bin/bash
set -euo pipefail
umask 077   # dump file readable by root only

# Configuration
DATABASE="myapp"
S3_BUCKET="s3://my-db-backups"
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_FILE="/tmp/${DATABASE}-${TIMESTAMP}.sql.gz"

# Remove the local dump even if a step fails
trap 'rm -f "$BACKUP_FILE"' EXIT

# Dump and compress
mysqldump --single-transaction --routines --triggers --events --no-tablespaces "$DATABASE" | gzip > "$BACKUP_FILE"

# Upload to S3
aws s3 cp "$BACKUP_FILE" "${S3_BUCKET}/${DATABASE}/${TIMESTAMP}.sql.gz"

echo "[$(date)] Backup completed: ${DATABASE}/${TIMESTAMP}.sql.gz"
  • set -euo pipefail — stops the script on any error, including pipe failures
  • umask 077 and trap ... EXIT — the dump is only readable by root while it exists, and it is deleted even when the upload fails
  • --single-transaction — takes a consistent snapshot without locking tables (InnoDB)
  • --routines --triggers --events — includes stored procedures, triggers and scheduled events in the dump
  • --no-tablespaces — skips tablespace metadata, which would otherwise need the PROCESS privilege
  • gzip — compresses the dump (typically 5–10x smaller)

Make it executable:

sudo chmod +x /usr/local/bin/mysql-backup-s3.sh

Test it manually:

sudo /usr/local/bin/mysql-backup-s3.sh

Check that the file appeared in S3:

aws s3 ls s3://my-db-backups/myapp/ --human-readable

Step 4: Back Up All Databases (Optional)

To back up every database on the server, replace the single database dump with --all-databases:

mysqldump --single-transaction --routines --triggers --events --no-tablespaces --all-databases | gzip > "$BACKUP_FILE"

Step 5: Schedule with Cron

Open the root crontab:

sudo crontab -e

Add a daily backup at 2:00 AM:

0 2 * * * /usr/local/bin/mysql-backup-s3.sh >> /var/log/mysql-backup.log 2>&1

Logs go to /var/log/mysql-backup.log so you can check if backups are running and catch errors.

A log file only helps if someone reads it. To get alerted when a backup fails, add an SNS notification to the script right after set -euo pipefail (replace the topic ARN with your own, and add sns:Publish on that topic to the instance role):

trap 'aws sns publish --topic-arn arn:aws:sns:your-region:123456789012:db-backup-alerts --message "MySQL backup failed on $(hostname)"' ERR
Schedule Cron Expression
Every day at 2 AM 0 2 * * *
Every 6 hours 0 */6 * * *
Every hour 0 * * * *
Twice a day (2 AM and 2 PM) 0 2,14 * * *

Step 6: Set Up S3 Lifecycle Policy for Retention

Instead of deleting old backups with a script, let S3 handle retention automatically with a lifecycle policy. This moves old backups to cheaper storage and eventually deletes them:

aws s3api put-bucket-lifecycle-configuration \
  --bucket my-db-backups \
  --lifecycle-configuration '{
    "Rules": [
      {
        "ID": "BackupRetention",
        "Status": "Enabled",
        "Filter": {"Prefix": ""},
        "Transitions": [
          {
            "Days": 30,
            "StorageClass": "GLACIER"
          }
        ],
        "Expiration": {
          "Days": 120
        }
      }
    ]
  }'
  • After 30 days, backups move to Glacier (much cheaper storage)
  • After 120 days, backups are deleted automatically

Glacier Flexible Retrieval has a 90-day minimum storage duration. If an object is deleted sooner, AWS bills the remaining days anyway. That’s why expiration is set to 120 days (30 days in Standard + 90 in Glacier). If you want a shorter retention, skip the Glacier transition or use STANDARD_IA (30-day minimum) instead. Adjust the days based on your retention requirements.

While you’re configuring the bucket, make sure it can never be made public and turn on versioning so an overwritten or deleted backup can be recovered:

aws s3api put-public-access-block \
  --bucket my-db-backups \
  --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true

aws s3api put-bucket-versioning \
  --bucket my-db-backups \
  --versioning-configuration Status=Enabled

With versioning on, add a NoncurrentVersionExpiration rule to the lifecycle policy too, or old versions will pile up and keep costing money.

Restoring from a Backup

Download the backup from S3 and restore it:

aws s3 cp s3://my-db-backups/myapp/2025-06-15_02-00-01.sql.gz /tmp/restore.sql.gz
gunzip /tmp/restore.sql.gz
mysql myapp < /tmp/restore.sql

Test your restores periodically — a backup that can’t be restored is worthless.

Conclusion

The setup is: IAM instance profile for S3 access, a bash script that runs mysqldump with gzip and uploads to S3, a cron job for scheduling, and a lifecycle policy for automatic retention. No access keys, no s3fs mounts, no manual cleanup scripts.

If you need to copy these backups to another AWS account for disaster recovery, see How to Copy S3 Bucket Objects Across AWS Accounts, or automate it with cross-account S3 uploads using AWS Lambda. For mounting S3 as a filesystem for other use cases, check out How to Mount S3 Bucket on Ubuntu 22.04 with S3FS Fuse.

Recommended for Linux builders

Contabo VPS hosting for Linux projects

Run Linux servers, automation tools, and self-hosted apps on a Contabo VPS with generous resources and predictable pricing.

Our customers sayExcellent4.6 out of 5 based on 11,008 reviews Trustpilot

Some links are affiliate links. This may earn LinuxBeast a commission at no extra cost to you.

Privacy settings

Choose which privacy controls to open.

Opening these controls does not change your saved choices.