set sql_log_bin=0;
查看配置文件
log_bin=/data/binlog/mysql-bin
binlog_format=row
前往log_bin指定的日志存放路径
登录mysql查看当前使用的日志文件
Master [(none)]>show master status;
+------------------+----------+--------------+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| mysql-bin.000011 | 5435 | | | 3a028bbf-602f-11e9-ae80-000c293e875b:1-27,
7859fc84-601c-11e9-90e1-000c293e875b:1-11,
e8642a8b-5ff3-11e9-958d-000c293e875b:1-216,
fe99ae3c-55c0-11e9-abcc-000c293e875b:1-24 |
+------------------+----------+--------------+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Master [(none)]>
查看日志文件详细内容(生产测试环境日志内容极大且不易寻找)
Master [(none)]>show binlog events in 'mysql-bin.000011';
+------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| mysql-bin.000011 | 4 | Format_desc | 6 | 123 | Server ver: 5.7.20-log, Binlog ver: 4 |
| mysql-bin.000011 | 123 | Previous_gtids | 6 | 274 | 7859fc84-601c-11e9-90e1-000c293e875b:1-11,
e8642a8b-5ff3-11e9-958d-000c293e875b:1-216,
fe99ae3c-55c0-11e9-abcc-000c293e875b:1-24 |
| mysql-bin.000011 | 274 | Gtid | 6 | 339 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:1' |
| mysql-bin.000011 | 339 | Query | 6 | 436 | create database week3 |
| mysql-bin.000011 | 436 | Gtid | 6 | 501 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:2' |
| mysql-bin.000011 | 501 | Query | 6 | 601 | use `week3`; create table t1 (id int) |
| mysql-bin.000011 | 601 | Gtid | 6 | 666 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:3' |
| mysql-bin.000011 | 666 | Query | 6 | 739 | BEGIN |
| mysql-bin.000011 | 739 | Table_map | 6 | 785 | table_id: 238 (week3.t1) |
| mysql-bin.000011 | 785 | Write_rows | 6 | 850 | table_id: 238 flags: STMT_END_F |
| mysql-bin.000011 | 850 | Xid | 6 | 881 | COMMIT /* xid=30 */ |
| mysql-bin.000011 | 881 | Gtid | 6 | 946 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:4' |
| mysql-bin.000011 | 946 | Query | 6 | 1043 | create database week2 |
| mysql-bin.000011 | 1043 | Gtid | 6 | 1108 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:5' |
| mysql-bin.000011 | 1108 | Query | 6 | 1209 | use `week2`; create table t1 (id int) |
| mysql-bin.000011 | 1209 | Gtid | 6 | 1274 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:6' |
| mysql-bin.000011 | 1274 | Query | 6 | 1347 | BEGIN |
| mysql-bin.000011 | 1347 | Table_map | 6 | 1393 | table_id: 239 (week2.t1) |
| mysql-bin.000011 | 1393 | Write_rows | 6 | 1453 | table_id: 239 flags: STMT_END_F |
| mysql-bin.000011 | 1453 | Xid | 6 | 1484 | COMMIT /* xid=76 */ |
| mysql-bin.000011 | 1484 | Gtid | 6 | 1549 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:7' |
| mysql-bin.000011 | 1549 | Query | 6 | 1622 | BEGIN |
| mysql-bin.000011 | 1622 | Table_map | 6 | 1668 | table_id: 238 (week3.t1) |
| mysql-bin.000011 | 1668 | Write_rows | 6 | 1733 | table_id: 238 flags: STMT_END_F |
| mysql-bin.000011 | 1733 | Xid | 6 | 1764 | COMMIT /* xid=93 */ |
| mysql-bin.000011 | 1764 | Gtid | 6 | 1829 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:8' |
| mysql-bin.000011 | 1829 | Query | 6 | 1924 | drop database week1 |
| mysql-bin.000011 | 1924 | Gtid | 6 | 1989 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:9' |
| mysql-bin.000011 | 1989 | Query | 6 | 2084 | drop database week2 |
| mysql-bin.000011 | 2084 | Gtid | 6 | 2149 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:10' |
| mysql-bin.000011 | 2149 | Query | 6 | 2244 | drop database week3 |
| mysql-bin.000011 | 2244 | Gtid | 6 | 2309 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:11' |
| mysql-bin.000011 | 2309 | Query | 6 | 2407 | drop database oldboy |
| mysql-bin.000011 | 2407 | Gtid | 6 | 2472 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:12' |
| mysql-bin.000011 | 2472 | Query | 6 | 2579 | drop database liulaoliu |
| mysql-bin.000011 | 2579 | Gtid | 6 | 2644 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:13' |
| mysql-bin.000011 | 2644 | Query | 6 | 2736 | drop database gtid |
| mysql-bin.000011 | 2736 | Gtid | 6 | 2801 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:14' |
| mysql-bin.000011 | 2801 | Query | 6 | 2893 | drop database day1 |
| mysql-bin.000011 | 2893 | Gtid | 6 | 2958 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:15' |
| mysql-bin.000011 | 2958 | Query | 6 | 3047 | drop database binlog |
| mysql-bin.000011 | 3047 | Gtid | 6 | 3112 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:16' |
| mysql-bin.000011 | 3112 | Query | 6 | 3206 | create database day1 |
| mysql-bin.000011 | 3206 | Gtid | 6 | 3271 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:17' |
| mysql-bin.000011 | 3271 | Query | 6 | 3369 | use `day1`; create table q1 (id int) |
| mysql-bin.000011 | 3369 | Gtid | 6 | 3434 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:18' |
| mysql-bin.000011 | 3434 | Query | 6 | 3506 | BEGIN |
| mysql-bin.000011 | 3506 | Table_map | 6 | 3551 | table_id: 242 (day1.q1) |
| mysql-bin.000011 | 3551 | Write_rows | 6 | 3606 | table_id: 242 flags: STMT_END_F |
| mysql-bin.000011 | 3606 | Xid | 6 | 3637 | COMMIT /* xid=154 */ |
| mysql-bin.000011 | 3637 | Gtid | 6 | 3702 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:19' |
| mysql-bin.000011 | 3702 | Query | 6 | 3794 | drop database day1 |
| mysql-bin.000011 | 3794 | Gtid | 6 | 3859 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:20' |
| mysql-bin.000011 | 3859 | Query | 6 | 3945 | drop database db |
| mysql-bin.000011 | 3945 | Gtid | 6 | 4010 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:21' |
| mysql-bin.000011 | 4010 | Query | 6 | 4107 | create database week3 |
| mysql-bin.000011 | 4107 | Gtid | 6 | 4172 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:22' |
| mysql-bin.000011 | 4172 | Query | 6 | 4272 | use `week3`; create table t1 (id int) |
| mysql-bin.000011 | 4272 | Gtid | 6 | 4337 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:23' |
| mysql-bin.000011 | 4337 | Query | 6 | 4410 | BEGIN |
| mysql-bin.000011 | 4410 | Table_map | 6 | 4456 | table_id: 247 (week3.t1) |
| mysql-bin.000011 | 4456 | Write_rows | 6 | 4521 | table_id: 247 flags: STMT_END_F |
| mysql-bin.000011 | 4521 | Xid | 6 | 4552 | COMMIT /* xid=280 */ |
| mysql-bin.000011 | 4552 | Gtid | 6 | 4617 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:24' |
| mysql-bin.000011 | 4617 | Query | 6 | 4714 | create database week2 |
| mysql-bin.000011 | 4714 | Gtid | 6 | 4779 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:25' |
| mysql-bin.000011 | 4779 | Query | 6 | 4880 | use `week2`; create table t1 (id int) |
| mysql-bin.000011 | 4880 | Gtid | 6 | 4945 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:26' |
| mysql-bin.000011 | 4945 | Query | 6 | 5018 | BEGIN |
| mysql-bin.000011 | 5018 | Table_map | 6 | 5064 | table_id: 248 (week2.t1) |
| mysql-bin.000011 | 5064 | Write_rows | 6 | 5124 | table_id: 248 flags: STMT_END_F |
| mysql-bin.000011 | 5124 | Xid | 6 | 5155 | COMMIT /* xid=298 */ |
| mysql-bin.000011 | 5155 | Gtid | 6 | 5220 | SET @@SESSION.GTID_NEXT= '3a028bbf-602f-11e9-ae80-000c293e875b:27' |
| mysql-bin.000011 | 5220 | Query | 6 | 5293 | BEGIN |
| mysql-bin.000011 | 5293 | Table_map | 6 | 5339 | table_id: 247 (week3.t1) |
| mysql-bin.000011 | 5339 | Write_rows | 6 | 5404 | table_id: 247 flags: STMT_END_F |
| mysql-bin.000011 | 5404 | Xid | 6 | 5435 | COMMIT /* xid=303 */ |
+------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------+
77 rows in set (0.00 sec)
Master [(none)]>
可以看到使用过的语句,然后开始恢复
mysqlbinlog --skip-gtids --include-gtids='3a028bbf-602f-11e9-ae80-000c293e875b:1-7' /data/binlog/mysql-bin.000011 >/tmp/qwe.sql
执行语句,将文件中语句追加到/tmp/qwe.sql文件中
–skip-gtids :跳过幂等性
登录数据库执行
Master [(none)]>source /tmp/qwe.sql;
Master [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| day1 |
| mysql |
| performance_schema |
| school |
| sys |
| week2 |
| week3 |
| world |
+--------------------+
9 rows in set (0.00 sec)
文件恢复
mysqldump -uroot -p -A -R -E --triggers --master-data=2 --single-transaction --set-gtid-purged=OFF >/data/backup/full.sql
将备份出的文件放在了/data/backup下,名为full.sql
-A | 全备参数,将所有全部备份 |
-u | 指定用户 |
-p | 指定密码 |
-R | 备份存储过程及函数 |
-B | 指定要备份的库 |
-E | 备份事件 |
-triggers | 备份触发器 |
–triggers --master-data=2 | 以注释的形式,保存备份开始时间点的binlog的状态信息 |
–single-transaction | 只对非InnoDB表进行锁表备份 |
执行过语句后删库模拟一下数据恢复
#生产测试环境因备份后有新数据产生还需配合二进制日志进行恢复
Master [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| day1 |
| mysql |
| performance_schema |
| school |
| sys |
| week2 |
| week3 |
| world |
+--------------------+
9 rows in set (0.01 sec)
Master [(none)]>drop database week2;
Query OK, 1 row affected (0.04 sec)
Master [(none)]>drop database week3;
Query OK, 1 row affected (0.01 sec)
Master [(none)]>drop database day1;
Query OK, 1 row affected (0.08 sec)
Master [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
执行语句
Master [(none)]>set sql_log_bin=0;
Master [(none)]>source /data/backup/full.sql;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
此处省略XXXXX行
Master [world]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| day1 |
| mysql |
| performance_schema |
| school |
| sys |
| week2 |
| week3 |
| world |
+--------------------+
9 rows in set (0.00 sec)
数据回来了
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
或者
yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL libev
#el7版本下载
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.12/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
#el6版本下载
https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm
#yum下载(el7)版本
yum -y install percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
优点:
(1)对于非Innodb表(比如 myisam)是直接,锁表cp数据文件,属于一种温备份。
(2)对于Innodb的表(支持事务的),不锁表,拷贝数据页,最终以数据文件的方式保存下来,把一部分redo和undo一并备走,属于热备方式。
全备
innobackupex --user=root --password=123 --no-timestamp /data/backup/full
全备恢复
cp -a /backup/full/* /data/mysql1/
#/data/mysql1是数据库存放数据的目录
首先全备文件
innobackupex --user=root --password 123 --no-timestamp /data/backup/backup >&/data/backup/xbk_backup.log
然后第一次增备
innobackupex --user=root --password=123 --no-timestamp --incremental --incremental-basedir=/data/backup/full /data/backup/backup01 &>/data/backup/inc1.log
第二次增备
innobackupex --user=root --password=123 --no-timestamp --incremental --incremental-basedir=/data/backup/backup01 /data/backup/backup02 &>/data/backup/inc2.log
恢复数据
XBK恢复数据时需要把数据导进一个空目录中,目录中不能有数据,而且数据库也不能开启
1.检查已备份的数据
[root@db01 backup]# cat backup/xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 130717809
last_lsn = 130717818
compact = 0
recover_binlog_info = 0
[root@db01 backup]# cat backup01/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 130717809
to_lsn = 130732942
last_lsn = 130732951
compact = 0
recover_binlog_info = 0
[root@db01 backup]# cat backup02/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 130732942
to_lsn = 130738754
last_lsn = 130738763
compact = 0
recover_binlog_info = 0
将redo进行重做,已提交的写到数据文件,未提交的使用undo回滚掉
备份整理(apply-log)+合并备份(full+inc1+inc2)
全备整理
innobackupex --apply-log --redo-only /data/backup/backup
将第一次备份合并
innobackupex --apply-log --redo-only --incremental-dir=/data/backup/backup01 /data/backup/backup
合并第二次备份
innobackupex --apply-log --incremental-dir=/data/backup/backup02 /data/backup/backup
最后一次整理
innobackupex --apply-log /data/backup/backup
恢复数据
将数据cp到数据目录下
cp -a * /data/mysql/data
或者
innobackupex --copy-back /data/mysql/data
数据目录下必须是空的,而且数据库必须关闭
参数:
redo-only:
在准备满基时应使用此选项
备份和合并除最后一个增量外的所有增量
一个。这将迫使xtrabackup跳过“回滚”阶段
只做“重做”。这是必要的,如果备份
稍后将对其应用增量更改。看到
有关详细信息的xtrabackup文档。
–apply-log
通过应用
位于名为“xtrabackup_logfile”的事务日志文件
在同一个目录中。另外,创建新的事务日志。
从文件中读取InnoDB配置
“backup-my.cnf”。
–copy-back
将所有文件复制到以前生成的备份中
将目录备份到它们的原始位置。