--- title: "Back up a MySQL database to E2E Object Storage" description: "Back up a MySQL database to an E2E Object Storage bucket on a schedule with s3cmd and cron, and restore it from there when you need it." content_type: "how-to" product: "object-storage" platform: "MyAccount" url: "/myaccount/object-storage/how-to/back-up-a-mysql-database-to-object-storage/" topic: ["use-with-other-products"] relationships: {"requires":["/myaccount/object-storage/how-to/create-a-bucket/","/myaccount/object-storage/how-to/connect-with-s3cmd/"]} --- # Back up a MySQL database to E2E Object Storage This works from any Linux server that can reach the internet, including a [node](/myaccount/node/). You need a bucket and s3cmd configured first; see [Connect with s3cmd](/myaccount/object-storage/how-to/connect-with-s3cmd/). ## Write the backup script Create `/opt/scripts/mysqlbackup.sh` with your database's own details filled in: ```bash #!/bin/bash USER="" PASSWORD="" HOST="localhost" DBNAME="" S3BUCKET="s3://" 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: ```bash frame="none" 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:///`; 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 Add a line to your crontab (`crontab -e`) to run it on the schedule you want. This example runs weekly: ```text 0 0 * * 0 bash /opt/scripts/mysqlbackup.sh >/dev/null 2>&1 ``` ## 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. 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: ```bash frame="none" s3cmd ls s3:/// ``` You see one line per file, with its date, size, and full path, for example `s3:///shop-12-Sep-2026-00:00:01.sql.gz`. 2. Download that file and unpack it. Replace `` with the file name from the list: ```bash frame="none" s3cmd get s3:///.sql.gz gunzip .sql.gz ``` This leaves `.sql` in the current directory. 3. Create an empty database to restore into. Replace `` with the name you want the restored database to have: ```bash frame="none" mysql -u -p -e "CREATE DATABASE ;" ``` 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: ```bash frame="none" mysql -u -p < .sql ``` Both `mysql` commands ask for the database user's password. 5. Check that the tables came back: ```bash frame="none" mysql -u -p -e "SHOW TABLES;" ``` 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](/myaccount/object-storage/how-to/set-a-lifecycle-rule/).