If a website or an app on your server keeps its data in MariaDB or MySQL, copying the database files while the database runs can give a broken copy. The safe way is a dump: a file with everything needed to rebuild the database. This page makes a dump of every database each night, keeps the last 7 days and shows how to restore one. The commands were tested on Ubuntu 26.04 LTS with MariaDB 11.8; run them as root.
1. The Backup Script
Create the file /usr/local/bin/db-backup, for example with nano /usr/local/bin/db-backup:
#!/bin/bash # Nightly dump of every database into /var/backups/mysql, kept for 7 days. set -euo pipefail DIR=/var/backups/mysql mkdir -p "$DIR" chmod 700 "$DIR" for DB in $(mariadb -N -e "SHOW DATABASES" | grep -Ev "^(information_schema|performance_schema|sys)$"); do mariadb-dump --single-transaction --routines --triggers --events "$DB" | gzip > "$DIR/$DB-$(date +%F).sql.gz" done find "$DIR" -name "*.sql.gz" -mtime +7 -delete
- One compressed file per database and day, for example
shop-2026-11-28.sql.gz. --single-transactionmakes a consistent copy of InnoDB tables without stopping your website.- The last line deletes dumps older than 7 days. Change
+7to keep them longer. - The folder can be read by
rootonly: the dumps contain all your data.
On MySQL instead of MariaDB, write mysql instead of mariadb and mysqldump instead of mariadb-dump. On Ubuntu, root usually logs in to MariaDB without a password; if your database asks for one, see the note at the end.
Make the script executable and run it once:
chmod 700 /usr/local/bin/db-backup /usr/local/bin/db-backup ls -l /var/backups/mysql/
It writes one file for each of your databases and one for the system database mysql. gzip -t /var/backups/mysql/*.sql.gz checks that the files are complete; it prints nothing when they are.
2. Run It Every Night
Create the file /etc/cron.d/db-backup with these two lines; the backup then runs every night at 02:30, server time:
# nightly database backup 30 2 * * * root /usr/local/bin/db-backup
To check it the next morning, look at the dates of the files with ls -l /var/backups/mysql/, or at the log of cron: journalctl -u cron --since yesterday | grep db-backup. The log then shows (root) CMD (/usr/local/bin/db-backup) at the time you set. More about timed tasks: Run Tasks on a Schedule on Linux: cron and systemd Timers.
3. Restore a Database
Restore into a new, empty database first and look at the data before you touch the original:
mariadb -e "CREATE DATABASE shop_restore" zcat /var/backups/mysql/shop-2026-11-28.sql.gz | mariadb shop_restore
The restored tables have the same rows and the same totals as the original. When everything is right, restore into the original database in the same way, or point your website to the restored one.
The dump of mysql contains the database users and their passwords. Keep it, but do not restore it over a running server unless you have to rebuild the users.
4. Keep a Copy Away From the Server
Dumps on the same server do not help if the server is lost or reinstalled. Add /var/backups/mysql to an off-server backup, for example: Automatic Encrypted Backups of Your Server to Another Server With restic. Schedule the dump before the other backup, for example at 02:30 and the other at 03:00.
If the Database Asks for a Password
Put the login into the file /root/.my.cnf, which both commands read by themselves, and make it readable for root only with chmod 600 /root/.my.cnf:
[client] user=root password=YOUR-DATABASE-PASSWORD