Log inSign up

Protect MySQL and MariaDB data with dumps and physical backups

For most sites, a nightly mysqldump --single-transaction (or mariadb-dump on MariaDB) per database, compressed and copied off the server, is the backup you need; restore it with mysql dbname < dump.sql. To restore one database from an all-databases dump use mysql --one-database, and when data grows past a few gigabytes switch to physical backups with mariadb-backup or Percona XtraBackup, which copy data files while the server keeps running.

Topic: Databases · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Which backup method should you use?

Use logical dumps for small and medium databases and physical backups for large ones or when restore time matters. Many servers run both: a physical backup nightly and a dump of the most important database every few hours.

  • Logical dump (mysqldump, mariadb-dump): SQL text you can read, edit, restore into another version and pick apart. Slow to restore on large data because every index is rebuilt.
  • Physical backup (mariadb-backup, XtraBackup): a consistent copy of the InnoDB files. Fast to restore, but tied to the same major version and to the whole instance.
  • Binary logs: the change stream between backups, used for point in time recovery.

Whatever you pick, a backup on the same disk as the database protects you only against mistakes, not against losing the server. Copy every backup elsewhere, and consider the daily backups add-on as a whole-disk safety net. The install guides for MySQL and MariaDB explain the server side.

How do you store credentials for backup scripts?

Put the login in an option file readable only by the account that runs the backups, so passwords never appear on the command line or in the process list. Create a dedicated backup user first:

sudo mysql
CREATE USER 'backup'@'localhost' IDENTIFIED BY 'Long-Random-Backup-Secret';
GRANT SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT, PROCESS, RELOAD, REPLICATION CLIENT ON *.* TO 'backup'@'localhost';
EXIT;

On MariaDB 10.5 and newer, add BINLOG MONITOR instead of REPLICATION CLIENT if the server complains. Then write the option file:

cat > ~/.my.cnf <<'EOF'
[client]
user=backup
password=Long-Random-Backup-Secret
EOF
chmod 600 ~/.my.cnf
mysql -e 'SELECT CURRENT_USER();'

Both mysql and mariadb client programs read ~/.my.cnf, and so do the dump tools.

How do you dump one database or all of them?

Dump with --single-transaction so InnoDB tables are copied from one consistent snapshot without locking the site, and include routines, triggers and events.

sudo install -d -o "$USER" -m 700 /var/backups/mysql
cd /var/backups/mysql
# one database (use mariadb-dump on MariaDB)
mysqldump --single-transaction --quick --routines --triggers --events shop | gzip > shop-$(date +%F).sql.gz
# every database in one file
mysqldump --single-transaction --quick --routines --triggers --events --all-databases | gzip > all-$(date +%F).sql.gz
ls -lh

On MySQL servers with GTIDs enabled, add --set-gtid-purged=OFF when you plan to restore into a server that is not a replica. MyISAM tables are not covered by the snapshot; convert them to InnoDB or accept a short lock with --lock-tables.

User accounts need care. On MariaDB, mariadb-dump --system=users writes them as portable CREATE USER and GRANT statements. On MySQL, save the output of SHOW CREATE USER and SHOW GRANTS for each account, which MySQL users and privileges walks through.

How do you restore a dump?

Create the empty database, then feed the dump into the client. Restore into a test name first when you only need to check or recover a few rows.

mysql -e 'CREATE DATABASE shop_restore CHARACTER SET utf8mb4;'
gunzip -c shop-2026-12-01.sql.gz | mysql shop_restore
mysql -e 'SELECT COUNT(*) FROM shop_restore.orders;'

The backup user cannot create databases or write data, so run restores as an administrator, for example with sudo mysql on Ubuntu or sudo mariadb on MariaDB, which log in through the socket. To replace the live database, stop the application first, then drop and recreate it, or restore into the new name and switch the app config over once you have checked the data. Large restores run faster if you temporarily raise innodb_buffer_pool_size and disable the slow query log.

How do you restore one database from a full dump?

Use the client option --one-database: it reads the whole all-databases file but runs only the statements that belong to the database you name.

sudo mysql -e 'CREATE DATABASE IF NOT EXISTS shop;'
gunzip -c all-2026-12-01.sql.gz | sudo mysql --one-database shop

This relies on the USE statements that an all-databases dump contains, so it works on files made with --all-databases or --databases. Alternatively, cut that database out into its own file with sed, which also lets you inspect it:

gunzip -c all-2026-12-01.sql.gz | sed -n '/^-- Current Database: `shop`/,/^-- Current Database: `/p' > shop-only.sql
head -n 30 shop-only.sql

The same idea extracts one table: print from -- Table structure for table `orders` up to the next table structure line, and load the result into a scratch database before copying rows back.

How do you take a physical backup of MariaDB with mariabackup?

Install the mariadb-backup package, run a backup into an empty directory, then prepare it so the files are consistent and ready to copy back.

sudo apt install -y mariadb-backup       # Ubuntu, Debian
sudo dnf install -y mariadb-backup       # Rocky, Alma
sudo mariadb-backup --backup --target-dir=/var/backups/mariadb/full-$(date +%F) --user=root
sudo mariadb-backup --prepare --target-dir=/var/backups/mariadb/full-2026-12-01

Running as root through sudo uses the socket login, so no password is needed on Ubuntu and Debian. To restore, the data directory must be empty and the server stopped:

sudo systemctl stop mariadb
sudo mv /var/lib/mysql /var/lib/mysql.old
sudo mariadb-backup --copy-back --target-dir=/var/backups/mariadb/full-2026-12-01
sudo chown -R mysql:mysql /var/lib/mysql
sudo systemctl start mariadb

Once the restored server looks right, remove /var/lib/mysql.old. On Rocky and Alma with SELinux enforcing, run sudo restorecon -R /var/lib/mysql before the start.

How do you take a physical backup of MySQL with XtraBackup?

Use Percona XtraBackup in the series that matches your server: 8.4 for MySQL 8.4 LTS and 8.0 for MySQL 8.0. Install the percona-release helper from the vendor repository, enable the right channel and install the tool.

# Ubuntu or Debian
curl -fsSL -O https://repo.percona.com/apt/percona-release_latest.generic_all.deb
sudo apt install -y ./percona-release_latest.generic_all.deb
sudo percona-release enable pxb-84-lts
sudo apt update && sudo apt install -y percona-xtrabackup-84
# Rocky or Alma
sudo dnf install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm
sudo percona-release enable pxb-84-lts
sudo dnf install -y percona-xtrabackup-84
xtrabackup --version

The workflow mirrors mariabackup: backup, prepare, then copy back into an empty data directory with the server stopped.

sudo xtrabackup --backup --target-dir=/var/backups/mysql/xb-$(date +%F) --user=root
sudo xtrabackup --prepare --target-dir=/var/backups/mysql/xb-2026-12-01
sudo systemctl stop mysql             # mysqld on Rocky and Alma
sudo mv /var/lib/mysql /var/lib/mysql.old
sudo xtrabackup --copy-back --target-dir=/var/backups/mysql/xb-2026-12-01
sudo chown -R mysql:mysql /var/lib/mysql
sudo systemctl start mysql

On servers where root uses a password, add --password to be prompted, or put a [xtrabackup] section in the root option file.

How do you automate backups and recover to a point in time?

Run the dump from a systemd timer, keep a fixed number of days, copy the files off the server and test a restore every month. A small script and timer do the job:

sudo tee /usr/local/bin/db-backup >/dev/null <<'EOF'
#!/bin/sh
set -eu
cd /var/backups/mysql
for db in $(mysql -N -e "SHOW DATABASES" | grep -Ev '^(information_schema|performance_schema|sys|mysql)$'); do
  mysqldump --single-transaction --quick --routines --triggers --events "$db" | gzip > "$db-$(date +%F).sql.gz"
done
find /var/backups/mysql -name '*.sql.gz' -mtime +14 -delete
EOF
sudo chmod 755 /usr/local/bin/db-backup

Schedule it with a service and timer pair running as the user that owns ~/.my.cnf; cron jobs work just as well. Ship the folder to another machine with rsync or restic.

For point in time recovery, keep binary logs. MySQL 8 writes them by default; on MariaDB set log_bin = mysql-bin in the server config. After restoring the last full backup, replay the logs up to just before the mistake:

sudo mysqlbinlog --stop-datetime="2026-12-01 14:29:00" /var/lib/mysql/binlog.000042 | sudo mysql
# MariaDB: mariadb-binlog with the mysql-bin.* files

Check the start position against the backup: a physical backup records it in xtrabackup_binlog_info or mariadb_backup_binlog_info.

Questions about Back up and restore MySQL and MariaDB

Is mysqldump safe to run on a live site?

Yes for InnoDB tables when you add --single-transaction: it reads one consistent snapshot without blocking writes. It adds read load, so run it at a quiet hour on busy servers.

What is the difference between mysqldump and mariadb-dump?

They are the same tool family. MariaDB renamed its programs to mariadb-dump, mariadb and so on, and newer releases may drop the old names, so scripts for MariaDB should use the new ones.

Can I restore a MySQL 8.4 dump into MariaDB?

Often, for plain tables and data. Collations such as utf8mb4_0900_ai_ci and some MySQL-only features do not exist in MariaDB, so replace those in the dump and test first.

Can I restore a physical backup into a different version?

Only into the same major version, or as part of an official upgrade path. To move between major versions or between MySQL and MariaDB, use a logical dump.

How often should I test restores?

At least monthly, and after every change to the backup script. Restore into a scratch database, count rows in key tables and open the app against it.

Where should backup files live?

Off the server, in a second location you control. A copy on the same disk only protects against human error.