Operating System Ubuntu

How to Install and Configure AutoMySQLBackup on Ubuntu 24.04

Set up AutoMySQLBackup from the Ubuntu package, understand that upstream stopped in 2011, and replace it with a small maintained script and systemd timer.

AutoMySQLBackup is a shell script that wraps mysqldump and keeps daily, weekly and monthly dumps of your databases. This guide installs and configures it on Ubuntu 24.04 LTS (the steps are the same on Ubuntu 22.04 and Debian 12), and then shows a small script with a systemd timer that does the same job with tools that are still maintained. Ubuntu 16.04, which older guides target, is past its standard support period.

Is AutoMySQLBackup still maintained?

Know what you are installing before you rely on it for backups:

  • The upstream project on SourceForge has not published a release since version 3.0 (a release candidate) in December 2011.
  • Ubuntu 22.04 and 24.04 still carry an automysqlbackup package in the universe repository. It is the older 2.6 line with Debian patches, not version 3.0.
  • Because of that, guides that edit /etc/automysqlbackup/automysqlbackup.conf and set CONFIG_mysql_dump_username-style variables do not match the package. Those belong to the 3.0 tarball. The package is configured in /etc/default/automysqlbackup with different variable names.

The package still works for simple setups, but nobody is developing the script. If you are building a new server, the script in the second half of this guide is a better foundation. On AlmaLinux 9 and Rocky Linux 9, use that script as well.

Requirements

  • Ubuntu 24.04 or 22.04 (or Debian 12) with MySQL or MariaDB running.
  • A user with sudo rights.

Step 1: Install the package

sudo apt update
sudo apt install automysqlbackup

The package installs three things that matter:

  • /usr/sbin/automysqlbackup — the script itself
  • /etc/default/automysqlbackup — the configuration
  • /etc/cron.daily/automysqlbackup — a cron job, so backups start running daily without further setup

Step 2: Create a database account for backups

Give the script its own read-only account instead of the database root password. Open a client session:

sudo mysql
CREATE USER 'backup'@'localhost' IDENTIFIED BY 'choose-a-long-password';
GRANT SELECT, SHOW VIEW, TRIGGER, EVENT, LOCK TABLES, PROCESS ON *.* TO 'backup'@'localhost';
exit

Step 3: Configure AutoMySQLBackup

First look at what the package set for you. The values differ between releases, so read them rather than assume:

grep -E '^(USERNAME|PASSWORD|DBHOST|DBNAMES|BACKUPDIR)=' /etc/default/automysqlbackup

Open the file:

sudo nano /etc/default/automysqlbackup

Set the following variables. Leave the others at their packaged values unless you have a reason to change them:

USERNAME=backup
PASSWORD="choose-a-long-password"
DBHOST=localhost
DBNAMES="all"
DBEXCLUDE=""
BACKUPDIR="/var/backups/automysqlbackup"
DOWEEKLY=6
COMP=gzip
MAILCONTENT="quiet"
MAILADDR="root"
VariableMeaning
USERNAME, PASSWORD, DBHOSTHow the script connects to the database server.
DBNAMESSpace-separated list of databases for the daily and weekly runs, or all.
DBEXCLUDEDatabases to skip when DBNAMES is all.
MDBNAMESDatabases included in the monthly run.
BACKUPDIRWhere the dumps are written.
DOWEEKLYDay of the weekly backup, 1 (Monday) to 7 (Sunday).
COMPCompression: gzip or bzip2.
SEPDIRyes writes one directory and file per database.
MAILCONTENT, MAILADDRWhat to mail and to whom: log, files, stdout or quiet (errors only). Mail needs a working local mail setup.
PREBACKUP, POSTBACKUPOptional commands to run before and after the backup, for example to copy the files to another host.

The file now contains a password, so restrict it to root. The cron job runs as root and can still read it:

sudo chmod 600 /etc/default/automysqlbackup

Step 4: Run a backup and check the result

Run the script once by hand:

sudo automysqlbackup

List what it wrote. You should see directories for the daily, weekly and monthly sets with compressed .sql files inside:

sudo ls -R /var/backups/automysqlbackup

From now on /etc/cron.daily/automysqlbackup runs it once a day. You do not need to create a cron job or symlink yourself. All options are described in the manual page:

man automysqlbackup

Step 5: Restore from a backup

The files are ordinary compressed SQL dumps. Create the target database if it does not exist and load the dump into it. Adjust the path to a file from your own listing:

sudo mysql -e "CREATE DATABASE IF NOT EXISTS appdb;"
gunzip < /path/to/appdb-dump.sql.gz | sudo mysql appdb

Test this on a scratch database before you need it for real.

The maintained alternative: a script and a systemd timer

Everything AutoMySQLBackup does can be done with a few lines around the dump client that ships with your database server, and you can read and audit every line. The script below writes one compressed file per database into a dated directory and removes directories older than a week. It runs as root and uses the local socket login that Ubuntu and Debian set up for the database root account, so no password is stored anywhere.

sudo nano /usr/local/sbin/db-backup
#!/bin/bash
set -euo pipefail
umask 077

BACKUP_DIR=/var/backups/databases
KEEP_DAYS=7
DAY=$(date +%F)

mkdir -p "$BACKUP_DIR/$DAY"

mysql -N -B -e 'SHOW DATABASES' \
  | grep -Ev '^(information_schema|performance_schema|sys)$' \
  | while read -r db; do
      mysqldump --single-transaction --routines --events --databases "$db" \
        | gzip > "$BACKUP_DIR/$DAY/$db.sql.gz"
    done

find "$BACKUP_DIR" -mindepth 1 -maxdepth 1 -type d -mtime +"$KEEP_DAYS" -exec rm -rf {} +

On a MariaDB server, replace mysql with mariadb and mysqldump with mariadb-dump. Make the script executable and test it:

sudo chmod 700 /usr/local/sbin/db-backup
sudo /usr/local/sbin/db-backup
sudo ls -lh /var/backups/databases/$(date +%F)

Create a service unit that runs the script:

sudo nano /etc/systemd/system/db-backup.service
[Unit]
Description=Dump all databases

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/db-backup

Create the timer that starts it every night at 02:30. Persistent=true runs a missed backup after the server has been powered off:

sudo nano /etc/systemd/system/db-backup.timer
[Unit]
Description=Nightly database dump

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true

[Install]
WantedBy=timers.target

Enable the timer and confirm that it is scheduled:

sudo systemctl daemon-reload
sudo systemctl enable --now db-backup.timer
systemctl list-timers db-backup.timer

After a run, a failed dump shows up as a failed unit, and the output is in the journal:

systemctl status db-backup.service
sudo journalctl -u db-backup.service

If you use this script, remove the package so two jobs do not dump the same databases every night:

sudo apt remove automysqlbackup

Frequently asked questions

Why is there no /etc/automysqlbackup/automysqlbackup.conf on my server?

That file belongs to AutoMySQLBackup 3.0, which is installed by hand from the SourceForge tarball. The Ubuntu and Debian package is the 2.6 line and reads /etc/default/automysqlbackup instead. Settings named CONFIG_… have no effect there.

Does AutoMySQLBackup work with MariaDB?

The script calls the mysqldump and mysql command names. It works as long as your MariaDB packages still provide those names as symlinks; MariaDB has deprecated them from version 11.0, which is one more reason to prefer a script that calls mariadb-dump directly.

Are the backups safe if they stay on the same server?

No. They protect you from a dropped table or a bad update, not from a lost disk or a deleted server. Copy the backup directory to another machine or to object storage after each run, for example from a POSTBACKUP command or an extra line at the end of the script.

Can I get incremental backups this way?

No. Every run is a full logical dump. For incremental or very large backups, use a physical backup tool for your server, such as mariadb-backup on MariaDB.

Share story:

Continue reading other guides on How VPS.