Skip to content

Back up a MySQL database to E2E Object Storage

View as Markdown

This works from any Linux server that can reach the internet, including a node. You need a bucket and s3cmd configured first; see Connect with s3cmd.

Create /opt/scripts/mysqlbackup.sh with your database’s own details filled in:

#!/bin/bash
USER="<db-user>"
PASSWORD="<db-password>"
HOST="localhost"
DBNAME="<db-name>"
S3BUCKET="s3://<bucket-name>"
LOCATION="/tmp/backups"
TIMESTAMP=$(date +"%d-%b-%Y-%H:%M:%S")
mkdir -p $LOCATION
mysqldump -h$HOST -u$USER -p$PASSWORD $DBNAME | gzip -9 > $LOCATION/$DBNAME-$TIMESTAMP.sql.gz
s3cmd sync $LOCATION/ $S3BUCKET/
rm -rf $LOCATION/*

Make it runnable and test it:

chmod +x /opt/scripts/mysqlbackup.sh
/opt/scripts/mysqlbackup.sh

A successful run leaves a new .sql.gz file in your bucket. Check with s3cmd ls s3://<bucket-name>/; the new file is named after the database and the time it ran, such as shop-23-Sep-2026-10:15:02.sql.gz.

Add a line to your crontab (crontab -e) to run it on the schedule you want. This example runs weekly:

0 0 * * 0 bash /opt/scripts/mysqlbackup.sh >/dev/null 2>&1

To restore, download a backup from the bucket and load it with the mysql client. Do this on the server that should end up with the data.

  1. List the backups in the bucket and find the one you want. Each file is named after the database and the time the backup ran:

    s3cmd ls s3://<bucket-name>/

    You see one line per file, with its date, size, and full path, for example s3://<bucket-name>/shop-12-Sep-2026-00:00:01.sql.gz.

  2. Download that file and unpack it. Replace <backup-file> with the file name from the list:

    s3cmd get s3://<bucket-name>/<backup-file>.sql.gz
    gunzip <backup-file>.sql.gz

    This leaves <backup-file>.sql in the current directory.

  3. Create an empty database to restore into. Replace <db-name> with the name you want the restored database to have:

    mysql -u <db-user> -p -e "CREATE DATABASE <db-name>;"
  4. Load the backup into it. If you point this at a database that already holds data, the tables in the backup overwrite the tables of the same name, so restore into a new, empty database unless you mean to replace what is there:

    mysql -u <db-user> -p <db-name> < <backup-file>.sql

    Both mysql commands ask for the database user’s password.

  5. Check that the tables came back:

    mysql -u <db-user> -p -e "SHOW TABLES;" <db-name>

    You should see the same tables the database had when the backup ran.

Your database now goes to the bucket on a schedule, and you know how to get it back. Do one test restore into a spare database now, so the first real one holds no surprises. To keep old backups from piling up in the bucket, see Set a lifecycle rule.