Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Back Up One Database

sudo mysqldump --single-transaction --routines --triggers --events appdb > appdb.sql

On Ubuntu and Debian the database root account usually logs in through the system root account, so sudo is enough and no password is asked. On MariaDB the same tool is called mariadb-dump and takes the same options; the old mysqldump name is a symlink that MariaDB 11 marks as deprecated. To see which databases exist, see how to list MySQL databases.

What the Options Do

  • --single-transaction: reads all InnoDB tables from one consistent point in time without locking them. MyISAM and MEMORY tables can still change during the dump, and ALTER TABLE, DROP TABLE, RENAME TABLE, or TRUNCATE TABLE on a table being dumped can make the dump wrong or fail, so schedule it away from migrations.
  • --routines: includes stored procedures and functions, which are left out by default.
  • --triggers: includes triggers. It is on by default; writing it keeps the intent clear.
  • --events: includes scheduled events, which are left out by default.

Keep Credentials in an Option File

A password after -p on the command line shows up in the process list and in shell history. Create a backup account with read rights instead:

CREATE USER 'backup'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT, PROCESS ON *.* TO 'backup'@'localhost';

The global SELECT also covers --routines; on MySQL, SHOW_ROUTINE is the narrower privilege for that. PROCESS is needed for tablespace information; leave it out if you dump with --no-tablespaces. Then store the login in ~/.my.cnf of the account that runs the backup:

[mysqldump]
user=backup
password=STRONG_PASSWORD
chmod 600 ~/.my.cnf

mysqldump reads the [mysqldump] group of this file by itself. The mysql client ignores that group, so sudo mysql still logs in as the database root account.

All Databases or Several

mysqldump --single-transaction --routines --triggers --events --all-databases > all.sql
mysqldump --single-transaction --routines --triggers --events --databases appdb shopdb > two.sql

Both forms add CREATE DATABASE and USE statements, so the dump restores into the original database names. --all-databases also includes the system tables with user accounts; when moving to a different server version, per-database dumps are easier to restore.

Compress Through a Pipe

mysqldump --single-transaction --routines --triggers --events appdb | gzip > appdb-$(date +%F).sql.gz

SQL text compresses well. In a script, set set -o pipefail so a failed dump is not hidden by a successful gzip.

A Nightly Script

#!/bin/bash
# /usr/local/sbin/mysql-backup
set -euo pipefail
umask 077
dir=/var/backups/mysql
day=$(date +%F)
mkdir -p "$dir"
for db in appdb shopdb; do
  mysqldump --single-transaction --routines --triggers --events "$db" | gzip > "$dir/$db-$day.sql.gz"
done
find "$dir" -name '*.sql.gz' -mtime +14 -delete

Make it executable with sudo chmod 700 /usr/local/sbin/mysql-backup, put the option file in /root/.my.cnf, and add it to root's crontab:

15 2 * * * /usr/local/sbin/mysql-backup >> /var/log/mysql-backup.log 2>&1

Cron is covered in how to create a cron job.

Where the Dumps Go

/var/backups/mysql is a staging folder on the same disk, not a backup by itself. Copy it off the server every night with restic, Borg, or rsync, and keep at least one copy away from the server's provider.

Test the Restore

sudo mysql -e 'CREATE DATABASE appdb_restoretest'
sudo sh -c 'gunzip < /var/backups/mysql/appdb-2026-11-28.sql.gz | mysql appdb_restoretest'
sudo mysql -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='appdb_restoretest'"

Compare the table count, and the row count of a few important tables, with the live database. Remove the scratch database afterward, checking its name twice. The full import procedure is in how to import an SQL file into MySQL.

Was this answer helpful?
Loading...