Percona XtraBackup
一、Xtrabackup-2.4.11的安装及使用
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不支持增量备份;
二、二进制包安装(推荐安装方式,不用安装依赖包,非常方便):
1、下载安二进制包:
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.11/binary/tarball/percona-xtrabackup-2.4.11-Linux-x86_64.libgcrypt145.tar.gz
2、解压并创建软连接:
# tar zxvf percona-xtrabackup-2.4.11-Linux-x86_64.libgcrypt145.tar.gz
# mv percona-xtrabackup-2.4.11-Linux-x86_64 /usr/local/xtrabackup
# ln -s /usr/local/xtrabackup/bin/* /usr/bin/
3、查看版本:
[root@service2 ~]# xtrabackup --version
xtrabackup version 2.4.11 based on MySQL server 5.7.19 Linux (x86_64) (revision id: b4e0db5)
三、全量备份
1. 备份数据存放在/root/test下面,innobackupex会自动创建一个文件夹+当前系统的时间戳(本次实验形成:/root/test/2018-06-13_14-05-05)
创建测试数据库,表
mysql> create database test;
mysql> use test;
Database changed
mysql> create table t1(id int auto_increment primary key);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t1 values(null);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t1 values(null);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values(null);
Query OK, 1 row affected (0.01 sec)
[root@service2 ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Qaz123$%^ --socket=/var/lib/mysql/mysql.sock /root/test
2. 这里测试直接使用root权限附上创建用户及权限命令(按理说,应该用测试用户进行,但是我全过程用的root用户)
mysql> create user 'backup'@'%' identified by 'Qaz123$%^';
mysql> grant reload,lock tables,replication client,show view,event,process on *.* to 'backup'@'%';
mysql> flush privileges;
mysql> drop database test;
Tips:备份后的文件:在备份的同时,备份数据会在备份目录下创建一个以当前日期时间为名字的目录存放备份文件。
各文件说明:
(1) backup-my.cnf —— 备份命令用到的配置选项信息;
(2) ibdata1 —— 备份的表空间文件;
(3) xtrabackup_binary —— 备份中用到的xtrabackup的可执行文件;
(4) xtrabackup_binlog_info —— mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置;
(5) xtrabackup_checkpoints —— 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
(6) xtrabackup_logfile —— 备份的重做日志文件。
[root@service2 ~]# cd /root/test/2018-06-13_14-05-05
[root@service2 2018-06-13_14-05-05]# cat xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 2546853
last_lsn = 2546862
compact = 0
recover_binlog_info = 0
可以看见相关文件记录了LSN,日志偏移量,还可以看见这次是全备份
3. 删除数据库,然后恢复全备(线上不要这样搞)
mysql> drop database test;
恢复全备
恢复备份到mysql的数据文件目录,这一过程要先关闭mysql数据库,重命名或者删除原数据文件目录都可以,再创建一个新的数据文件目录,将备份数据复制到新的数据文件目录下,赋权,修改权限,启动数据库
[root@service2 ~]# service mysqld stop
[root@service2 ~]# mv /var/lib/mysql /root/testdata
[root@service2 ~]# mkdir /var/lib/mysql
[root@service2 ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Qaz123$%^ --use-memory=1G --apply-log /root/test/2018-06-13_14-05-05/
#第1步是apply-log,为了加快速度,一般建议设置--use-memory,这个步骤完成之后,目录就准备就绪
[root@service2 ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Qaz123$%^ --copy-back /root/test/2018-06-13_14-05-05/
#第2步是copy-back,即把备份文件拷贝至原数据目录下。注意:innobackupex 增量备份仅针对InnoDB这类支持事务的引擎,对于MyISAM等引擎,则仍然是全备
可以看见已经成功恢复,修改数据目录权限,启动mysql,效验数据是否正常,查看test库下面的t1表中的数据。
[root@service2 ~]# chown -R mysql.mysql /var/lib/mysql 必须是mysql用户,mysql组
[root@service2 ~]# service mysqld start
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)
四、增量备份
在进行增量备份时,首先要进行一次全量备份,第一次增量备份是基于全备的,之后的增量备份是基于上一次的增量备份,以此类推。
全备份放在/root/test1/,增量备份放在/root//incremental
先来全量备份
[root@service2 2018-06-13_14-05-05]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Qaz123$%^ --socket=/var/lib/mysql/mysql.sock /root/test1
180613 15:25:22 innobackupex: Starting the backup operation
IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
180613 15:25:22 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;mysql_socket=/var/lib/mysql/mysql.sock' as 'root' (using passw
ord: YES).Failed to connect to MySQL server as DBD::mysql module is not installed at - line 1327.
180613 15:25:22 Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: /var/lib/mysql/mysql.sock
Using server version 5.7.19
innobackupex version 2.4.11 based on MySQL server 5.7.19 Linux (x86_64) (revision id: b4e0db5)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /var/lib/mysql
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
InnoDB: Number of pools: 1
180613 15:25:22 >> log scanned up to (2547268)
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID 21 for sys/sys_config, old maximum was 0
180613 15:25:22 [01] Copying ./ibdata1 to /root/test1/2018-06-13_15-25-22/ibdata1
180613 15:25:22 [01] ...done
.......................................................
180613 15:25:26 [01] ...done
180613 15:25:26 Finished backing up non-InnoDB tables and files
180613 15:25:26 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '2547259'
xtrabackup: Stopping log copying thread.
.180613 15:25:26 >> log scanned up to (2547268)
180613 15:25:26 Executing UNLOCK TABLES
180613 15:25:26 All tables unlocked
...............................................................
180613 15:25:26 [00] ...done
180613 15:25:26 [00] Writing /root/test1/2018-06-13_15-25-22/xtrabackup_info
180613 15:25:26 [00] ...done
xtrabackup: Transaction log of lsn (2547259) to (2547268) was copied.
180613 15:25:26 completed OK!
为了测试效果,我们在t1表中插入数据
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)
插入数据
mysql> insert into t1 values(null);
Query OK, 1 row affected (0.02 sec)
mysql> insert into t1 values(null);
Query OK, 1 row affected (0.05 sec)
mysql> insert into t1 values(null);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
6 rows in set (0.00 sec)
再来增量备份
[root@service2 ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Qaz123$%^ --socket=/var/lib/mysql/mysql.sock --incremental /root/incremental/ --incremental-basedir=/root/test1/2018-06-13_15-25-22/ --parallel=2
我们看看增量备份的大小以及文件内容
[root@service2 ~]# cd /root/incremental/2018-06-13_15-33-55
[root@service2 2018-06-13_15-33-55]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2547259
to_lsn = 2550561
last_lsn = 2550570
compact = 0
recover_binlog_info = 0
在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');
mysql> select * from t2;
+------+
| name |
+------+
| will |
| tom |
| jim |
+------+
3 rows in set (0.00 sec)
创建增量备份2(这次是基于上次的增量备份)
innobackupex --defaults-file=/etc/my.cnf --user=root --password=Qaz123$%^ --socket=/var/lib/mysql/mysql.sock --incremental /root/incremental/ --incremental-basedir=/root/incremental/2018-06-13_15-33-55/ --parallel=2
--incremental-basedir这次基于上次增量备份哦
增量备份恢复
增量备份的恢复大体为3个步骤
*恢复完全备份
*恢复增量备份到完全备份(开始恢复的增量备份要添加--redo-only参数,到最后一次增量备份去掉--redo-only参数)
*对整体的完全备份进行恢复,回滚那些未提交的数据
恢复完全备份(注意这里一定要加--redo-only参数,该参数的意思是只应用xtrabackup日志中已提交的事务数据,不回滚还未提交的数据)
[root@service2 ~]# innobackupex --apply-log --redo-only /root/test1/2018-06-13_15-25-22
将增量备份1应用到完全备份
[root@service2 ~]# innobackupex --apply-log --redo-only /root/test1/2018-06-13_15-25-22 --incremental-dir=/root/incremental/2018-06-13_15-33-55
将增量备份2应用到完全备份(注意恢复最后一个增量备份时需要去掉--redo-only参数,回滚xtrabackup日志中那些还未提交的数据)
[root@service2 ~]# innobackupex --apply-log /root/test1/2018-06-13_15-25-22 --incremental-dir=/root/incremental/2018-06-13_15-43-24
把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据:
[root@service2 ~]# innobackupex --apply-log /root/test1/2018-06-13_15-25-22
把恢复完的备份复制到数据库目录文件中,赋权,然后启动mysql数据库,检测数据正确性
[root@service2 ~]# service mysqld stop
[root@service2 ~]# mv /var/lib/mysql /root/testdata1
[root@service2 ~]# mkdir /var/lib/mysql
[root@service2 ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /root/test1/2018-06-13_15-25-22/
[root@service2 ~]# chown -R mysql.mysql /var/lib/mysql
启动数据,查看增量数据在否:
[root@service2 ~]# service mysqld start
正在启动 mysqld: [确定]
[root@service2 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from t2;
ERROR 1046 (3D000): No database selected
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t2;
+------+
| name |
+------+
| will |
| tom |
| jim |
+------+
3 rows in set (0.00 sec)
mysql> select * from t1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
6 rows in set (0.00 sec)
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆引用☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
克隆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即为要新搭建的从库。在老的从库上面进行备份:
[root@MySQL-02 ~]# 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 目录要不存在,如果存在是会报错的。
查看目录下生成的文件:
[root@MySQL-02 ~]# 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
[root@MySQL-02 ~]#
查看xtrabackup_slave_info文件内容,这个内容就是为搭建从库时需要change master to的参数:
[root@MySQL-02 ~]# cat /data/cloneslave/xtrabackup_slave_info
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=732
[root@MySQL-02 ~]#
在老的slave服务器上进行还原,即192.168.0.20
[root@MySQL-02 ~]# 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!
[root@MySQL-02 ~]#
将还原的文件复制到新的从库newslave,即192.168.0.100
[root@MySQL-02 data]# rsync -avprP -e ssh /data/cloneslave/ 192.168.0.100:/data/mysql/
在主库master上添加对新从库newslave的授权:
(要注意/dir01/表示目录下的所有内容 /dir01表示的目录本身)
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参数,修改完毕后,启动新的从库;
[root@MySQL-02 data]# scp /etc/my.cnf 192.168.0.100:/etc/
root@192.168.0.100's password:
my.cnf 100% 4881 4.8KB/s 00:00
[root@MySQL-02 data]#
[root@newslave mysql]# egrep 'log-slave|^server-id|skip_slave' /etc/my.cnf
server-id = 3
skip_slave_start
log-slave-updates=1
[root@newslave mysql]#
[root@newslave mysql]# chown -R mysql.mysql .
[root@newslave mysql]# /etc/init.d/mysqld restart
Shutting down MySQL. [ OK ]
Starting MySQL.. [ OK ]
[root@newslave 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就结束啦。
https://www.linuxidc.com/Linux/2017-03/142380.htm