xtarbackup介绍
xtrabackup是Percona公司CTO Vadim参与开发的一款基于InnoDB的在线热备工具
xtrabackup包含两个主要的工具,即xtrabackup和innobackupex,二者区别如下:
xtrabackup:是用于热备innodb,xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;
innobackupex:是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力.
常用选项:
--host 指定主机
--user 指定用户名
--password 指定密码
--port 指定端口
--databases 指定数据库
--incremental 创建增量备份
--incremental-basedir 指定包含完全备份的目录
--incremental-dir 指定包含增量备份的目录
--apply-log 对备份进行预处理操作
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
--redo-only 不回滚未提交事务
--copy-back 恢复备份目录
xtrabackup的官方下载地址为http://www.percona.com/software/percona-xtrabackup
优点:
(1)备份速度快,物理备份可靠
(2)备份过程不会打断正在执行的事务(无需锁表)
(3)能够基于压缩等功能节约磁盘空间和流量
(4)自动备份校验
(5)还原速度快
(6)可以流传将备份传输到另外一台机器上
(7)在不增加服务器负载的情况备份数据
[root@localhost ~]# rpm -qa |grep libgcrypt libgcrypt-1.8.3-4.el8.x86_64 如本地安装了libgcrypt183,就下载下方的包 wget https://www.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.14/binary/tarball/percona-xtrabackup-2.4.14-Linux-x86_64.libgcrypt183.tar.gz
解压缩并配置环境变量
[root@localhost ~]# tar -xf percona-xtrabackup-2.4.14-Linux-x86_64.libgcrypt183.tar.gz [root@localhost ~]# mv percona-xtrabackup-2.4.14-Linux-x86_64 /usr/local/xtrabackup [root@localhost ~]# echo "export PATH=$PATH:/usr/local/xtrabackup/bin" > /etc/profile.d/xtarbackup.sh [root@localhost ~]# source /etc/profile.d/xtarbackup.sh
检查安装是否成功
[root@localhost ~]# xtrabackup --version xtrabackup: recognized server arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin xtrabackup version 2.4.14 based on MySQL server 5.7.19 Linux (x86_64) (revision id: ef675d4)
进行全备份
备份数据存放在/data/backup/下面,innobackupex会自动创建一个文件夹,是当前系统的时间戳
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123456 --host=192.168.248.130 /opt/backup xtrabackup: recognized server arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin xtrabackup: recognized client arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin 201231 14:34:04 innobackupex: Starting the backup operation .................................................................................................. xtrabackup: Transaction log of lsn (2727446) to (2727455) was copied. 201231 14:34:19 completed OK! [root@localhost ~]# ll /opt/backup/2020-12-31_14-34-04/ total 12340 -rw-r-----. 1 root root 488 Dec 31 14:34 backup-my.cnf -rw-r-----. 1 root root 365 Dec 31 14:34 ib_buffer_pool -rw-r-----. 1 root root 12582912 Dec 31 14:34 ibdata1 drwxr-x---. 2 root root 4096 Dec 31 14:34 mysql drwxr-x---. 2 root root 8192 Dec 31 14:34 performance_schema drwxr-x---. 2 root root 8192 Dec 31 14:34 sys drwxr-x---. 2 root root 52 Dec 31 14:34 t1 -rw-r-----. 1 root root 21 Dec 31 14:34 xtrabackup_binlog_info -rw-r-----. 1 root root 135 Dec 31 14:34 xtrabackup_checkpoints -rw-r-----. 1 root root 522 Dec 31 14:34 xtrabackup_info -rw-r-----. 1 root root 2560 Dec 31 14:34 xtrabackup_logfile
测试删除数据库,然后恢复全备
mysql> drop database t1; Query OK, 1 row affected (0.01 sec)
恢复全备
恢复备份到mysql的数据文件目录,这一过程要先关闭mysql数据库,重命名或者删除原数据文件目录都可以,再创建一个新的数据文件目录,将备份数据复制到新的数据文件目录下,赋权,修改权限,启动数据库
#停止mysql服务 [root@localhost opt]# service mysqld stop Shutting down MySQL.. SUCCESS #合并数据 [root@localhost opt]# innobackupex --apply-log /opt/backup/2020-12-31_14-34-04/ #移走原数据目录 [root@localhost opt]# mv /opt/data /tmp/ #恢复数据 [root@localhost opt]# innobackupex --copy-back /opt/backup/2020-12-31_14-34-04/ ............................................ 201231 15:33:14 completed OK! [root@localhost opt]# ll /opt/ backup/ data/ [root@localhost opt]# chown -R mysql.mysql /opt/data/ #启动mysql查看数据是否恢复 [root@localhost opt]# service mysqld start Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'. .. SUCCESS! [root@localhost opt]# mysql -uroot -p123456 -e 'show databases;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | t1 | +--------------------+
增量备份
在进行增量备份时,首先要进行一次全量备份,第一次增量备份是基于全备的,之后的增量备份是基于上一次的增量备份,以此类推。
在test表中插入一些数据
mysql> select * from test; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | tom | 20 | | 2 | jerry | 21 | | 3 | xx | 22 | +----+-------+------+ mysql> insert test(name,age) values('xuaos',23),('xiaos',25); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from test; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | tom | 20 | | 2 | jerry | 21 | | 3 | xx | 22 | | 4 | xuaos | 23 | | 5 | xiaos | 25 | +----+-------+------+ 5 rows in set (0.00 sec)
增量备份1
root@localhost incremental]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123456 --host=192.168.248.130 --incremental /opt/incremental --incremental-basedir=/opt/backup/2020-12-31_14-34-04 ......................................................................... xtrabackup: Transaction log of lsn (2730794) to (2730803) was copied. 201231 15:54:53 completed OK! [root@localhost incremental]# du -sh /opt/backup/2020-12-31_14-34-04/ 142M /opt/backup/2020-12-31_14-34-04/ [root@localhost incremental]# du -sh /opt/incremental/2020-12-31_15-54-51/ 3.2M /opt/incremental/2020-12-31_15-54-51/ [root@localhost incremental]# cat /opt/incremental/2020-12-31_15-54-51/xtrabackup_checkpoints backup_type = incremental from_lsn = 2727446 to_lsn = 2730794 last_lsn = 2730803 compact = 0 recover_binlog_info = 0 flushed_lsn = 2730803
增量备份2 (基于上一次增量备份)
表中插入数据
Database changed mysql> insert test(name,age) values('adasd',23),('sdiiuf',45); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from test; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | tom | 20 | | 2 | jerry | 21 | | 3 | xx | 22 | | 4 | xuaos | 23 | | 5 | xiaos | 25 | | 6 | adasd | 23 | | 7 | sdiiuf | 45 | +----+--------+------+ 7 rows in set (0.00 sec)
增量备份(2)
[root@localhost incremental]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123456 --host=192.168.248.130 --incremental /opt/incremental/ --incremental-basedir=/opt/incremental/2020-12-31_15-54-51/ ............................................................................. xtrabackup: Transaction log of lsn (2733484) to (2733493) was copied. 201231 16:08:53 completed OK! drwxr-x---. 6 root root 259 Dec 31 16:08 2020-12-31_16-08-51 [root@localhost ~]# ll /opt/incremental/ total 0 drwxr-x---. 6 root root 259 Dec 31 15:54 2020-12-31_15-54-51 drwxr-x---. 6 root root 259 Dec 31 16:08 2020-12-31_16-08-51
增量备份的恢复
恢复完全备份(注意这里一定要加--redo-only参数,该参数的意思是只应用xtrabackup日志中已提交的事务数据,不回滚还未提交的数据)
root@localhost ~]# mysql -uroot -p123456 -e 'drop database t1;' [root@localhost ~]# mysql -uroot -p123456 -e 'show databases;;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ [root@localhost ~]# service mysqld stop Shutting down MySQL.. SUCCESS! #恢复完全备份(注意这里一定要加--redo-only参数,该参数的意思是只应用xtrabackup日志中已提交的事务数据,不回滚还未提交的数据) [root@localhost ~]# innobackupex --apply-log --redo-only /opt/backup/2020-12-31_14-34-04/ #将增量备份1应用到完全备份 [root@localhost ~]# innobackupex --apply-log --redo-only /opt/backup/2020-12-31_14-34-04/ --incremental-dir=/opt/incremental/2020-12-31_15-54-51/ .............................................. 201231 16:24:18 completed OK! #将增量备份2应用到完全备份(注意恢复最后一个增量备份时需要去掉--redo-only参数,回滚xtrabackup日志中那些还未提交的数据 [root@localhost ~]# innobackupex --apply-log /opt/backup/2020-12-31_14-34-04/ --incremental-dir=/opt/incremental/2020-12-31_16-08-51 ....................................................... 201231 16:27:17 completed OK! #恢复 [root@localhost ~]# innobackupex --copy-back /opt/backup/2020-12-31_14-34-04/ ..................................................... 201231 16:39:40 [01] ...done 201231 16:39:40 completed OK! [root@localhost ~]# chown -R mysql.mysql /opt/data/ [root@localhost ~]# service mysqld start Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'. .. SUCCESS! [root@localhost ~]# mysql -uroot -p123456 -e 'show databases;;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | t1 | +--------------------+ [root@localhost ~]# mysql -uroot -p123456 -e 'select * from t1.test;' mysql: [Warning] Using a password on the command line interface can be insecure. +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | tom | 20 | | 2 | jerry | 21 | | 3 | xx | 22 | | 4 | xuaos | 23 | | 5 | xiaos | 25 | | 6 | adasd | 23 | | 7 | sdiiuf | 45 | +----+--------+------+
报错Failed to connect to MySQL server as DBD::mysql module is not installed at
解决:yum -y install perl-DBD-MySQL