简介Xtrabackup
Xtrabackup是由percona开源的免费数据库热备份软件,官方文档:https://www.percona.com/doc/percona-xtrabackup/2.4/index.html
它能对InnoDB数据库和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加表锁);
Xtrabackup优点
备份速度快,物理备份可靠
备份过程不会打断正在执行的事务(无需锁表)
能够基于压缩等功能节约磁盘空间和流量
自动备份校验
还原速度快
在xtrabackup的上一次整库备份基础上做增量备份(innodb only)
以流的形式产生备份,可以直接保存到远程机器上(本机硬盘空间不足时很有用)
Xtrabackup备份原理
备份开始时首先会开启一个后台检测进程,实时检测mysql redo的变化,一旦发现有新的日志写入,立刻将日志记入后台日志文件xtrabackup_log中,之后复制innodb的数据文件,复制结束后,将执行flush tables with readlock,然后复制.frm MYI MYD等文件,最后执行unlock tables,最终停止xtrabackup_log。
步骤:
开启redo日志拷贝线程,从最新的检查点开始顺序拷贝redo日志;
开启ibd文件拷贝线程,拷贝innodb表的数据
ibd文件拷贝结束,通知调用FTWRL(flush tables with read lock),获取一致性位点
备份非innodb表(系统表)和frm文件 由于此时没有新事务提交,等待redo日志拷贝完成
最新的redo日志拷贝完成后,相当于此时的innodb表和非innodb表数据都是最新的
获取binlog位点,输出到STDERR,此时数据库的状态是一致的。
释放锁,备份结束。
Xtrabackup增量备份介绍
对于增量备份只对InnoDB,MyISAM和其它引擎仍然是完整备份的方式,增量备份主要是处理InnoDB中有变更的页(页的LSN).LSN信息在xtrabackup_checkpoints中。 xtrabackup增量备份的原理是:
1)、首先完成一个完全备份,并记录下此时检查点LSN;
2)、然后增量备份时,比较表空间中每个页的LSN是否大于上次备份的LSN,若是则备份该页并记录当前检查点的LSN。
增量备份优点:
1)、数据库太大没有足够的空间全量备份,增量备份能有效节省空间,并且效率高;
2)、支持热备份,备份过程不锁表(针对InnoDB而言),不阻塞数据库的读写;
3)、每日备份只产生少量数据,也可采用远程备份,节省本地空间;
4)、备份恢复基于文件操作,降低直接对数据库操作风险;
5)、备份效率更高,恢复效率更高。
XtraBackup的工具
安装XtraBackup后,其实会有几个工具:
innobackupex:实际是xtrabackup的一个链接(```ll `which innobackupex````),以后的版本会废弃。
xtrabackup:一个由C编译而来的二进制文件,可以备份MyISAM, InnoDB,XtraDB表,并不是像有些人说的只能备份InnoDB和XtraDB数据。
xbcrypt:用来加密或解密备份的数据。
xbstream:用来解压或压缩xbstream格式的压缩文件。
建议使用perl封装的innobackupex来作数据库备份,因为比较容易使用。
安装Percona XtraBackup 2.4
对于Percona XtraBackup 2.4 版本支持的引擎,官方文档的说明:
It can back up data from InnoDB, XtraDB, and MyISAM tables on MySQL
5.1 [1], 5.5, 5.6 and 5.7 servers, as well as Percona Server with XtraDB.
注意:Percona XtraBackup 8.0 不支持MySQL 8.0之前的版本
下载源安装包
# wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb --2019-12-30 16:37:03-- https://repo.percona.com/apt/percona-release_latest.bionic_all.deb Resolving repo.percona.com (repo.percona.com)... 167.99.233.229 Connecting to repo.percona.com (repo.percona.com)|167.99.233.229|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 9364 (9.1K) [application/octet-stream] Saving to: ‘percona-release_latest.bionic_all.deb’ percona-release_latest.bionic_all.deb 100%[=====================================================================================================================>] 9.14K --.-KB/s in 0s 2019-12-30 16:37:04 (27.5 MB/s) - ‘percona-release_latest.bionic_all.deb’ saved [9364/9364]
安装源安装包
# sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb Selecting previously unselected package percona-release. (Reading database ... 166326 files and directories currently installed.) Preparing to unpack percona-release_latest.bionic_all.deb ... Unpacking percona-release (1.0-13.generic) ... Setting up percona-release (1.0-13.generic) ... * Enabling the Percona Original repository <*> All done! ==> Please run "apt-get update" to apply changes The percona-release package now contains a percona-release script that can enable additional repositories for our newer products. For example, to enable the Percona Server 8.0 repository use: percona-release setup ps80 Note: To avoid conflicts with older product versions, the percona-release setup command may disable our original repository for some products. For more information, please visit: https://www.percona.com/doc/percona-repo-config/percona-release.html
查询新增的安装源
# head /etc/apt/sources.list.d/percona-original-release.list # # This repo is managed by "percona-release" utility, do not edit! # deb http://repo.percona.com/percona/apt bionic main deb-src http://repo.percona.com/percona/apt bionic main
更新本地缓存
# sudo apt-get update ......
安装percona-xtrabackup-24
$ sudo apt-get install percona-xtrabackup-24
全量备份
执行全量备份
root@scutech:/home/scutech# innobackupex -uroot -pdingjia /home/scutech/xtra xtrabackup: recognized server arguments: --datadir=/var/lib/mysql --server-id=135 --log_bin=master-binlog xtrabackup: recognized client arguments: 191230 17:43:31 innobackupex: Starting the backup operation ...... 191230 17:43:38 Executing UNLOCK TABLES 191230 17:43:38 All tables unlocked 191230 17:43:38 [00] Copying ib_buffer_pool to /home/scutech/xtra/2019-12-30_17-43-31/ib_buffer_pool 191230 17:43:38 [00] ...done 191230 17:43:38 Backup created in directory '/home/scutech/xtra/2019-12-30_17-43-31/' MySQL binlog position: filename 'master-binlog.000001', position '154', GTID of the last change '' 191230 17:43:38 [00] Writing /home/scutech/xtra/2019-12-30_17-43-31/backup-my.cnf 191230 17:43:38 [00] ...done 191230 17:43:38 [00] Writing /home/scutech/xtra/2019-12-30_17-43-31/xtrabackup_info 191230 17:43:38 [00] ...done xtrabackup: Transaction log of lsn (2661749) to (2661758) was copied. 191230 17:43:38 completed OK!
请注意其中记录的备份结束时的binlog的位置。
查看general log中备份期间执行的命令
/var/lib/mysql# tail -f scutech.log 2019-12-30T09:43:36.615291Z 20 Query FLUSH TABLES WITH READ LOCK ...... 2019-12-30T09:43:38.388002Z 20 Query UNLOCK TABLES ......
注意看这里有 “FLUSH TABLES WITH READ LOCK”,这个锁如果时间长对性能影响很大。
到备份的目录查看文件信息:
root@:/home/scutech/xtra# ll total 12 drwxr-xr-x 3 root root 4096 Dec 30 17:43 ./ drwxr-xr-x 16 scutech scutech 4096 Dec 30 17:41 ../ drwxr-x--- 7 root root 4096 Dec 30 17:43 2019-12-30_17-43-31/ root@:/home/scutech/xtra# ll 2019-12-30_17-43-31/ total 12348 drwxr-x--- 7 root root 4096 Dec 30 17:43 ./ drwxr-xr-x 3 root root 4096 Dec 30 17:43 ../ -rw-r----- 1 root root 489 Dec 30 17:43 backup-my.cnf -rw-r----- 1 root root 293 Dec 30 17:43 ib_buffer_pool -rw-r----- 1 root root 12582912 Dec 30 17:43 ibdata1 drwxr-x--- 2 root root 4096 Dec 30 17:43 mysql/ drwxr-x--- 2 root root 4096 Dec 30 17:43 performance_schema/ drwxr-x--- 2 root root 4096 Dec 30 17:43 scott/ drwxr-x--- 2 root root 12288 Dec 30 17:43 sys/ drwxr-x--- 2 root root 4096 Dec 30 17:43 test/ -rw-r----- 1 root root 26 Dec 30 17:43 xtrabackup_binlog_info -rw-r----- 1 root root 135 Dec 30 17:43 xtrabackup_checkpoints -rw-r----- 1 root root 498 Dec 30 17:43 xtrabackup_info -rw-r----- 1 root root 2560 Dec 30 17:43 xtrabackup_logfile
全量恢复
prepare 阶段
–prepare:表示还原,即恢复
–apply-log-only:表示不回滚事务,如果后面有基于全备的增量备份,就不需要回滚,如果没有则去掉该参数
# xtrabackup --prepare --target-dir=/home/scutech/xtra/2019-12-30_17-43-31 xtrabackup: recognized server arguments: --innodb_checksum_algorithm=crc32 --innodb_log_checksum_algorithm=strict_crc32 --innodb_data_file_path=ibdata1:12M:autoextend --innodb_log_files_in_group=2 --innodb_log_file_size=50331648 --innodb_fast_checksum=0 --innodb_page_size=16384 --innodb_log_block_size=512 --innodb_undo_directory=./ --innodb_undo_tablespaces=0 --server-id=135 --redo-log-version=1 xtrabackup: recognized client arguments: --prepare=1 --target-dir=/home/scutech/xtra/2019-12-30_17-43-31 xtrabackup version 2.4.18 based on MySQL server 5.7.26 Linux (x86_64) (revision id: 29b4ca5) xtrabackup: cd to /home/scutech/xtra/2019-12-30_17-43-31/ xtrabackup: This target seems to be not prepared yet. ......
恢复阶段
停止mysql服务进程
# service mysql stop
使用xtrabackup恢复
root@scutech:~# xtrabackup --copy-back --target-dir=/home/scutech/xtra/2019-12-30_17-43-31/ xtrabackup: recognized server arguments: --datadir=/var/lib/mysql --server-id=135 --log_bin=master-binlog xtrabackup: recognized client arguments: --copy-back=1 --target-dir=/home/scutech/xtra/2019-12-30_17-43-31/ xtrabackup version 2.4.18 based on MySQL server 5.7.26 Linux (x86_64) (revision id: 29b4ca5) Original data directory /var/lib/mysql is not empty!
提示目录不为空,考虑到这个目录里面有很多文件,直接拷贝吧:
cp -rv /home/scutech/xtra/2019-12-30_17-43-31/* /var/lib/mysql
或者
rsync -avrP /home/scutech/xtra/2019-12-30_17-43-31/* /var/lib/mysql
修改属主
chown -R mysql. /var/lib/mysql
恢复完成,启动MySQL,一切正常。
增量备份
先做全量备份
# xtrabackup --backup --target-dir=/home/scutech/xtra xtrabackup: recognized server arguments: --datadir=/var/lib/mysql --server-id=135 --log_bin=master-binlog xtrabackup: recognized client arguments: --user=root --password=* --backup=1 --target-dir=/home/scutech/xtra
查看备份的lsn
# cat /home/scutech/xtra/xtrabackup_checkpoints backup_type = full-backuped from_lsn = 0 to_lsn = 2662716 last_lsn = 2662725 compact = 0 recover_binlog_info = 0 flushed_lsn = 2662725
再做增量备份
# xtrabackup --backup --target-dir=/home/scutech/xtra_inc1 --incremental-basedir=/home/scutech/xtra
在这个目录下( /home/scutech/xtra_inc1)下有一些.delta结尾的文件存放增量内容,查看备份的lsn。
# cat /home/scutech/xtra_inc1/xtrabackup_checkpoints backup_type = incremental from_lsn = 2662716 to_lsn = 2667408 last_lsn = 2667417 compact = 0 recover_binlog_info = 0 flushed_lsn = 2667417
增量恢复
Preparing阶段
对全量prepare,只前滚不回滚
# xtrabackup --prepare --apply-log-only --target-dir=/home/scutech/xtra
对最后一个增量的prepare,既前滚也后滚。
xtrabackup --prepare --target-dir=/home/scutech/xtra --incremental-dir=/home/scutech/xtra_inc1
恢复阶段
恢复的方式同全量恢复,注意目录是全量目录