How to Auto Backup Database from WHM to AWS S3
Summary: Step by step automatic zipped SQL backup to AWS S3 using cron.
Ensuring your server's databases are regularly backed up is a vital part of web hosting management. In this tutorial, we'll walk you through a reliable method to automatically back up your MySQL databases from a WHM (cPanel) server to an AWS S3 bucket, compressing the backups and scheduling the entire process using cron. This solution is low-maintenance, secure, and cloud-scalable.
Table of Contents
- Prerequisites
- Step 1: Create an S3 Bucket & Access Keys
- Step 2: Install AWS CLI on Your Server
- Step 3: Configure AWS CLI
- Step 4: Write Backup Script
- Step 5: Automate with Cron
- Conclusion
Prerequisites
- Root access to your WHM/cPanel server (typically via SSH)
- Amazon Web Services (AWS) account
- Familiarity with basic Linux commands
Step 1: Create an S3 Bucket & Access Keys
1.1. Create S3 Bucket
- Log into AWS Console: Go to https://console.aws.amazon.com/s3/
- Create a new bucket:
- Click Create bucket.
- Enter a unique Bucket name, e.g.,
my-whm-backups
- Choose a region close to your server location.
- Leave other settings as default or customize as needed, then click Create bucket.
1.2. Create IAM User & Access Key
- Go to IAM → Users → Add users.
- Set user name, e.g.,
s3backup-user
. - Enable Programmatic access.
- Set permissions (recommend: "Attach existing policies directly" → AmazonS3FullAccess or custom policy limiting to your bucket).
- Create user and download/save access key ID and secret access key.
Step 2: Install AWS CLI on Your Server
SSH into your WHM server:
ssh root@your-server-ip
For most CentOS/CloudLinux systems:
yum install -y awscli
Or, for Ubuntu/Debian:
apt update
apt install -y awscli
Check installation:
aws --version
Step 3: Configure AWS CLI
Run the configuration wizard:
aws configure
- AWS Access Key ID: (from IAM user)
- AWS Secret Access Key: (from IAM user)
- Default region name: (e.g.,
us-east-1
) - Default output format:
json
This creates a config in ~/.aws/credentials
for the root (or specified) user.
Step 4: Write Backup Script
Create a script that:
- Dumps your databases as .sql files.
- Compresses them.
- Uploads to S3 with a timestamp.
- Create and edit the script file:
nano /root/mysql-s3-backup.sh
Paste the following:
#!/bin/bash
# Variables
BACKUP_DIR="/root/db_backups"
DATE=$(date +%F_%H-%M)
MYSQL_USER="root"
MYSQL_PASS="YOUR_MYSQL_ROOT_PASSWORD"
S3_BUCKET="s3://my-whm-backups/mysql-backups"
# Create backup dir if not exists
mkdir -p $BACKUP_DIR
# Get list of databases (you may want to exclude system DBs)
databases=$(mysql --user=$MYSQL_USER --password="$MYSQL_PASS" -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql|sys)")
for db in $databases; do
mysqldump --user=$MYSQL_USER --password="$MYSQL_PASS" --databases $db | gzip > $BACKUP_DIR/${db}_${DATE}.sql.gz
done
# Optionally: tar all into one file
tar -czf $BACKUP_DIR/all_databases_${DATE}.tar.gz $BACKUP_DIR/*.sql.gz
# Upload to S3
aws s3 cp $BACKUP_DIR/all_databases_${DATE}.tar.gz $S3_BUCKET/
# Clean up old backups (older than 7 days)
find $BACKUP_DIR -type f -name "*.gz" -mtime +7 -delete
find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +7 -delete
Remember: Replace YOUR_MYSQL_ROOT_PASSWORD
and my-whm-backups
with your own credentials and bucket name.
- Save, exit, and make the script executable:
chmod 700 /root/mysql-s3-backup.sh
Step 5: Automate with Cron
Automate the script to run daily (or as needed):
- Edit root's crontab:
crontab -e
- Add this line for daily backup at 2:30am:
30 2 * * * /root/mysql-s3-backup.sh > /dev/null 2>&1
(Adjust time as needed.)
Conclusion
You now have an automated system for zipped SQL backups from your WHM/cPanel MySQL server to AWS S3! This approach ensures offsite, cloud-hosted backups managed via cron, providing peace of mind against data loss. Be sure to test the backup and restore process periodically, and keep your AWS keys secure.
Have questions or want to share your backup strategies? Let us know in the comments below!
Tags: WHM, MySQL, AWS, S3, Backup, Cron, Automation, SysAdmin