Updated: 11 October 2026 · Applies to: Ubuntu 24.04 and Debian 12 (AlmaLinux/Rocky script included)
automysqlbackup and autopostgresqlbackup create daily, weekly and monthly database dumps with automatic rotation. Both are packaged for Ubuntu 24.04 and Debian 12; on AlmaLinux/Rocky use the simple script at the end.
MariaDB / MySQL: automysqlbackup
apt install automysqlbackup
It runs every day from /etc/cron.daily/automysqlbackup and stores dumps in /var/lib/automysqlbackup/ (daily, weekly, monthly folders). Settings are in /etc/default/automysqlbackup, for example BACKUPDIR, DBEXCLUDE and MAILADDR. Run it once by hand with automysqlbackup to test.
PostgreSQL: autopostgresqlbackup
apt install autopostgresqlbackup
Configuration is in /etc/default/autopostgresqlbackup; backups go to /var/lib/autopostgresqlbackup/.
Restore
zcat /var/lib/automysqlbackup/daily/shop/shop_2026-09-28.sql.gz | mariadb shop zcat /var/lib/autopostgresqlbackup/daily/shop/shop_2026-09-28.sql.gz | sudo -u postgres psql shop
AlmaLinux / Rocky: a simple daily dump
#!/bin/bash
# /usr/local/bin/db-backup.sh
DIR=/backup/mysql/$(date +%F); mkdir -p "$DIR"
for db in $(mariadb -N -e "SHOW DATABASES" | grep -Ev "^(information_schema|performance_schema|sys)$"); do
mariadb-dump --single-transaction --routines --events "$db" | gzip > "$DIR/$db.sql.gz"
done
find /backup/mysql -maxdepth 1 -type d -mtime +14 -exec rm -rf {} +
Make it executable and run it daily from cron: 0 2 * * * /usr/local/bin/db-backup.sh.
Note: Backups on the same disk do not protect against disk failure. Copy them to another server as well (see "Back up your files with rsync and a VPS").