一、增量备份
原理:Mysql中没有直接提供增量本分的方法,但可以通过二进制文件间接的进行增量备份。
操作:第一步先开启二进制文件
[root@localhost data]# vim /etc/my.cnf [mysqld] log-bin=mysql_bin[root@localhost data]# systemctl restart mysqld.service [root@localhost data]# ls auto.cnf ibdata1 ib_logfile1 mysql mysql_bin.index performance_schema sys ib_buffer_pool ib_logfile0 ibtmp1 mysql_bin.000001 mysql-slow_query.log school
此时查看 /usr/local/mysql/data 中存在二进制文件mysql_bin.000001
查看二进制文件mysql_bin.000001
[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql_bin.000001
生成新的二进制文件mysql_bin.000002
[root@localhost data]# mysqladmin -uroot -p flush-logs Enter password: [root@localhost data]# ls auto.cnf ibdata1 ib_logfile1 mysql mysql_bin.000002 mysql-slow_query.log school ib_buffer_pool ib_logfile0 ibtmp1 mysql_bin.000001 mysql_bin.index performance_schema sys
二、增量备份的恢复
首先,在生成二进制文件mysql_bin.000002 后,我们进去mysql做一些简单的操作来完成这个恢复的实验。
[root@localhost data]# mysql -uroot -p mysql> create database myschool; Query OK, 1 row affected (0.01 sec) mysql> use myschool;Database changed mysql> create table info(id int not null primary key auto_increment,name char(10),age int(2)); Query OK, 0 rows affected (0.38 sec) mysql> insert into info(name,age) values('tom',21); Query OK, 1 row affected (0.01 sec) mysql> insert into info(name,age) values('jerry',22); Query OK, 1 row affected (0.00 sec) mysql> select * from info;+----+-------+------+| id | name | age |+----+-------+------+| 1 | tom | 21 || 2 | jerry | 22 |+----+-------+------+2 rows in set (0.00 sec)
然后,我们执行刷新命令生成新的二进制文件mysql_bin.000003,以便后续删除库后进行恢复操作
[root@localhost data]# mysqladmin -uroot -p flush-logs[root@localhost data]# mysql -uroot -p mysql> use myschool; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> drop ; Query OK, 0 rows affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
此时,数据库中的库mychool已经不存在了,最后,我们执行恢复操作
[root@localhost data]# mysqlbinlog --no-defaults mysql_bin.000002 | mysql -uroot -pEnter password: [root@localhost data]# mysql -uroot -p mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || myschool || mysql || performance_schema || school || sys |+--------------------+6 rows in set (0.00 sec)
myschool数据库已完成恢复
三、增量备份的异常操作恢复
在上面操作中,如果我们没有执行刷新命令生成新的二进制文件mysql_bin.000003,直接mysql> drop database myschool;时,再进行上述恢复操作是无法恢复数据库myschool的,这是我们就需要通过跳过异常操作来进行恢复。
跳过异常操作有两种方法:
①第一种是基于时间点
首先进入mysql删除myschool数据库后查看二进制文件mysql_bin.000003
[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql_bin.000003# at 1523#180903 16:54:28 server id 1 end_log_pos 1627 CRC32 0x9637c595 Query thread_id=11 exec_time=0 error_code=0SET TIMESTAMP=1535964868/*!*/;SET @@session.sql_mode=1437073414/*!*/;drop database myschool/*!*/;SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/; DELIMITER ;
找到在16:54:28时进行的删除命令,执行以下操作进行恢复到错误操作之前
[root@localhost data]# mysqlbinlog --no-defaults --stop-datetime='201-09-03 16:54:28' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -pEnter password:
然后在执行以下操作从正确操作点继续开始恢复
[root@localhost data]# mysqlbinlog --no-defaults --start-datetime='201-09-03 16:55:10' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -pEnter password:
②基于位置点
如上所示:# at 1523为异常位置起始点, 执行以下操作进行恢复到错误操作之前
[root@localhost data]# mysqlbinlog --no-defaults --stop-postion='1523' /usr/local/mysql/data/mysql-bin.000003 | mysql -u root -pEnter password:
然后在执行以下操作从正确操作点继续开始恢复
[root@localhost data]# mysqlbinlog --no-defaults --start-postion='1555' /usr/local/mysql/data/mysql-bin.000003 | mysql -u root -pEnter password: