xtrabackup-2.4.8的安装及使用
Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具。特点:
- (1)备份过程快速、物理备份可靠;
- (2)备份过程不会打断正在执行的事务(无需锁表);
- (3)能够基于压缩等功能节约磁盘空间和流量;
- (4)自动实现备份检验;
- (5)还原速度快;
- (6)可以流传将备份传输到另外一台机器上;
xtrabackup包含两个主要的工具,即xtrabackup和innobackupex,二者区别如下:
Xtrabackup:只能备份innodb和xtradb两种引擎的表,而不能备份myisam引擎的表;
Innobackup:是一个封装了xtrabackup的Perl脚本,支持同时备份innodb和myisam,但在对myisam备份时需要加一个全局的读锁。还有就是myisam不支持增量备份;
—Yum安装
官网地址:https://www.percona.com/doc/percona-xtrabackup/LATEST/installation/yum_repo.html
[[email protected] ~]# yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
[[email protected] ~]# yum list | grep percona
[[email protected] ~]# yum install percona-xtrabackup-24 -y # 卸载命令:yum remove percona-xtrabackup
[[email protected] ~]# xtrabackup -version
xtrabackup version 2.4.8 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 97330f7)
—使用二进制编译好的通用安装包
下载地址:https://www.percona.com/downloads/XtraBackup/LATEST/binary/redhat/6/x86_64/ #自行选择版本
下载地址:https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.8/binary/tarball/percona-xtrabackup-2.4.8-Linux-x86_64.tar.gz #本次使用版本
[[email protected] ~]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.8/binary/tarball/percona-xtrabackup-2.4.8-Linux-x86_64.tar.gz
[[email protected] ~]# tar xf percona-xtrabackup-2.4.8-Linux-x86_64.tar.gz -C /usr/local/
[[email protected] ~]# mv /usr/local/percona-xtrabackup-2.4.8-Linux-x86_64/ /usr/local/xtrabackup
[[email protected] local]# echo “export PATH=$PATH:/usr/local/xtrabackup/bin” >> /etc/profile
[[email protected] local]# source /etc/profile
[[email protected] ~]# yum install numactl -y #安装依赖
—-全量备份
备份数据存放在/data/backup/下面,innobackupex会自动创建一个文件夹+当前系统的时间戳
创建测试数据库,表
mysql> create database test;
mysql> use test;
mysql> create table t1;
mysql> create table t1(id int auto_increment primary key);
mysql> insert into t1 values(null);
mysql> insert into t1 values(null);
mysql> insert into t1 values(null);
[[email protected] ~]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=123456 –socket=/data/mysqldata/mysqld.sock /data/backup/full
#这里测试直接使用root权限附上创建用户及权限命令
mysql> create user ‘backup’@’localhost’ identified by ‘123456’;
mysql> grant reload,lock tables,replication client,show view,event,process on *.* to [email protected];
mysql> plush privileges;
#备份后的文件:在备份的同时,备份数据会在备份目录下创建一个以当前日期时间为名字的目录存放备份文件。
各文件说明:
(1) backup-my.cnf —— 备份命令用到的配置选项信息;
(2) ibdata1 —— 备份的表空间文件;
(3) xtrabackup_binary —— 备份中用到的xtrabackup的可执行文件;
(4) xtrabackup_binlog_info —— mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置;
(5) xtrabackup_checkpoints —— 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
6) xtrabackup_logfile —— 备份的重做日志文件。
可以看见相关文件记录了LSN,日志偏移量,还可以看见这次是全备份
删除数据库,然后恢复全备(线上不要这样搞)
mysql> drop database test;
恢复全备
恢复备份到mysql的数据文件目录,这一过程要先关闭mysql数据库,重命名或者删除原数据文件目录都可以,再创建一个新的数据文件目录,将备份数据复制到新的数据文件目录下,赋权,修改权限,启动数据库
[[email protected] ~]# service mysqld stop
[[email protected] ~]# mv /data/mysqldata/ /tmp/mysqldata/
[[email protected] ~]# mkdir /data/mysqldata
[[email protected] ~]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=123456 –use-memory=4G –apply-log /data/2017-08-05_11-44-06/ #第1步是apply-log,为了加快速度,一般建议设置–use-memory,这个步骤完成之后,目录就准备就绪
170805 16:49:32 completed OK! #成功
[[email protected] ~]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=123456 –copy-back /data/2017-08-05_11-33-06/ #第2步是copy-back,即把备份文件拷贝至原数据目录下。注意:innobackupex 增量备份仅针对InnoDB这类支持事务的引擎,对于MyISAM等引擎,则仍然是全备
170805 16:49:32 completed OK! #成功
可以看见已经成功恢复,修改数据目录权限,启动mysql,效验数据是否正常,查看yayun库下面的t1表中的数据。
[[email protected] ~]# chown -R mysql.mysql /data/
[[email protected] ~]# service mysqld start
数据已经成功恢复
增量备份
在进行增量备份时,首先要进行一次全量备份,第一次增量备份是基于全备的,之后的增量备份是基于上一次的增量备份,以此类推。
全备份放在/data/backup/full,增量备份放在/data/backup/incremental
先来全量备份
[[email protected] ~]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=123456 –socket=/data/mysqldata/mysqld.sock /data/backup/full
为了测试效果,我们在t1表中插入数据
mysql> insert into t1 values(null);
再来增量备份
[[email protected] ~]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=123456 –socket=/data/mysqldata/mysqld.sock –incremental /data/backup/incremental/ –incremental-basedir=/data/backup/full/2017-08-05_11-59-39/ –parallel=2
我们看看增量备份的大小以及文件内容
在test种创建t2,并插入数据然后创建增量备份2
mysql> use test;
mysql> create table t2(name varchar(20));
mysql> insert into t2 values(‘will’);
mysql> insert into t2 values(‘tom’);
mysql> insert into t2 values(‘jim’);
创建增量备份2(这次是基于上次的增量备份)
[[email protected] ~]# innobackupex –defaults-file=/etc/my.cnf –user=root –password=123456 –socket=/data/mysqldata/mysqld.sock –incremental /data/backup/incremental/ –incremental-basedir=/data/backup/incremental/ 2017-08-05_12-13-27/ –parallel=2
增量备份恢复
增量备份的恢复大体为3个步骤
*恢复完全备份
*恢复增量备份到完全备份(开始恢复的增量备份要添加–redo-only参数,到最后一次增量备份去掉–redo-only参数)
*对整体的完全备份进行恢复,回滚那些未提交的数据
恢复完全备份(注意这里一定要加–redo-only参数,该参数的意思是只应用xtrabackup日志中已提交的事务数据,不回滚还未提交的数据)
[[email protected] ~]# innobackupex –apply-log –redo-only /data/backup/full/2017-08-05_11-59-39
将增量备份1应用到完全备份
[[email protected] ~]# innobackupex –apply-log –redo-only /data/backup/full/2017-08-05_11-59-39 –incremental-dir=/data/backup/incremental/2017-08-05_12-04-59
将增量备份2应用到完全备份(注意恢复最后一个增量备份时需要去掉–redo-only参数,回滚xtrabackup日志中那些还未提交的数据)
[[email protected] ~]# innobackupex –apply-log –redo-only /data/backup/full/2017-08-05_11-59-39 –incremental-dir=/data/backup/incremental/2017-08-05_12-13-27
把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据:
[[email protected] ~]# innobackupex –apply-log /data/backup/full/2014-04-07_23-37-20/
把恢复完的备份复制到数据库目录文件中,赋权,然后启动mysql数据库,检测数据正确性
[[email protected] ~]# service mysqld start
[[email protected] ~]# mv /data/mysqldata/ /tmp/mysqldata/
[[email protected] ~]# mkdir /data/mysqldata
[[email protected] ~]# innobackupex –defaults-file=/etc/my.cnf –copy-back –rsync /data/backup/full/2017-08-05_11-59-39/
[[email protected] ~]# chown -R mysql.mysql /data/
克隆slave
在日常工作中,我们有时候需要在线添加从库,比如线上有一主一从两个数据库,但是由于业务的需要,一台从库的读取无法满足现在的需求,这样就需要我们在线添加从库,由于出于安全考虑,我们通常需要在从库上进行在线克隆slave。
克隆slave时,常用参数–slave-info和–safe-slave-backup。
–slave-info会将master的binlog文件名和偏移量位置保存到xtrabackup_slave_info文件中
–safe-slave-backup会暂停slave的SQL线程直到没有打开的临时表的时候开始备份。备份结束后SQL线程会自动启动,这样操作的目的主要是确保一致性的复制状态。
下面的例子,将介绍一主一从情况下在线搭建新的从库,环境如下:
master 192.168.0.10 #主库
slave 192.168.0.20 #从库
newslave 192.168.0.100 # 新的从库
在上述示例中,newslave即为要新搭建的从库。在老的从库上面进行备份:
[[email protected] ~]# innobackupex –user=root –password=12345 –socket=/tmp/mysqld.sock –defaults-file=/etc/my.cnf –slave-info –safe-slave-backup –no-timestamp /data/cloneslave
innobackupex: Backup created in directory ‘/data/cloneslave’
innobackupex: MySQL binlog position: filename ‘mysql-bin.000022’, position 107
innobackupex: MySQL slave binlog position: master host ‘192.168.0.10’, filename ‘mysql-bin.000006’, position 732
140413 23:25:13 innobackupex: completed OK!
这里的/data/cloneslave 目录要不存在,如果存在是会报错的。
查看目录下生成的文件:
[[email protected] ~]# ll /data/cloneslave/
total 26668
-rw-r–r– 1 root root 261 Apr 13 23:24 backup-my.cnf
-rw-r–r– 1 root root 27262976 Apr 13 23:24 ibdata1
drwxr-xr-x 2 root root 4096 Apr 13 23:25 mysql
drwxr-xr-x 2 root root 4096 Apr 13 23:25 performance_schema
drwxr-xr-x 2 root root 4096 Apr 13 23:25 sakila
drwxr-xr-x 2 root root 4096 Apr 13 23:25 world_innodb
-rw-r–r– 1 root root 13 Apr 13 23:25 xtrabackup_binary
-rw-r–r– 1 root root 23 Apr 13 23:25 xtrabackup_binlog_info
-rw-r–r– 1 root root 79 Apr 13 23:25 xtrabackup_checkpoints
-rw-r–r– 1 root root 2560 Apr 13 23:25 xtrabackup_logfile
-rw-r–r– 1 root root 72 Apr 13 23:25 xtrabackup_slave_info
drwxr-xr-x 2 root root 4096 Apr 13 23:25 yayun
[[email protected] ~]#
查看xtrabackup_slave_info文件内容,这个内容就是为搭建从库时需要change master to的参数:
[[email protected] ~]# cat /data/cloneslave/xtrabackup_slave_info
CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000006′, MASTER_LOG_POS=732
[[email protected] ~]#
在老的slave服务器上进行还原,即192.168.0.20
[[email protected] ~]# innobackupex –apply-log –redo-only /data/cloneslave/
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
140413 23:30:37 InnoDB: Starting shutdown…
140413 23:30:37 InnoDB: Shutdown completed; log sequence number 12981048
140413 23:30:37 innobackupex: completed OK!
[[email protected] ~]#
将还原的文件复制到新的从库newslave,即192.168.0.100
[[email protected] data]# rsync -avprP -e ssh /data/cloneslave/ 192.168.0.100:/data/mysql/
在主库master上添加对新从库newslave的授权:
mysql> grant replication slave on *.* to ‘repl’@’192.168.0.100’ identified by ‘123456’;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
mysql>
拷贝老的从库的配置文件到新的从库newslave,并且修改server-id参数,修改完毕后,启动新的从库;
[[email protected] data]# scp /etc/my.cnf 192.168.0.100:/etc/
[email protected]‘s password:
my.cnf 100% 4881 4.8KB/s 00:00
[[email protected] data]#
[[email protected] mysql]# egrep ‘log-slave|^server-id|skip_slave’ /etc/my.cnf
server-id = 3
skip_slave_start
log-slave-updates=1
[[email protected] mysql]#
[[email protected] mysql]# chown -R mysql.mysql .
[[email protected] mysql]# /etc/init.d/mysqld restart
Shutting down MySQL. [ OK ]
Starting MySQL.. [ OK ]
[[email protected] mysql]#
查找老的从库备份后生成的xtrabackup_slave_info文件,提取其中的master_log_file和master_log_pos信息,然后在新的从库上进行change master to操作:
在新的从库上进行同步:
mysql> CHANGE MASTER TO MASTER_HOST=’192.168.0.10′,MASTER_USER=’repl’, MASTER_PASSWORD=’123456′,MASTER_LOG_FILE=’mysql-bin.000006′, MASTER_LOG_POS=732;
Query OK, 0 rows affected (0.09 sec)
mysql>
启动io线程和sql线程,并观察复制是否正常:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 2
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 1309
Relay_Log_File: MySQL-02-relay-bin.000002
Relay_Log_Pos: 830
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: yayun.%
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1309
Relay_Log_Space: 989
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
mysql>
查看主库,发现已经有两个线程(Binlog Dump)
mysql> show processlist/G
*************************** 1. row ***************************
Id: 8
User: slave
Host: 192.168.0.20:44251
db: NULL
Command: Binlog Dump
Time: 1088
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 2. row ***************************
Id: 9
User: root
Host: localhost
db: yayun
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 3. row ***************************
Id: 10
User: repl
Host: 192.168.0.100:45844
db: NULL
Command: Binlog Dump
Time: 124
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
3 rows in set (0.00 sec)
mysql>
正常工作,到此在线克隆slave就结束啦。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/4744.html