How to Back Up a MariaDB Database to S3 with Automysqlbackup

A database backup that lives on the same disk as the database is not a backup. It is a copy waiting for the same failure. If you want to back up MariaDB database to S3, the practical setup is simple: Automysqlbackup creates a dated dump every night, and a small script pushes that dump to object storage. Setup takes about half an hour, and a restore test proves it works before you ever need it for real.

What Automysqlbackup Does for a MariaDB Server

Automysqlbackup is a small shell script that wraps mysqldump. Each run dumps every database (or the ones you list), compresses the output with gzip, and stores the files in dated folders. By default it keeps daily dumps for seven days, weekly dumps for five weeks, and monthly dumps for twelve months. That rotation alone fixes the most common backup mistake: one dump that gets overwritten before anyone notices it is corrupt.

The tool does not talk to S3 itself. That is a strength, not a gap. It does one job well, and you pair it with s3cmd, the AWS CLI, or rclone for the upload step. If you later move storage providers, only the upload line changes. Readers who want a fuller offsite strategy should also look at our guide on how to back up a VPS to S3-compatible storage with Restic, which covers whole-server snapshots for the same bucket.

One honest limit: Automysqlbackup takes logical dumps, not physical snapshots. A dump of a very large database can take minutes and briefly lock MyISAM tables. InnoDB tables, which MariaDB uses by default, dump cleanly with a single transaction. For most WordPress sites, small shops, and app backends on a VPS, a nightly logical dump is the right trade-off.

What You Need Before You Start

Gather four things first. First, a MariaDB user with read access to every database you want to back up. A dedicated backup user beats root: give it SELECT, SHOW VIEW, TRIGGER, and LOCK TABLES privileges, and store its long random password in a credentials file, not shell history.

Second, an S3 bucket in a region close to your VPS, with a private access policy. Enable versioning if your provider supports it, so an accidental overwrite never destroys the only good copy. Third, access keys scoped to that bucket only. Fourth, enough free disk space for at least two full dumps, because the upload script should only delete a local dump after the upload is confirmed.

Install Automysqlbackup on Your VPS

On Debian and Ubuntu, install the package from the distribution repository:

sudo apt update
sudo apt install automysqlbackup

The package installs the script at /usr/sbin/automysqlbackup and a configuration file at /etc/default/automysqlbackup. Check the installed version with automysqlbackup --version first.

Configure Automysqlbackup for MariaDB

Open /etc/default/automysqlbackup and set the connection details first:

USERNAME=backupuser
PASSWORD='your-long-random-password'
DBHOST=localhost
DBNAMES="all"
BACKUPDIR="/var/backups/mysql"

Setting DBNAMES to all backs up every database, including new ones you add later. That is the safer default for a VPS hosting several small sites. If you run one large database and several throwaway test databases, list only the ones that matter.

Next, set the rotation and compression options. The defaults are sensible: gzip compression on, daily rotation of seven days, weekly rotation of five weeks, monthly rotation of twelve months.

Run the tool once by hand and watch the output:

sudo automysqlbackup

Check /var/backups/mysql/daily/ for a dated folder with one .sql.gz file per database. Open one with zcat and confirm it starts with real SQL, not an error message. A dump that contains “Access denied” instead of table definitions is worse than no dump at all. This is the same check we recommend in our guide on how to back up MySQL databases automatically with mysqldump and Cron, where the dump command runs directly instead of through a wrapper.

Back Up MariaDB Database to S3 with an Upload Script

With local dumps working, add the upload step. This automysqlbackup s3 pattern keeps the two jobs separate: one tool dumps, another uploads. Install the AWS CLI or s3cmd, then write a short mariadb backup script that uploads the newest daily folder and verifies the result. The verification matters more than the upload itself: a script that deletes local files after a failed upload destroys your only copy.

#!/bin/bash
set -e
BACKUP_DIR="/var/backups/mysql/daily"
BUCKET="s3://your-bucket-name/mariadb"
LATEST=$(ls -dt "$BACKUP_DIR"/*/ | head -1)
aws s3 sync "$LATEST" "$BUCKET/$(basename "$LATEST")/" --delete
echo "Uploaded $LATEST to $BUCKET"

Save this as /usr/local/bin/mariadb-backup-to-s3.sh and make it executable. The set -e line stops the script on any error, so a failed upload never reaches a cleanup step.

If you prefer s3cmd, the equivalent line is s3cmd sync "$LATEST" "$BUCKET/". For MinIO or another S3-compatible service, point the AWS CLI at your endpoint with --endpoint-url. Our walkthrough on how to set up a S3-compatible backup for your VPS with MinIO shows the endpoint configuration if you self-host the storage side.

Run the script once by hand and confirm the files appear in the bucket. Then add a cleanup line that removes local daily folders older than two days, but only after the sync succeeds. Keeping two days locally gives you a fast restore path for yesterday’s mistake, while S3 holds the longer history.

Schedule Nightly Backups with Cron

Automysqlbackup on Debian and Ubuntu already ships a daily cron entry, but check that it exists and runs at a quiet hour. A 02:00 run suits most sites. If you installed the script manually, add your own entry:

0 2 * * * /usr/sbin/automysqlbackup >> /var/log/automysqlbackup.log 2>&1
15 2 * * * /usr/local/bin/mariadb-backup-to-s3.sh >> /var/log/automysqlbackup.log 2>&1

The fifteen-minute gap gives the dump time to finish before the upload starts. Logging both steps to one file makes morning checks easy: one glance tells you whether the dump ran, whether the upload ran, and whether either failed. Add a simple alert if you can, even a cron job that emails you when the log contains the word “error”.

WordPress owners can reuse the same pattern. Our guide on how to back up a WordPress database automatically every night covers the WordPress-specific pieces.

Test a Restore Before You Trust the Backup

A backup you have never restored is a hope, not a plan. Test the restore on the same VPS once a month, into a scratch database, so the test never touches production data:

mysql -u root -p -e "CREATE DATABASE restore_test;"
zcat /var/backups/mysql/daily/$(date +%Y-%m-%d)_*/yourdb.sql.gz | mysql -u root -p restore_test
mysql -u root -p -e "SELECT COUNT(*) FROM restore_test.wp_posts;"

If the table count looks right, drop the scratch database and note the test date in your runbook. Also test a restore from S3 at least once: download a dump from the bucket and repeat the same steps. That second test catches the failure mode people forget, which is a backup that uploads fine but downloads as a truncated file. Watch the size trend too: set a lifecycle rule to move dumps older than 90 days to a cheaper storage class, and check official pricing for that class before relying on it.

Common Problems and Fixes

The most frequent error is an authentication failure on the first run. Automysqlbackup prints “Access denied for user” and creates an empty or error-filled dump. Fix the credentials in /etc/default/automysqlbackup, grant the backup user the privileges listed above, and rerun.

The second common problem is a disk that fills up. A growing database plus seven days of rotation needs real space. Check /var/backups with a weekly df -h, or set an alert at 80 percent disk use. If space is tight, lower daily rotation to three days and lean on S3 for history.

The third is a silent cron failure. Cron runs with a minimal environment, so a command that works in your shell can fail in cron because aws is not on its PATH. Use full paths in the cron entry and in the script, and check the log file after the first scheduled run.

Frequently Asked Questions

Can Automysqlbackup back up a remote MariaDB server?

Yes. Set DBHOST to the remote host and open the MariaDB port only to your VPS IP. Run it over a VPN or an SSH tunnel rather than the open internet.

How long should I keep dumps in S3?

Keep daily dumps for 30 days, weekly dumps for three months, and monthly dumps for a year. That covers yesterday’s bad deploy, last month’s deleted table, and a request for an old snapshot. A database dump restores the database only, not the web server configuration or uploaded files, so pair it with a file-level backup of the VPS.

Once the nightly job runs and the restore test passes, a monthly glance at the log and bucket size is enough. That is the point of a mariadb s3 backup automation built from small, boring parts: each piece is easy to inspect, easy to replace, and easy to trust because you have watched it work.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.