- MyAccount
- E2E Object Storage
- Use With Other Products
Back up a MySQL database to E2E Object Storage
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.
Write the backup script
Section titled “Write the backup script”Create /opt/scripts/mysqlbackup.sh with your database’s own details filled in:
#!/bin/bashUSER="<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 $LOCATIONmysqldump -h$HOST -u$USER -p$PASSWORD $DBNAME | gzip -9 > $LOCATION/$DBNAME-$TIMESTAMP.sql.gzs3cmd sync $LOCATION/ $S3BUCKET/rm -rf $LOCATION/*Make it runnable and test it:
chmod +x /opt/scripts/mysqlbackup.sh/opt/scripts/mysqlbackup.shA 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.
Schedule the MySQL backup
Section titled “Schedule the MySQL backup”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>&1Restore a backup
Section titled “Restore a backup”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.
-
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. -
Download that file and unpack it. Replace
<backup-file>with the file name from the list:s3cmd get s3://<bucket-name>/<backup-file>.sql.gzgunzip <backup-file>.sql.gzThis leaves
<backup-file>.sqlin the current directory. -
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>;" -
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>.sqlBoth
mysqlcommands ask for the database user’s password. -
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.