MySQL-5.7 备份与恢复

一、备份分类

按介质分类:

  • 物理备份
    指通过拷贝数据库文件方式完成备份,适用于数据库很大,数据重要且需要快速恢复的数据库。
  • 逻辑备份
    指通过备份数据库的逻辑结构和数据内容的方式完成备份,适用于数据库不是很大,或需要对导出文件做一定修改,或重建此库的情况。

优缺点:

  • 物理备份速度快于逻辑备份,因为逻辑备份需要访问数据库并将内容转化成逻辑备份需要的格式
  • 物理备份的备份恢复粒度范围是整个数据库或单个文件,对单表是否有恢复能力取决于存储引擎(MyISAM下每个表对应独立文件,可以单独恢复;InnoDB可能使用共享数据文件)
  • 物理备份要求在数据库关闭情况下执行,如果在运行情况下执行,要求备份期间数据库不能修改,逻辑备份需要在数据库运行状态下执行
  • 通常逻辑备份的文件大小比物理备份大
  • 逻辑备份不包含数据库的配置文件和日志文件内容

按状态分类:

  • 在线备份
  • 离线备份

按距离分类:

  • 本地备份
  • 远程备份

按类型分类:

  • 全量备份
    指备份中包含所有数据
  • 增量备份
    指备份中仅包含在某个指定时间段内的变化情况,需要借助二进制日志完成

二、MySQL备份方式

(1)mysqldump

mysqldump -u 用户名 -p 数据库名 数据库表 > 导出的文件名

(2)拷贝物理表生成备份
当前存储引擎下每个表都有自己独立的数据文件时可以使用这种方式。如果当前数据库是运行状态,则需要对此表加上一个只读锁,防止备份期间的修改操作。
对InnoDB存储引擎的表不太支持。

(3)select...into outfile

  • 通过select * into outfile ‘file_name’ from tbl_name生成在服务器上的文件
  • 通过mysqldump命令加--tab参数生成文件

只会生成表数据,不会生成表结构

(4)增量备份
将MySQL实例设置开启log-bin参数,备份增量生成的二进制日志到指定的备份地

(5)Xtrabackup
支持全量和增量备份

三、MySQL备份演示

(1)物理备份

CREATE TABLE `students_myisam` (
  `sid` int(11) NOT NULL,
  `sname` varchar(64) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`sid`),
  KEY `idx_sname` (`sname`),
  KEY `idx_gender` (`gender`),
  KEY `dept_id` (`dept_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
mysql> insert into students_myisam values(1,'a',1,1),(2,'b',2,2),(3,'c',3,3);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

开始备份
[root@localhost course]# pwd
/data1/mysql/data/course
[root@localhost course]# ll students_my*
-rw-r----- 1 mysql mysql 8660 Mar  5 11:11 students_myisam.frm
-rw-r----- 1 mysql mysql   60 Mar  5 11:12 students_myisam.MYD
-rw-r----- 1 mysql mysql 5120 Mar  5 11:12 students_myisam.MYI
把这个表相关的三个文件拷贝到另外的数据库实例对应的数据库目录下(记得需要修改文件权限)
[root@codis-178 cmdb_v2]# cp /home/xiaoda/students_myisam.* ./
[root@codis-178 cmdb_v2]# chown mysql:mysql students_myisam.*
mysql> select * from students_myisam;
+-----+-------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-------+--------+---------+
|   1 | a     |      1 |       1 |
|   2 | b     |      2 |       2 |
|   3 | c     |      3 |       3 |
+-----+-------+--------+---------+
3 rows in set (0.00 sec)

对于InnoDB表来说,即使设置了innodb_file_per_table=on时,直接拷贝也不行

CREATE TABLE `students_myisam2` (
  `sid` int(11) NOT NULL,
  `sname` varchar(64) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`sid`),
  KEY `idx_sname` (`sname`),
  KEY `idx_gender` (`gender`),
  KEY `dept_id` (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> insert into students_myisam2 values(1,'a',1,1),(2,'b',2,2),(3,'c',3,3);                                                     
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

开始备份
[root@codis-178 cmdb_v2]# cp /home/xiaoda/students_myisam2.* ./
[root@codis-178 cmdb_v2]# chown mysql:mysql students_myisam2.*
mysql> select * from students_myisam2;
ERROR 1146 (42S02): Table 'cmdb_v2.students_myisam2' doesn't exist

日志报错:
180305 11:23:53 [ERROR] Cannot find or open table cmdb_v2/students_myisam2 from
the internal data dictionary of InnoDB though the .frm file for the
table exists. Maybe you have deleted and recreated InnoDB data
files but have forgotten to delete the corresponding .frm files
of InnoDB tables, or you have moved .frm files to another database?
or, the table contains indexes that this version of the engine
doesn't support.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-troubleshooting.html
how you can resolve the problem.

所以对于InnoDB来说可以通过拷贝整个data目录方式来完成备份和恢复。

(2)Mysqldump
用来生成MySQL的逻辑备份文件,其文件内容就是构成数据库对象和数据内容的可重复执行的SQL语句。

mysqldump [OPTIONS] database [tables]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
mysqldump [OPTIONS] --all-databases [OPTIONS]

options的关键参数:

-h, --host=name  要导出的目标数据库所在主机,默认是localhost
-u, --user=name  链接目标数据库的数据库用户名
-p, --password[=name]  链接目标数据库的数据库密码
-P, --port=#  链接目标数据库的端口

--add-drop-database  在使用--databases或--all-databases参数时在每个create database命令前都加上drop database命令
--add-drop-table  在每个create table命令前加上drop table命令
--default-character-set=name  指定默认的字符集,默认是UTF8
--replace  使用该命令插入数据而不是使用insert命令
--set-charset  将set names default_character_set命令写入到导出备份文件中,默认是开启状态
--dump-slave[=#]  表示从复制的slave从库导出备份,且其中包含了change master 通语句。value参数如果不写或=-1的情况下,则change master to语句写入dump文件中,设置为2则表示也写入dump文件中,只是会注释掉
--master-data[=#]  表示从复制的主库上导出备份。value参数与--dump-slave相同。使用该参数会自动打开lock-all-table参数,除非同时使用--single-transaction参数
-T, --tab=name  表示将备份文件以文本文件的方式生成,并指定存放文件路径,每个表会生成两个文件,一个是.sql文件保存表结构,一个是.txt文件保存表数据信息
-A, --all-databases  导出所有数据库里的所有表
-B, --databases  导出指定的一个或多个数据库
--ignore-table=name  代表导出过程中忽略某个指定表的导出,如果要忽略多个表则这个参数使用多次
-d, --no-data  代表只导出表结构
-R, --routines  代表导出时也要把存储过程和函数也导出来
--triggers  代表导出时也将触发器导出来
-w, --where=name  代表导出符合条件的数据
-x, --lock-all-tables  代表在导出过程中对每个数据库的每个表加上一个只读锁
--no-autocommit  代表对每个表的数据导出内容用set autocommit=0和commit两个语句包裹
--single-transaction  代表将事务隔离级别设置为可重复读并在导出开始执行start transaction开启一个新事务,在dump执行过程中也不会阻止任何读写操作

例子:

导出一个数据库
[root@localhost ~]# mysqldump -uroot -p -P3306 --databases course > backup.sql

导出多个数据库
[root@localhost ~]# mysqldump -uroot -p -P3306 --databases course test > course.sql
[root@localhost ~]# mysqldump -uroot -p -P3306 -B course test > course.sql

导出所有数据库
[root@localhost ~]# mysqldump -uroot -p -P3306 --all-databases > all.sql

仅导出course数据库的数据,不包括表结构
[root@localhost ~]# mysqldump -uroot -p -P3306 --no-create-info course > course.sql

仅导出course数据库中的students和students_myisam两个表
[root@localhost ~]# mysqldump -uroot -p -P3306 --no-create-info course students students_myisam > students.sql

仅导出course数据库的表结构
[root@localhost ~]# mysqldump -uroot -p -P3306 --no-data course > course.sql

导出course数据库中除了teacher和score两个表的其他表结构和数据
[root@localhost ~]# mysqldump -uroot -p -P3306 --ignore-table=course.teacher --ignore-table=course.score course > course.sql

导出course数据库的表和存储过程和触发器
[root@localhost ~]# mysqldump -uroot -p -P3306 --routines --triggers course > course.sql

导出course数据库中符合条件的数据
[root@localhost ~]# mysqldump -uroot -p -P3306 --where="sid in (1,2)" course students students_myisam > course.sql

远程导出course数据库
[root@localhost ~]# mysqldump -uroot -p -P3306 -h192.168.1.178 cmdb_v2 students_myisam > students.sql

在主库备份
[root@codis-178 ~]# mysqldump -uroot -p -P3306 --master-data=2 --single-transctions course > course.sql
(在备份开始之初,在所有表上加一个只读锁(flush table with read lock),当成功获取该锁并开始备份之后,此锁就会立即释放,后续dump过程不会影响其他的读写操作)

在从库备份
[root@codis-178 ~]# mysqldump -uroot -p -P3306 --dump-slave --single-transctions test > test.sql

这里注意,导出时报以下错误:

[root@codis-178 ~]# mysqldump -uroot -p -P3306 -h192.168.1.68 course > course.sql
Enter password: 
mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_QUOTE_SHOW_CREATE=1' at line 1 (1064)

这是由于mysqldump版本低导致,也就是5.5版本不能导出5.7版本
如何解决?
用5.7或更高版本的mysqldump覆盖或者指定目录运行即可?

使用mysqldump命令导出文本文件,通过指定--tab=dir_name参数来指定文件路径

添加配置
secure-file-priv=/tmp/

[root@localhost ~]# mysqldump -uroot -p -P3306 --tab=/tmp course
[root@localhost ~]# ll /tmp/course.sql
-rw-r--r-- 1 root root 1544 Mar  5 13:28 /tmp/course.sql

还可指定文件格式

  • --fields-teminated-by=str
    指定每个字段值之间的间隔符,默认是tab
  • --fields-enclosed-by=char
    指定每个字段值使用什么字符括起来,默认是没有
  • --fields-optionsally-enclosed-by=char
    指定对每个非数字类型的字段使用什么字符括起来,默认没有
  • --lines-terminated-by=str
    指定行之间的结束符,默认是newline
[root@localhost ~]# mysqldump -uroot -p -P3306 --tab=/tmp course --fields-terminated-by=, --fields-enclosed-by="'" --lines-terminated-by="
" course
Enter password: 
[root@localhost ~]# cat /tmp/course.txt 
'1','math','3'
'2','english','2'
'3','chinese','4'
'4','history','1'
'5','biology','5'

(3)select... into outfile
用来导出表中符合条件的数据到文本文件,不导出表结构

mysql> select * from students_myisam into outfile '/tmp/students_myisam_test.txt' fields terminated by ',' enclosed by "'" lines teerminated by '
';
Query OK, 3 rows affected (0.00 sec)
[root@localhost ~]# cat /tmp/students_myisam_test.txt 
'1','a','1','1'
'2','b','2','2'
'3','c','3','3'

mysql> select * from students_myisam where sid in (1,2) into outfile '/tmp/students_myisam_test2.txt' fields terminated by ',' encllosed by "'" lines terminated by '
';
Query OK, 2 rows affected (0.01 sec)
[root@localhost ~]# cat /tmp/students_myisam_test2.txt 
'1','a','1','1'
'2','b','2','2'

参数说明:

  • terminated by
    字段以什么字符分隔
  • enclosed by
    字段以什么字符括起来
  • escaped by
    转义字符,默认是反斜杠
  • lines
    每条记录的分隔符,默认是换行符
  • local
    指定从客户主机读文件,没有指定则文件必须在服务器上
  • replace
    新行将代替有相同的唯一值的现有行
  • ignore
    跳过有唯一键的现有行的重复行的输入,不指定时当遇到重复行会报错

四、备份的重要概念

(1)锁
在执行mysqldump时,会添加flush tables with read lock(FTWRL),用于备份时获取一致性备份(数据与binlog位点匹配)。
由于FTWRL总共需要持有两把全局MDL锁,并且还需要关闭所有表对象,因此这个命令杀伤力很大,执行命令时容易导致库hang住。

FTWEL主要包括三个步骤:
1.上全局读锁(lock_global_read_lock)
导致所有更新操作都会被堵塞
2.清理表缓存(close_cached_tables)
关闭表过程中,如果有大量查询导致关闭表等待,那么所有访问该表的查询和更新都需要等待
3.上全局commit锁(make_global_read_lock_block_commit)
会堵塞活跃事务提交

第一个session
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.15 sec)

第二个session
mysql> select * from dept;
+----+------------------+
| id | dept_name        |
+----+------------------+
|  1 | Education        |
|  2 | Computer Science |
|  3 | Mathematics      |
+----+------------------+
3 rows in set (0.00 sec)

mysql> update dept set dept_name="Sport" where id=1;
此时写操作会被阻止,等待超时

第一个session
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

flush tables with read lock与lock table read local的区别

第一个session
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> update dept set dept_name="Sport" where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

第二个session
执行备份操作,当存在--master-data参数时,导出成功
[root@localhost ~]# mysqldump -uroot -p -P3306 --master-data course > course.sql

当普通导出时,发生锁等待情况
[root@localhost ~]# mysqldump -uroot -p -P3306 course > course.sql
Enter password:

在第一个session中查看
mysql> show processlist;

(2)可重复读隔离级别

start transaction和start transaction with consistent snapshot语句的区别

  • start transaction
    是第一条语句的执行时间点,就是事务开始的时间点,第一条select语句建立一致性读的snapshot;
  • start transaction with consistent snapshot
    是立即建立事务的一致性读snapshot,同时开启事务;
第一个session
mysql> set tx_isolation='repeatable-read';
Query OK, 0 rows affected, 1 warning (0.00 sec)

第二个session
mysql> select * from A;
+------+-------+
| sid  | score |
+------+-------+
|    8 |    94 |
+------+-------+
1 row in set (0.01 sec)

第一个session
mysql> start transaction;
Query OK, 0 rows affected (0.04 sec)

第二个session
mysql> insert into A value(9,87);
Query OK, 1 row affected (0.07 sec)

第一个session
mysql> select * from A;
+------+-------+
| sid  | score |
+------+-------+
|    8 |    94 |
|    9 |    87 |
+------+-------+
2 rows in set (0.00 sec)

可以看到session2修改后的记录
第一个session
mysql> start transaction with consistent snapshot;
Query OK, 0 rows affected (0.00 sec)

第二个session
mysql> insert into A value(10,76);
Query OK, 1 row affected (0.08 sec)

第一个session
mysql> select * from A;
+------+-------+
| sid  | score |
+------+-------+
|    8 |    94 |
|    9 |    87 |
+------+-------+
2 rows in set (0.00 sec)

不可以看到session2修改后的记录,需要提交

说明:
start transaction执行后,事务并没有开始,所以insert发生在session1的事务开始之前,所以可以读到修改后的值。
start transaction with consistent snapshot已经开始了事务,所以不能读到。

五、恢复

(1)普通恢复

导入一个备份文件
mysql -uroot -p course < course.sql
或者
进入数据库,并切换到实例下
source course.sql

(2)恢复文本文件

  • 先导入表结构
  • 再导入数据文件
数据文件导入使用mysqlimport或是load data infile
mysqlimport -uroot -p --fields-terminated-by=, --fields-enclosed-by="'" --lines-terminated-by="
" course /tmp/course.txt

use course;
load data infile '/tmp/course.txt' into table students fields terminated by ',' enclosed by "'" lines terminated by '
';

(3)全量恢复
将备份文件中的所有数据进行恢复,恢复完成后的数据就是生成备份的那一刻的数据状态。

(4)基于时间点的恢复
将数据库恢复到指定的某个时间点的状态,通常需要依赖二进制日志将指定时间点前的所有数据库操作都重新操作一遍。
步骤:
1.通过全量备份将数据库恢复到上一个全量恢复的时间点
2.利用二进制日志恢复到指定时间点

开启二进制日志

log-bin=mysql-bin
binlog_format=ROW
expire_logs_days=10

测试实验:

mysql> alter table students add tstamp timestamp;
Query OK, 0 rows affected (0.92 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> flush logs;
Query OK, 0 rows affected (0.30 sec)

mysql> insert into students(sid,sname,gender,dept_id,tstamp) values(9,'Mix',1,2,now()),(10,'Tom',0,1,now());
Query OK, 2 rows affected (0.09 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> flush logs;
Query OK, 0 rows affected (0.24 sec)

mysql> insert into students(sid,sname,gender,dept_id,tstamp) values(11,'Luis',-1,2,now()),(12,'Sun',0,3,now());
Query OK, 2 rows affected (0.09 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into students(sid,sname,gender,dept_id,tstamp) values(13,'Martis',-1,1,now()),(14,'Oifer',1,3,now());
Query OK, 2 rows affected (0.07 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> flush logs;
Query OK, 0 rows affected (0.24 sec)

mysql> select * from students;
+-----+--------+--------+---------+---------------------+
| sid | sname  | gender | dept_id | tstamp              |
+-----+--------+--------+---------+---------------------+
|   1 | abc    | 1      |       1 | 2018-03-05 14:46:41 |
|   2 | Andy   | -1     |       1 | 2018-03-05 14:46:41 |
|   3 | Bob    | -1     |       1 | 2018-03-05 14:46:41 |
|   4 | Ruth   | -1     |       2 | 2018-03-05 14:46:41 |
|   5 | Mike   | -1     |       2 | 2018-03-05 14:46:41 |
|   6 | John   | 0      |       3 | 2018-03-05 14:46:41 |
|   7 | Cindy  | 1      |       3 | 2018-03-05 14:46:41 |
|   8 | Susan  | 1      |       3 | 2018-03-05 14:46:41 |
|   9 | Mix    | 1      |       2 | 2018-03-05 14:50:04 |
|  10 | Tom    | 0      |       1 | 2018-03-05 14:50:04 |
|  11 | Luis   | -1     |       2 | 2018-03-05 14:51:48 |
|  12 | Sun    | 0      |       3 | 2018-03-05 14:51:48 |
|  13 | Martis | -1     |       1 | 2018-03-05 14:52:27 |
|  14 | Oifer  | 1      |       3 | 2018-03-05 14:52:27 |
+-----+--------+--------+---------+---------------------+
14 rows in set (0.00 sec)

mysql> truncate table students;
Query OK, 0 rows affected (0.21 sec)

mysql> select * from students;
Empty set (0.00 sec)

首先恢复students表的全量备份
mysql> source backup.sql;
mysql> select * from students;
+-----+-------+--------+---------+---------------------+
| sid | sname | gender | dept_id | tstamp              |
+-----+-------+--------+---------+---------------------+
|   1 | abc   | 1      |       1 | 2018-03-05 14:46:41 |
|   2 | Andy  | -1     |       1 | 2018-03-05 14:46:41 |
|   3 | Bob   | -1     |       1 | 2018-03-05 14:46:41 |
|   4 | Ruth  | -1     |       2 | 2018-03-05 14:46:41 |
|   5 | Mike  | -1     |       2 | 2018-03-05 14:46:41 |
|   6 | John  | 0      |       3 | 2018-03-05 14:46:41 |
|   7 | Cindy | 1      |       3 | 2018-03-05 14:46:41 |
|   8 | Susan | 1      |       3 | 2018-03-05 14:46:41 |
+-----+-------+--------+---------+---------------------+
8 rows in set (0.01 sec)

恢复某个时间点数据
[root@localhost data]# mysqlbinlog mysql-bin.000002 | mysql -uroot -p
Enter password: 
mysql> select * from students;
+-----+-------+--------+---------+---------------------+
| sid | sname | gender | dept_id | tstamp              |
+-----+-------+--------+---------+---------------------+
|   1 | abc   | 1      |       1 | 2018-03-05 14:46:41 |
|   2 | Andy  | -1     |       1 | 2018-03-05 14:46:41 |
|   3 | Bob   | -1     |       1 | 2018-03-05 14:46:41 |
|   4 | Ruth  | -1     |       2 | 2018-03-05 14:46:41 |
|   5 | Mike  | -1     |       2 | 2018-03-05 14:46:41 |
|   6 | John  | 0      |       3 | 2018-03-05 14:46:41 |
|   7 | Cindy | 1      |       3 | 2018-03-05 14:46:41 |
|   8 | Susan | 1      |       3 | 2018-03-05 14:46:41 |
|   9 | Mix   | 1      |       2 | 2018-03-05 14:50:04 |
|  10 | Tom   | 0      |       1 | 2018-03-05 14:50:04 |
+-----+-------+--------+---------+---------------------+
10 rows in set (0.00 sec)

[root@localhost data]# mysqlbinlog mysql-bin.000003 | mysql -uroot -p
Enter password: 
mysql> select * from students;
+-----+--------+--------+---------+---------------------+
| sid | sname  | gender | dept_id | tstamp              |
+-----+--------+--------+---------+---------------------+
|   1 | abc    | 1      |       1 | 2018-03-05 14:46:41 |
|   2 | Andy   | -1     |       1 | 2018-03-05 14:46:41 |
|   3 | Bob    | -1     |       1 | 2018-03-05 14:46:41 |
|   4 | Ruth   | -1     |       2 | 2018-03-05 14:46:41 |
|   5 | Mike   | -1     |       2 | 2018-03-05 14:46:41 |
|   6 | John   | 0      |       3 | 2018-03-05 14:46:41 |
|   7 | Cindy  | 1      |       3 | 2018-03-05 14:46:41 |
|   8 | Susan  | 1      |       3 | 2018-03-05 14:46:41 |
|   9 | Mix    | 1      |       2 | 2018-03-05 14:50:04 |
|  10 | Tom    | 0      |       1 | 2018-03-05 14:50:04 |
|  11 | Luis   | -1     |       2 | 2018-03-05 14:51:48 |
|  12 | Sun    | 0      |       3 | 2018-03-05 14:51:48 |
|  13 | Martis | -1     |       1 | 2018-03-05 14:52:27 |
|  14 | Oifer  | 1      |       3 | 2018-03-05 14:52:27 |
+-----+--------+--------+---------+---------------------+

如果恢复某个日志文件中的一部分内容,可以通过指定--start-datetime或是--stop-datetime参数来确定开始恢复和停止的时间。

mysqlbinlog --start-datetime="2018-02-05 10:23:41" /data1/mysql/data/mysql-bin.000001 | mysql -uroot -p
mysqlbinlog --stop-datetime="2018-03-05 15:00:00" /data1/mysql/data/mysql-bin.000001 | mysql -uroot -p

六、Xtrabackup

Xtrabackup是一个对MySQL做数据备份的工具,支持在线热备份(备份时不影响数据读写)。

特点:

  • 备份过程快、可靠
  • 备份过程不会打断正在执行的事务
  • 能够基于压缩等功能节约磁盘空间和流量
  • 自动实现备份检验
  • 还原速度快
[root@localhost ~]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/tarball/percona-xtrabackup-2.4.9-Linux-x86_64.tar.gz
[root@localhost ~]# tar -zxvf percona-xtrabackup-2.4.9-Linux-x86_64.tar.gz
[root@localhost ~]# cp percona-xtrabackup-2.4.9-Linux-x86_64/bin/* /usr/bin/

全量备份
[root@localhost data1]# mkdir backup
[root@localhost backup]# xtrabackup --backup --target-dir=/data1/backup/ -uroot -p'MY@)!&sql2017' -P3306 --host=127.0.0.1
...
...
...
xtrabackup: Transaction log of lsn (11460068) to (11460077) was copied.
180305 15:14:34 completed OK!

恢复
[root@localhost mysql]# mv data data_bak
[root@localhost mysql]# ls
data_bak
[root@localhost mysql]# mkdir data
[root@localhost mysql]# chown -R mysql:mysql data

首先执行prepare,将所有的数据文件都准备到同一时间点,因为在备份过程中所有数据文件都在不同的时间点,如果直接恢复会导致冲突
[root@localhost mysql]# xtrabackup --prepare --target-dir=/data1/backup/
...
...
...
InnoDB: Shutdown completed; log sequence number 11461479
180305 15:16:48 completed OK!

全量恢复
[root@localhost mysql]# xtrabackup --copy-back --target-dir=/data1/backup/ --datadir=/data1/mysql/data
...
...
...
180305 15:20:17 [01] Copying ./test1/app01.ibd to /data1/mysql/data/test1/app01.ibd
180305 15:20:17 [01]        ...done
180305 15:20:17 completed OK!
[root@localhost mysql]# chown -R mysql:mysql data
增量备份
[root@localhost backup]# mkdir base
[root@localhost backup]# chown -R mysql:mysql base
[root@localhost backup]# xtrabackup --backup --traget-dir=/data1/backup/base -uroot -p'MY@)!&sql2017' -P3306 --host=127.0.0.1

mysql> insert into students values(15,'aa',1,1,now());
Query OK, 1 row affected (0.07 sec)

mysql> insert into students values(16,'bb',1,2,now());
Query OK, 1 row affected (0.09 sec)

[root@localhost ~]# xtrabackup --backup --target-dir=/data1/backup/inc1 --incremental-basedir=/data1/backup/base -uroot -p'MY@)!&sql2017' -P3306 --host=127.0.0.1

mysql> insert into students values(17,'cc',0,3,now());
Query OK, 1 row affected (0.19 sec)

mysql> insert into students values(18,'dd',-1,3,now());
Query OK, 1 row affected (0.22 sec)

[root@localhost ~]# mkdir -p /data1/backup/inc2
[root@localhost ~]# chown -R mysql:mysql /data1/backup/inc2
[root@localhost ~]# xtrabackup --backup --target-dir=/data1/backup/inc2 --incremental-basedir=/data1/backup/inc1 -uroot -p'MY@)!&sql2017' -P3306 --host=127.0.0.1

增量恢复
[root@localhost mysql]# xtrabackup --prepare --apply=log-only --target-dir=/data1/backup/base --datadir=/data1/mysql/data
[root@localhost mysql]# xtrabackup --prepare --apply-log-only --target-dir=/data1/backup/base --incremental-datadir=/data1/backup/inc1 --datadir=/data1/mysql/data
[root@localhost mysql]# xtrabackup --prepare --target-dir=/data1/backup/base --incremental-datadir=/data1/backup/inc2 --datadir=/data1/mysql/data
[root@localhost mysql]# xtrabackup --copy-back --target-dir=/data1/backup/base/ --datadir=/data1/mysql/data

mysql> select * from students;
+-----+--------+--------+---------+---------------------+
| sid | sname  | gender | dept_id | tstamp              |
+-----+--------+--------+---------+---------------------+
|   1 | abc    | 1      |       1 | 2018-03-05 14:46:41 |
|   2 | Andy   | -1     |       1 | 2018-03-05 14:46:41 |
|   3 | Bob    | -1     |       1 | 2018-03-05 14:46:41 |
|   4 | Ruth   | -1     |       2 | 2018-03-05 14:46:41 |
|   5 | Mike   | -1     |       2 | 2018-03-05 14:46:41 |
|   6 | John   | 0      |       3 | 2018-03-05 14:46:41 |
|   7 | Cindy  | 1      |       3 | 2018-03-05 14:46:41 |
|   8 | Susan  | 1      |       3 | 2018-03-05 14:46:41 |
|   9 | Mix    | 1      |       2 | 2018-03-05 14:50:04 |
|  10 | Tom    | 0      |       1 | 2018-03-05 14:50:04 |
|  11 | Luis   | -1     |       2 | 2018-03-05 14:51:48 |
|  12 | Sun    | 0      |       3 | 2018-03-05 14:51:48 |
|  13 | Martis | -1     |       1 | 2018-03-05 14:52:27 |
|  14 | Oifer  | 1      |       3 | 2018-03-05 14:52:27 |
|  15 | aa     | 1      |       1 | 2018-03-05 15:34:55 |
|  16 | bb     | 1      |       2 | 2018-03-05 15:35:55 |
|  17 | cc     | 0      |       3 | 2018-03-05 15:42:14 |
|  18 | dd     | -1     |       3 | 2018-03-05 15:42:23 |
+-----+--------+--------+---------+---------------------+
18 rows in set (0.00 sec)
原文地址:https://www.cnblogs.com/tongxiaoda/p/8507688.html