AutoMySQLBackup: backing up MySQL and MariaDB databases automatically

AutoMySQLBackup is a small script that automatically backs up all MySQL or MariaDB databases on a server – compressed and rotated into daily, weekly and monthly backups. For a typical web server with WordPress, Nextcloud or Paperless it is a simple, reliable solution that works right after installation.

Installation

sudo apt install automysqlbackup

The package configures itself: on Debian and Ubuntu it logs in with the maintenance credentials from /etc/mysql/debian.cnf or as root through the Unix socket. As a rule you do not need to enter a user name or password.

First test

sudo automysqlbackup
sudo ls -lR /var/lib/automysqlbackup

The backup directory now contains the subfolders daily, weekly and monthly, each with one folder per database holding compressed SQL files, for example daily/wordpress/wordpress_2026-11-30_06h25m.Monday.sql.gz.

Automatic runs and rotation

The package installs a daily cron job at /etc/cron.daily/automysqlbackup – so there is nothing else to set up. The rules are:

  • daily: one backup per day; the files of the last week are kept
  • weekly: one backup per week (Saturdays by default); several weeks are kept
  • monthly: one backup on the 1st of the month; these are not deleted automatically

You should therefore tidy up the monthly folder by hand from time to time.

Configuration

The settings are in /etc/default/automysqlbackup. The most important ones:

# Which databases? "all" = all of them
DBNAMES="all"

# Databases to exclude (separated by spaces)
DBEXCLUDE="performance_schema information_schema"

# Target directory
BACKUPDIR="/var/lib/automysqlbackup"

# Mail report: "log" = report, "quiet" = only on errors, "stdout" = no mail
MAILCONTENT="quiet"
MAILADDR="admin@example.org"

# Day of the week for the weekly backup (1 = Monday … 7 = Sunday)
DOWEEKLY=6

# Compression: gzip or bzip2
COMP=gzip

For mail delivery the server must be able to send mail – how to do that with msmtp is covered in the article on unattended-upgrades.

If the database does not run with the default access (e.g. MySQL 8 with a dedicated admin user), enter the credentials in the file with USERNAME= and PASSWORD= and protect the file with chmod 600.

Restoring

# Restore into an existing (empty) database
zcat /var/lib/automysqlbackup/daily/wordpress/wordpress_2026-11-30_06h25m.Monday.sql.gz | sudo mysql wordpress

# Or into a new database for testing
sudo mysql -e "CREATE DATABASE wordpress_test"
zcat …sql.gz | sudo mysql wordpress_test

On newer MariaDB versions the programs are called mariadb and mariadb-dump; the old names mysql and mysqldump still work, though.

Important: store backups off the server

If the backups only exist on the same server, they are no help after a disk failure or on a compromised system. The directory /var/lib/automysqlbackup should therefore also be copied to another system – for example encrypted with duplicity or via rsync to Strato HiDrive.

Originally published in 2022, revised in November 2026. Source of the first version: ServerPilot.