Percona XtraBackup
Percona XtraBackup works with MySQL, MariaDB, and Percona Server. It supports completely non-blocking backups of InnoDB, XtraDB, and HailDB storage engines. In addition, it can back up the following storage engines by briefly pausing writes at the end of the backup: MyISAM, Merge, and Archive, including partitioned tables, triggers, and database options.
https://learn.percona.com/hubfs/Datasheet/Percona_XtraBackup/Datasheet_Percona_XtraBackup.pdfInstall
sudo yum install percona-xtrabackup-80
- libdev
- mariadb-connector-c
- perl-DBD-MySQL
- perl-DBI
- perl-Data-Dumper
- perl-Digest
- perl-Digest-MD5
- perl-Math-BigInt
- perl-Math-Complex
- rsync
Create Database User
CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY RANDOM PASSWORD;
GRANT RELOAD ON *.* TO 'bkpuser'@'localhost';
GRANT LOCK TABLES ON *.* TO 'bkpuser'@'localhost';
GRANT BACKUP_ADMIN ON *.* TO 'bkpuser'@'localhost';
GRANT REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost';
GRANT CREATE TABLESPACE ON *.* TO 'bkpuser'@'localhost';
GRANT PROCESS ON *.* TO 'bkpuser'@'localhost';
GRANT SUPER ON *.* TO 'bkpuser'@'localhost';
GRANT CREATE ON *.* TO 'bkpuser'@'localhost';
GRANT ALTER ON *.* TO 'bkpuser'@'localhost';
GRANT INSERT ON *.* TO 'bkpuser'@'localhost';
GRANT SELECT ON *.* TO 'bkpuser'@'localhost';
GRANT SELECT ON performance_schema.log_status TO 'bkpuser'@'localhost';
GRANT SELECT ON performance_schema.keyring_component_status TO bkpuser@'localhost';
GRANT SELECT ON performance_schema.replication_group_members TO bkpuser@'localhost';
FLUSH PRIVILEGES;
Backup
xtrabackup --user=bkpuser --password=mypassword --backup --target-dir=/backup
xtrabackup -u bkpuser -p --backup --databases=mydatabase --target-dir=/backup --host=myhost
Compression
--compress=lz4
TL;DR Use LZ4 Compression.
The following compression algorithms are available: quicklz, lz4, Zstandard (ZSTD). Quicklz is the default, but is also deprecated. ZSTD is a "tech preview" feature first available in 8.0.30. "Tech preview" features are NOT considered production ready.Example
xtrabackup --login-path=xtrabackup --backup --rsync --target-dir=/var/lib/mysql/backup --host=localhost --compress=lz4
Backup Script
Prerequisites
sudo -u mysql -s mysql_config_editor set --host=localhost --user=bkpuser --password --login-path=xtrabackup
Script
vi /usr/local/bin/mysqlbackup.sh
# Set VariablesBACKUP_DIR="/var/lib/mysql/backup/FULL"LOCKFILE="/tmp/xtrabackup.lock"LOG_DIR="/var/lib/mysql/backup/log"LOGFILE="${LOG_DIR}/mysqlbackup.log.$(date '+%d')"
# Initialise Directories (irrelevant if they already exist)mkdir -p ${BACKUP_DIR}mkdir -p ${LOG_DIR}
# Prevent more than one copy of this script from runningtouch ${LOCKFILE}read myPID < ${LOCKFILE}[ ! -z "${myPID}" -a -d /proc/${myPID} ] && exitecho $$ > ${LOCKFILE}
# Preserve last complete backup whilst new backup in progressmv ${BACKUP_DIR} ${BACKUP_DIR}_old
# Backup. If successful, remove previous backupxtrabackup --login_path=xtrabackup --backup --rsync --target-dir=${BACKUP_DIR} --host=localhost --compress=lz4 2>>${LOGFILE}if [ $? -eq 0 ]then rm -rf ${BACKUP_DIR}_oldfi
# Remove lockfilerm -f ${LOCKFILE}
exit 0
Test
sh -o xtrace /usr/local/bin/mysqlbackup.sh
Backup Scheduling
sudo -u mysql -s crontab -e
E.g. daily backup at 10pm...
00 22 * * * /usr/local/bin/mysqlbackup.sh >/tmp/mysqlbackup.$(date +'%d').log 2>&1
Check
sudo -u mysql -s crontab -l