前言:增量备份与恢复的方法,可以在完全备份的基础上,减少备份的文件大小,加快备份和恢复的速度
mysqlbinlog [--no-defaults] 增量备份文件 | mysql -u 用户名 -p
//将某个起始时间的二进制日志导入数据库中,从而跳过某个发生错误的时间点实现数据恢复
恢复数据到指定位置:
mysqlbinlog --stop-position='操作id' 二进制日志 |mysql -u 用户名 -p 密码
从指定的位置开始恢复数据:
mysqlbinlog --start-position='操作id' 二进制日志 |mysql -u 用户名 -p 密码
//使用基于时间点的恢复,可能会出现在一个时间点里既同时存在正确的操作又存在错误的操作,所以需要更为精确的恢复方式
从日志开头截止到某个时间点的恢复:
mysqlbinlog [--no-defaults] --stop-datetime='年-月-日 小时:分钟:秒' 二进制日志 |mysql -u 用户名 -p 密码
从某个时间点到日志结尾的恢复:
mysqlbinlog [--no-defaults] --start-datetime='年-月-日 小时:分钟:秒' 二进制日志 |mysql -u 用户名 -p 密码
从某个时间点到某个时间点的恢复:
mysqlbinlog [--no-defaults] --start-datetime='年-月-日 小时:分钟:秒' --stop-datetime='年-月-日 小时:分钟:秒' 二进制日志 |mysql -u 用户名 -p 密码
[root@localhost ~]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data
[root@localhost ~]# ls /opt
mysql-2020-02-10.tar.xz mysql-5.7.17 rh
[root@localhost ~]# systemctl start mysqld //开启服务
[root@localhost ~]# vim /etc/my.cnf
//在[mysqld]项中加入配置 log-bin=mysql-bin
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
log-bin=mysql-bin //添加此句
...省略内容
[root@localhost ~]# systemctl restart mysqld //重启服务
[root@localhost ~]# ls /usr/local/mysql/data/
...省略内容
mysql-bin.000001 //生成了二进制文件,设置成功
[root@localhost data]# mysql -uroot -p
Enter password: //输入密码登陆
mysql> create database ku; //创建库
Query OK, 1 row affected (0.00 sec)
mysql> use ku; //进入库
Database changed
mysql> create table biao (id int(4) primary key,name char(10)); //创建表
Query OK, 0 rows affected (0.00 sec)
mysql> insert into biao values(1,'wangwu'),(2,'zhaoliu'); //插入记录
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from biao;
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 |zhaoliu |
+----+----------+
2 rows in set (0.00 sec)
mysql>exit
[root@localhost data]# mysqldump -uroot -p ku > /opt/ku.sql //进行完全备份
Enter password:
[root@localhost data]# mysqladmin -uroot -p flush-logs //进行增量备份
Enter password:
[root@localhost data]# cd /usr/local/mysql/data/
[root@localhost data]# ls
mysql-bin.000001 mysql-bin.000002
//刚刚备份的操作记录都存放在 mysql-bin.000001中, mysql-bin.000002是准备存放后续操作的增量拜备份
mysql> insert into biao values(3,'tianqi'); //正常操作
Query OK, 1 row affected (0.00 sec)
mysql> delete from biao where name='tianqi'; //失误操作
Query OK, 1 row affected (0.00 sec)
mysql> insert into biao values(4,'changzhi'); //正常操作
Query OK, 1 row affected (0.00 sec)
mysql> select * from biao;
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 | zhaoliu |
| 4 | changzhi |
+----+----------+
3 rows in set (0.00 sec)
mysql> exit
Bye
[root@localhost data]# mysqladmin -uroot -p flush-logs; //再次增量备份
Enter password:
[root@localhost data]# ls //发现再次生成新的增量备份文件ysql-bin.000003
mysql-bin.000001 mysql-bin.000002 mysql-bin.000003
//刚刚的正常操作和失误操作都存放到了mysql-bin.000002中
[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 > /opt/bak.txt
[root@localhost data]# vim /opt/bak.txt //查看之前的操作语句
...省略部分内容
# at 343 //正常操作语句段
#200108 0:29:18 server id 1 end_log_pos 390 CRC32 0x9ada4ac9 Write_rows: table id 219 flags: STMT_END_F
### INSERT INTO `ku`.`biao`
### SET
### @1=3
### @2='tianqi'
# at 390
。。省略部分内容
# at 558 //误操作语句段,558为误操作语句的开始位置id,200108 0:29:56 为误操作语句的开始位置时间,也是上一语句的结束时间
#200108 0:29:56 server id 1 end_log_pos 610 CRC32 0xb990c664 Table_map: `ku`.`biao` mapped to number 219
# at 610
#200108 0:29:56 server id 1 end_log_pos 657 CRC32 0x1738ac5d Delete_rows: table id 219 flags: STMT_END_F
### DELETE FROM `ku`.`biao`
### WHERE
### @1=3
### @2='tianqi'
# at 657
...省略内容
# at 825 //正常操作语句段,825为正常操作语句的开始位置id,200108 0:30:11 为正常操作语句的开始位置时间,也是上一语句的结束时间
#200108 0:30:11 server id 1 end_log_pos 877 CRC32 0x89f71cba Table_map: `ku`.`biao` mapped to number 219
# at 877
#200108 0:30:11 server id 1 end_log_pos 925 CRC32 0xae064160 Write_rows: table id 219 flags: STMT_END_F
### INSERT INTO `ku`.`biao`
### SET
### @1=4
### @2='changzhi'
# at 925
#200108 0:30:11 server id 1 end_log_pos 956 CRC32 0xe6213e68 Xid = 57
...省略部分内容
[root@localhost data]# mysql -uroot -p
Enter password:
mysql> drop table biao; //删除原有的表
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> source /opt/ku.sql; //恢复库
mysql> show tables; //查看表
+----------------+
| Tables_in_ku |
+----------------+
| biao | //恢复成功
+----------------+
1 row in set (0.00 sec)
mysql> select * from biao; //恢复成功
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 | zhaoliu |
+----+----------+
2 rows in set (0.00 sec)
[root@localhost data]# mysqlbinlog --no-defaults --stop-datetime='2020-01-08 0:29:56' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p
//第一个正常操作的结尾时间是2020-01-08 00:29:56,所以此处用此时间,表示恢复到此时间的操作
//时间在5查看增量备份文件中有写
Enter password:
[root@localhost data]# mysql -u root -p
Enter password:
mysql> use ku;
mysql> select * from biao; //查看表,恢复成功
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 | zhaoliu |
| 3 | tianqi |
+----+----------+
3 rows in set (0.00 sec)
[root@localhost data]# mysqlbinlog --no-defaults --start-datetime='2020-01-08 00:30:11' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p
//第二个失误操作,我们要跳过。第三个正常操作的结尾时间是2020-01-08 00:30:11,所以此处用此时间,表示从此时间恢复到结尾的操作
//时间在5查看增量备份文件中有写
Enter password:
[root@localhost data]# mysql -u root -p
Enter password:
mysql> use ku;
mysql> select * from biao; //查看表,恢复成功
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 | zhaoliu |
| 3 | tianqi |
| 4 | changzhi |
+----+----------+
4 rows in set (0.00 sec)
//先删除刚刚恢复的两个表记录
mysql> delete from biao where id=3;
Query OK, 1 row affected (0.01 sec)
mysql> delete from biao where id=4;
Query OK, 1 row affected (0.00 sec)
mysql> select * from biao;
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 | zhaoliu |
+----+----------+
2 rows in set (0.00 sec)
mysql> exit
[root@localhost data]# mysqlbinlog --no-defaults --stop-position='558' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p
//错误操作真正开始的位置是558,而不是610,所以要恢复到558位置结束
//位置在5查看增量备份文件中有写
Enter password:
[root@localhost data]# mysql -u root -p
Enter password:
mysql> use ku;
mysql> select * from biao; //查看表,恢复成功
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 | zhaoliu |
| 3 | tianqi |
+----+----------+
3 rows in set (0.00 sec)
[root@localhost data]# mysqlbinlog --no-defaults --start-position='825' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p
//正常操作真正开始的位置是825,而不是877,所以要恢复到825位置结束
//位置在5查看增量备份文件中有写
Enter password:
[root@localhost data]# mysql -u root -p
Enter password:
mysql> use yiku;
mysql> select * from yibiao; //查看表,恢复成功
+----+----------+
| id | name |
+----+----------+
| 1 | wangwu |
| 2 | zhaoliu |
| 3 | tianqi |
| 4 | changzhi |
+----+----------+
4 rows in set (0.00 sec)