介绍
Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具。特点:
(1)备份过程快速、可靠;
(2)备份过程不会打断正在执行的事务;
(3)能够基于压缩等功能节约磁盘空间和流量;
(4)自动实现备份检验;
(5)还原速度快;
下载地址: https://www.percona.com/downloads/Percona-XtraBackup-LATEST/ 文档地址: https://www.percona.com/doc/percona-xtrabackup/2.4/index.html
安装
如果安装需要依赖就把依赖安装一下
wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb
sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb
sudo apt-get update
sudo apt-get install percona-xtrabackup-24
设置数据库用于备份账户
mysql> CREATE USER 'zlh'@'localhost' IDENTIFIED BY '123456';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost';
mysql> FLUSH PRIVILEGES;
全量备份
trabackup --user=zlh --password=123456 --backup --target-dir=/data/backups/mysql
# 会看到输出
200603 09:55:37 Executing UNLOCK TABLES
200603 09:55:37 All tables unlocked
200603 09:55:37 [00] Copying ib_buffer_pool to /data/backups/mysql/ib_buffer_pool
200603 09:55:37 [00] ...done
200603 09:55:37 Backup created in directory '/data/backups/mysql/'
200603 09:55:37 [00] Writing /data/backups/mysql/backup-my.cnf
200603 09:55:37 [00] ...done
200603 09:55:37 [00] Writing /data/backups/mysql/xtrabackup_info
200603 09:55:37 [00] ...done
xtrabackup: Transaction log of lsn (837940114) to (837940123) was copied.
200603 09:55:37 completed OK!
xtrabackup --prepare --target-dir=/data/backups/mysql
我这里为了演示全量备份就直接将我博客 mysql 存储的数据目录给移动一下
mv /var/lib/mysql /var/lib/mysql_bak
mkdir /var/lib/mysql
xtrabackup --copy-back --target-dir=/data/backups/mysql # 这样会保留原始备份 他会将当时读到my.cnf的datadir设置为恢复路径
200603 10:47:42 [01] ...done
200603 10:47:42 [01] Copying ./performance_schema/mutex_instances.frm to /var/lib/mysql/performance_schema/mutex_instances.frm
200603 10:47:42 [01] ...done
200603 10:47:42 [01] Copying ./performance_schema/events_transactions_history_long.frm to /var/lib/mysql/performance_schema/events_transactions_history_long.frm
200603 10:47:42 [01] ...done
200603 10:47:42 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info
200603 10:47:42 [01] ...done
200603 10:47:42 [01] Copying ./ibtmp1 to /var/lib/mysql/ibtmp1
200603 10:47:42 [01] ...done
200603 10:47:42 completed OK!
# 将恢复目录的属主更改一下
chown -R mysql:mysql mysql
/etc/init.d/mysql start
如果恢复完不想要备份数据可以使用 xtrabackup --move-back 命令
增量备份
增量是基于已有数据进行备份的,也就行需要先创建一次全量备份,然后记录当时的记录点
xtrabackup --user=bkpuser --password=123456 --backup --target-dir=/data/backups/base
# 基于全量备份进行增量
xtrabackup --user=bkpuser --password=123456 --backup --target-dir=/data/backups/inc1 --incrementa
root@longing:/data/backups/inc1# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 837943393
to_lsn = 837943393
last_lsn = 837943402
compact = 0
recover_binlog_info = 0
flushed_lsn = 837943402
from_lsn 是备份的起始 LSN,对于增量备份,它必须to_lsn与先前 base 备份的相同。
在这种情况下,您可以看到to_lsn (最后一个检查点LSN)和last_lsn(最后一个复制的LSN)之间存在差异,这意味着在备份过程中服务器上有一些流量。
??? 我们可以测试一下 对第一个增加继续创建增量 创建增量之前先创建几条数据 ?
trabackup --user=bkpuser --password=123456 --backup --target-dir=/data/backups/inc2 --incre
已经有3个备份了,我们要先对基础数据进行准备,然后对两个增量进行准备
xtrabackup --user=bkpuser --password=123456 --prepare --apply-log-only --target-dir=/data/backups/base
xtrabackup --user=bkpuser --password=123456 --prepare --apply-log-only --target-dir=/data/backups/base --incremental-dir=/data/backups/inc1
xtrabackup --user=bkpuser --password=123456 --prepare --target-dir=/data/backups/base --incremental-dir=/data/backups/inc2
xtrabackup --apply-log-only 合并除最后一个以外的所有增量时应使用, 一旦准备好,增量备份就与完整备份相同,可以用相同的方式还原它们。
xtrabackup --copy-back --target-dir=/data/backups/base
|