1、创建库表和插入数据内容
MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> create database aaa;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use aaa;
Database changed
MariaDB [aaa]> create table student(id int(10),name varchar(20),age int (10));
Query OK, 0 rows affected (0.00 sec)
MariaDB [aaa]> insert into student values(1,'xiaoming',10),(2,'xiaohong',12),(3,'xiaodong',14);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [aaa]> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | xiaoming | 10 |
| 2 | xiaohong | 12 |
| 3 | xiaodong | 14 |
+------+----------+------+
3 rows in set (0.00 sec)
2)备份数据库并且切割日志文件
[root@jyy ~]#
[root@jyy ~]# mysqldump -uroot -p -F -A > all_bak.sql
Enter password:
[root@jyy ~]#
[root@jyy ~]#
[root@jyy ~]# ll all_bak.sql
-rw-r--r-- 1 root root 515902 Jan 22 20:36 all_bak.sql
[root@jyy ~]#
3)插入新数据后删除数据库模拟误操作
MariaDB [(none)]> use aaa;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [aaa]> insert into student values(4,'xiaojiang',19),(5,'xiaoyi',20),(6,'xiaoyang',21);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [aaa]> select * from student;
+------+-----------+------+
| id | name | age |
+------+-----------+------+
| 1 | xiaoming | 10 |
| 2 | xiaohong | 12 |
| 3 | xiaodong | 14 |
| 4 | xiaojiang | 19 |
| 5 | xiaoyi | 20 |
| 6 | xiaoyang | 21 |
+------+-----------+------+
6 rows in set (0.00 sec)
MariaDB [aaa]> drop database aaa;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
此时新插入的数据在全量备份的文件中并不存在,此时我们需要利用MySQL的binlog来进行增量数据的恢复
4)分析最新binlog日志并生成sql文件
[root@jyy mysql]#
oot@jyy mysql]# mysqlbinlog mysql-bin.000006 > binlog.sql
▽root@jyy mysql]# vim binlog.sql
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#190122 20:36:44 server id 1 end_log_pos 245 Start: binlog v 4, server v 5.5.41-MariaDB-log created 190122 20:36:44
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
XA5HXA8BAAAA8QAAAPUAAAABAAQANS41LjQxLU1hcmlhREItbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAaUgn8A==
'/*!*/;
# at 245
#190122 20:40:33 server id 1 end_log_pos 312 Query thread_id=9 exec_time=0 error_code=0
SET TIMESTAMP=1548160833/*!*/;
SET @@session.pseudo_thread_id=9/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!
*/;
:/drop
/*!*/;
# at 312
#190122 20:40:33 server id 1 end_log_pos 452 Query thread_id=9 exec_time=0 error_code=0
use `aaa`/*!*/;
SET TIMESTAMP=1548160833/*!*/;
insert into student values(4,'xiaojiang',19),(5,'xiaoyi',20),(6,'xiaoyang',21)
/*!*/;
# at 452
#190122 20:40:33 server id 1 end_log_pos 479 Xid = 678
COMMIT/*!*/;
# at 479
#190122 20:41:01 server id 1 end_log_pos 558 Query thread_id=9 exec_time=0 error_code=0
SET TIMESTAMP=1548160861/*!*/;
drop database aaa //删除此行drop语句,不然导入数据不会生效
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
5)开始导入全量备份文件和增量备份文件进行数据恢复
[root@jyy mysql]# cp binlog.sql /root/
[root@jyy mysql]# cd
[root@jyy ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.41-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> system ls
all_bak.sql binlog.sql Documents initial-setup-ks.cfg Pictures Templates
anaconda-ks.cfg Desktop Downloads Music Public Videos
MariaDB [(none)]>
MariaDB [(none)]> source all_bak.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)
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> source binlog.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)
Query OK, 0 rows affected (0.00 sec)
MariaDB [aaa]>
MariaDB [aaa]>
MariaDB [aaa]> select * from aaa.student;
+------+-----------+------+
| id | name | age |
+------+-----------+------+
| 1 | xiaoming | 10 |
| 2 | xiaohong | 12 |
| 3 | xiaodong | 14 |
| 4 | xiaojiang | 19 |
| 5 | xiaoyi | 20 |
| 6 | xiaoyang | 21 |
+------+-----------+------+
6 rows in set (0.00 sec) //数据恢复成功!!!
MariaDB [aaa]>