Linux—MySQL增量备份与恢复
一.增量备份与恢复
1.增量备份的优点
没有重复数据,备份量不大,时间短;
2.缺点
需要上次完全备份以及完全备份之后的所有增量备份才能恢复,而且对所有增量备份进行逐个反推恢复,操作较为繁琐
3.MySQL没有直接提供增量备份的方法,但是通过MySQL的二进制日志间接实现增量备份
4.二进制日志保存了所有更新或者可能更新或者可能更新数据库的操作
5.二进制日志在启动MySQL服务器后开始记录,并且在文件达到max_binlog_size所设置的大小或者接收到的flush logs命令之后重新创建日志文件
6.只需要定时执行flush logs 方法重新创建新的日志,生成日志文件序列,并及时把这些日志保存到安全的地方就完成了一个时间段的增量备份
二.具体操作
1.开启二进制日志功能,重启服务(mysqld里添加)
[root@localhost opt]# vim /etc/my.cnf
log-bin=mysql-bin
[root@localhost opt]# systemctl restart mysqld.service
2.使用mysqldump工具进行完全备份
[root@localhost data]# mysqldump -uroot -pabc123 school > /opt/school.sql
[root@localhost data]# ls /opt
mysql-2020-01-09.tar.xz mysql-5.7.20 rh school.sql
3.创建增量备份支持的文件
[root@localhost data]# mysqladmin -uroot -pabc123 flush-logs
4.进行误操作
[root@localhost data]# mysql -u root -pabc123
mysql> use school;
mysql> select * from info;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | stu01 | 88.0 |
| 2 | stu02 | 77.0 |
+----+-------+-------+
mysql> insert into info (name,score) values ('test01',66);
mysql> select * from info;
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 1 | stu01 | 88.0 |
| 2 | stu02 | 77.0 |
| 3 | test01 | 66.0 |
+----+--------+-------+
mysql> delete from info where name='stu01';
mysql> insert into info (name,score) values ('test02',99);
mysql> select * from info;
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 2 | stu02 | 77.0 |
| 3 | test01 | 66.0 |
| 4 | test02 | 99.0 |
+----+--------+-------+
5.查看记录:
[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 > /opt/bak.txt
6.基于完全备份进行恢复:
[root@localhost opt]# mysql -u root -pabc123
mysql> use school;
mysql> drop table info;
mysql> source /opt/school.sql
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| info |
+------------------+
mysql> select * from info;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | stu01 | 88.0 |
| 2 | stu02 | 77.0 |
+----+-------+-------+
基于时间:
[root@localhost opt]# mysqlbinlog --no-defaults --stop-datetime='2020-01-09 19:48:35' /usr/local/mysql-bin.000002 | mysql -u root -p
[root@localhost opt]# mysqlbinlog --no-defaults --start-datetime='2020-01-09 19:50:25' /usr/local/mysql-bin.000002 | mysql -u root -p
基于位置:
[root@localhost opt]# mysqlbinlog --no-defaults --stop-postion='443' /usr/local/mysql-bin.000002 | mysql -u root -p
[root@localhost opt]# mysqlbinlog --no-defaults --start-postion='456' /usr/local/mysql-bin.000002 | mysql -u root -p
[root@localhost data]# mysql -u root -pabc123
mysql> use school;
mysql> select * from info;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | stu01 | 88.0 |
| 2 | stu02 | 77.0 |
| 3 | test01 | 66.0 |
| 4 | test02 | 99.0 |
+----+--------+-------+
来源:CSDN
作者:obsessiveY
链接:https://blog.csdn.net/obsessiveY/article/details/103918420