Production-Ready MySQL Backup Script (with Compression and AWS Upload)
dev.to·3h·
Discuss: DEV
Flag this post

🗄️ Automate MySQL Backups to AWS S3 with a Simple Bash Script

Backups are one of those things you don’t think about… until it’s too late.

I wanted a simple, no-dependency solution for MySQL backups that I could run via cron, store safely on AWS S3, and clean up automatically.

So I wrote a Bash script that does exactly that — checks versions, loads environment variables, dumps the DB, compresses it, uploads to S3, and cleans up afterward.

Here’s the full script and a breakdown of how it works. 👇


💾 Script

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

DIRNAME=$(dirname "$0")
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")

echo "Starting database backup for $TIMESTAMP";

check_versions() {
aws --version
mysqldump --version
}

load_env() {
echo "Loading .env file";

ENV_FILE="$DIR...

Similar Posts

Loading similar posts...