How to Back Up and Restore MySQL or MariaDB Databases on Ubuntu 24.04
Export databases with mysqldump or mariadb-dump, schedule a nightly compressed backup, and restore and test it on Ubuntu, Debian and AlmaLinux.
A database backup you have never restored is only a hope. This guide shows you how to export MySQL and MariaDB databases with mysqldump (or mariadb-dump), run the export every night without typing a password, and restore a dump when you need it. The commands apply to Ubuntu 24.04 LTS and 22.04, Debian 12 and 13, and AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9.
The dump is a logical backup: a text file of SQL statements that recreates your tables and data. It is the right tool for small and medium databases and for moving a database between servers.
Requirements
- A running MySQL or MariaDB server and a shell account with
sudo. - A database account that is allowed to read everything you want to back up. The
rootdatabase account works; a dedicated backup account is described below. - Free disk space for the dump files, ideally on a different disk or server from the database itself.
mysqldump or mariadb-dump?
MySQL ships the client as mysqldump. MariaDB renamed its client to mariadb-dump; the old name is still available as a symlink in most packages, but the MariaDB documentation marks that symlink as deprecated from MariaDB 11.0. The options used in this guide are the same for both, so on a MariaDB server replace mysqldump with mariadb-dump and mysql with mariadb.
Check which server you have:
sudo mysql -e "SELECT VERSION();"
Step 1: Decide how the backup logs in
On Ubuntu and Debian the database root account normally authenticates through the local socket, so any command you run with sudo is logged in without a password. That is enough for manual and scheduled backups on a single server.
If your server asks for a password, or you want the backup to run with fewer privileges, create a dedicated account. Open a client session:
sudo mysql
Create the account and grant the privileges that a dump needs. Replace the password with your own:
CREATE USER 'backup'@'localhost' IDENTIFIED BY 'choose-a-long-password';
GRANT SELECT, SHOW VIEW, TRIGGER, EVENT, LOCK TABLES, PROCESS ON *.* TO 'backup'@'localhost';
exit
Store the credentials in an option file so they never appear on the command line or in your shell history:
sudo nano /root/.my.cnf
[client]
user=backup
password=choose-a-long-password
Make the file readable by root only:
sudo chmod 600 /root/.my.cnf
Both client programs read the [client] group of this file when they run as root. Avoid -pPASSWORD on the command line: other users can see it in the process list.
Step 2: Export a database
Dump a single database named appdb into a file:
sudo mysqldump --single-transaction --routines --events appdb > appdb.sql
What the options do:
--single-transactionstarts a transaction before reading, so InnoDB tables are exported in a consistent state without blocking your application. It only helps with transactional tables such as InnoDB.--routinesand--eventsadd stored procedures, functions and scheduled events. Triggers are included by default.
To dump several databases, or all of them, use --databases or --all-databases. With these options the file also contains the CREATE DATABASE statements:
sudo mysqldump --single-transaction --routines --events --databases appdb shopdb > two-databases.sql
sudo mysqldump --single-transaction --routines --events --all-databases > all-databases.sql
SQL text compresses well. Pipe the output through gzip to save space:
sudo mysqldump --single-transaction --routines --events appdb | gzip > appdb.sql.gz
Step 3: Schedule a nightly backup
Create a script that dumps everything, compresses it and deletes files older than two weeks:
sudo nano /usr/local/sbin/db-backup
#!/bin/bash
set -euo pipefail
umask 077
BACKUP_DIR=/var/backups/mysql
KEEP_DAYS=14
STAMP=$(date +%F_%H%M)
mkdir -p "$BACKUP_DIR"
mysqldump --single-transaction --routines --events --all-databases \
| gzip > "$BACKUP_DIR/all-$STAMP.sql.gz"
find "$BACKUP_DIR" -name 'all-*.sql.gz' -mtime +"$KEEP_DAYS" -delete
set -o pipefail makes the script fail when the dump fails, instead of quietly leaving a truncated file, and umask 077 keeps the dumps private to root. On MariaDB, change mysqldump to mariadb-dump.
Make it executable and run it once by hand:
sudo chmod 700 /usr/local/sbin/db-backup
sudo /usr/local/sbin/db-backup
sudo ls -lh /var/backups/mysql
Then add a cron entry that runs it every night at 02:30 as root:
echo '30 2 * * * root /usr/local/sbin/db-backup' | sudo tee /etc/cron.d/db-backup
A backup stored on the same disk as the database disappears with it. Copy the directory to another machine after each run, for example with rsync over SSH to a backup host:
sudo rsync -a /var/backups/mysql/ deploy@203.0.113.10:/srv/backups/mysql/
Step 4: Restore a database
A dump of a single database (made without --databases) contains no CREATE DATABASE statement, so you choose the target yourself. Create an empty database and load the file into it:
sudo mysql -e "CREATE DATABASE appdb_restored;"
sudo mysql appdb_restored < appdb.sql
For a compressed file, decompress on the fly:
gunzip < appdb.sql.gz | sudo mysql appdb_restored
A dump made with --databases or --all-databases names its own databases, so you load it without a database argument. It overwrites tables that already exist under those names:
gunzip < all-2026-01-15_0230.sql.gz | sudo mysql
Verify your backups
Check that the archive is intact and that the dump ran to the end. A finished dump ends with a -- Dump completed comment:
sudo gunzip -t /var/backups/mysql/all-2026-01-15_0230.sql.gz
sudo zcat /var/backups/mysql/all-2026-01-15_0230.sql.gz | tail -n 1
The only real test is a restore. Every so often, load a recent dump into a scratch database or a test server as shown in step 4, check that the tables and row counts look right, and drop the scratch database again:
sudo mysql -e "SHOW TABLES IN appdb_restored;"
sudo mysql -e "DROP DATABASE appdb_restored;"
Frequently asked questions
Does mysqldump lock my tables while it runs?
With --single-transaction, InnoDB tables stay readable and writable during the dump. Without it, the default behaviour is to lock the tables of each database while it is being read. Non-transactional tables such as MyISAM cannot be dumped consistently without locking, so convert them to InnoDB if you can.
Are user accounts included in the backup?
Accounts and grants live in the mysql system database, which is included in an --all-databases dump. Restoring that onto a different server version can cause trouble. On MariaDB you can export the accounts as portable CREATE USER and GRANT statements with mariadb-dump --system=users.
My database is tens of gigabytes. Is a dump still the right tool?
It works, but both the export and especially the restore become slow, because every row is replayed as SQL. For large databases look at the physical backup tools that copy the data files while the server runs, such as mariadb-backup for MariaDB, and keep an occasional logical dump for portability.
How many backups should I keep?
Keep enough to go back past a mistake you notice late: a common pattern is daily files for one or two weeks plus a weekly or monthly copy held longer on another machine. Adjust KEEP_DAYS in the script and let the remote copy have its own retention.