一、XtraBackup介绍
1、备份工具
xtrabackup:是一款基于InnoDB的在线热备工具,具有开源,免费,支持在线热备,占用磁盘空间小,能够非常快速地备份与恢复mysql数据库,(备份时不影响数据读写)
官方下载地址为http://www.percona.com/software/percona-xtrabackup
mysqldump:备份是逻辑备份,备份出来的文件是sql语句,但是当备份MySQL数据超过10G时,用mysqldump来导出备份就比较慢了
2、备份原理
3、xtrabackup包含两个主要的工具
①xtrabackup只能备份innodb和xtradb两种引擎的表,而不能备份myisam引擎的表;
②innobackupex是一个封装了xtrabackup的Perl脚本,支持同时备份innodb和myisam,但在对myisam备份时需要加一个全局的读锁,还有就是myisam不支持增量备份
innobackupex备份和恢复的工作原理
二、XtraBackup安装部署
1、环境
①db
[root@db ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@db ~]# uname -r 2.6.32-696.el6.x86_64 [root@db ~]# /etc/init.d/iptables status iptables: Firewall is not running. [root@db ~]# getenforce Disabled [root@db ~]# hostname -I 172.19.5.54 172.16.1.54
②db01
[root@db01 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@db01 ~]# uname -r 2.6.32-696.el6.x86_64 [root@db01 ~]# /etc/init.d/iptables status iptables: Firewall is not running. [root@db01 ~]# getenforce Disabled [root@db01 ~]# hostname -I 172.19.5.55 172.16.1.55
2、安装XtraBackup
cd /server/tools/ wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.9-1.el6.x86_64.rpm yum localinstall percona-xtrabackup-24-2.4.9-1.el6.x86_64.rpm -y
3、db服务器模拟插入数据
create database oldboy;#创建oldboy库 use oldboy; create table test( id int(4) not null,name char(20) not null);#在oldboy库中创建test表 insert into test values(1,"oldboy");#test表中插入数据 insert into test values(2,"oldgirl"); insert into test values(3,"inca"); insert into test values(4,"zuma"); insert into test values(5,"kaka"); insert into test values(6,"老男孩");
查看test表中内容
mysql> select * from test; +----+-----------+ | id | name | +----+-----------+ | 1 | oldboy | | 2 | oldgirl | | 3 | inca | | 4 | zuma | | 5 | kaka | | 6 | 老男孩 | +----+-----------+ 6 rows in set (0.00 sec)
4、备份数据
①db操作
mkdir /backup -p && cd /backup innobackupex --defaults-file="/etc/my.cnf" --user=root --password=‘123456‘ --socket=/application/mysql/tmp/mysql.sock /backup/ #备份完成会出现如下内容 …… xtrabackup: Transaction log of lsn (1642766) to (1642766) was copied. 180109 11:40:30 completed OK!
②查看备份的内容
[root@db backup]# ll /backup/2018-01-09_11-57-49/ total 131104 -rw-r----- 1 root root 418 2018-01-09 11:57 backup-my.cnf -rw-r----- 1 root root 12582912 2018-01-09 12:01 ibdata1 -rw-r----- 1 root root 50331648 2018-01-09 12:01 ib_logfile0 -rw-r----- 1 root root 50331648 2018-01-09 12:01 ib_logfile1 -rw-r----- 1 root root 12582912 2018-01-09 12:01 ibtmp1 drwxr-x--- 2 root root 4096 2018-01-09 11:57 mysql drwxr-x--- 2 root root 4096 2018-01-09 11:57 oldboy drwxr-x--- 2 root root 4096 2018-01-09 11:57 performance_schema -rw-r----- 1 root root 22 2018-01-09 11:57 xtrabackup_binlog_info -rw-r--r-- 1 root root 22 2018-01-09 12:01 xtrabackup_binlog_pos_innodb -rw-r----- 1 root root 113 2018-01-09 12:01 xtrabackup_checkpoints -rw-r----- 1 root root 536 2018-01-09 11:57 xtrabackup_info -rw-r----- 1 root root 8388608 2018-01-09 12:01 xtrabackup_logfile
③预处理数据
innobackupex --defaults-file="/etc/my.cnf" --user=root --password=‘123456‘ --socket=/application/mysql/tmp/mysql.sock --apply-log --use-memory=1G /backup/2018-01-09_11-57-49/
④模拟删除oldboy库,恢复全备
drop database oldboy;
⑤为了保持数据一致性,停库恢复
[root@db ~]# /etc/init.d/mysqld stop Shutting down MySQL.. SUCCESS! rm -fr /application/mysql/data/* innobackupex --defaults-file="/etc/my.cnf" --user=root --password=‘123456‘ --socket=/application/mysql/tmp/mysql.sock --move-back /backup/2018-01-09_11-57-49/
⑥恢复全备成功后,重新授权数据目录,启动MySQL
chown -R mysql.mysql /application/mysql/data/ [root@db mysql]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS! #登录数据库查看恢复的数据 mysql> select * from oldboy.test; +----+-----------+ | id | name | +----+-----------+ | 1 | oldboy | | 2 | oldgirl | | 3 | inca | | 4 | zuma | | 5 | kaka | | 6 | 老男孩 | +----+-----------+ 6 rows in set (0.00 sec)
三、主从复制部署
②从库预处理
授权从库连接用户
grant replication slave on *.* to ‘rep‘@‘172.19.5.%‘ identified by ‘123456‘;
innobackupex --defaults-file="/etc/my.cnf" --user=root --password=‘123456‘ --socket=/application/mysql/tmp/mysql.sock --apply-log --use-memory=1G /backup/2018-01-09_10-33-44/
③关闭从库,复制数据