############################## 第一步1.全库备份 ##########################################
[root@db01 data]# mysqldump -p --flush-logs --master-data=2 --all-databases > /tmp/all.sql
Enter password:
--flush-logs //备份时先将内存中日志写回磁盘,然后截断二进制日志,并产生新的日志文件
初始状态:
mysql> select * from t1;
+------+---------+----------+
| id | name | sal |
+------+---------+----------+
| 1 | harry | 12000.33 |
| 2 | tom | 15000.00 |
| 3 | harryyy | NULL |
+------+---------+----------+
查看完整备份文件中的字段
# vim /tmp/all.sql
...
--开始复制或时间点恢复的位置
-- Position to start replication or point-in-time recovery from
--将主日志文件更改为主日志文件 mysqld-bin.000003 pos点位置为120
-- CHANGE MASTER TO MASTER_LOG_FILE='mysqld-bin.000003', MASTER_LOG_POS=120;
--
-- Current Database: `db01`
--
...
############################## 第二步 2. 更新相关数据 ######################################
注释:更新的db02库中的t1表
mysql> insert into t1 values (4,'jack',2000);
Query OK, 1 row affected (0.01 sec)
--------------------------------误删除之前的状态-----------------------------------------
mysql> select * from t1;
+------+---------+----------+
| id | name | sal |
+------+---------+----------+
| 1 | harry | 12000.33 |
| 2 | tom | 15000.00 |
| 3 | harryyy | NULL |
| 4 | jack | 2000.00 |
+------+---------+----------+
4 rows in set (0.00 sec)
--------------------------------误删除之前的状态---------------------------------------
mysql> delete from t1 where id=3;
Query OK, 1 row affected (0.01 sec)
----------------------------------最新状态-------------------------------------------
mysql> select * from t1;
+------+-------+----------+
| id | name | sal |
+------+-------+----------+
| 1 | harry | 12000.33 |
| 2 | tom | 15000.00 |
| 4 | jack | 2000.00 |
+------+-------+----------+
3 rows in set (0.00 sec)
----------------------------------最新状态-------------------------------------------
[root@db01 data]# mysqlbinlog mysqld-bin.000003|less ---用mysqlbinlog工具查看这个二进制文件会看到刚刚所操作数据库的sql语句
----------------------mysqld-bin.000003二进制日志文件 开始------------------------------
# at 120 起始位置
#181012 19:06:00 server id 1 end_log_pos 199 CRC32 0xc4a7306e Query thread_id=9
exec_time=0 error_code=0
SET TIMESTAMP=1539342360/*!*/;
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/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET
@@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8
/*!*/;
SET @@session.lc_time_names=0/*!*/;
# at 199
#181012 19:06:00 server id 1 end_log_pos 310 CRC32 0x764ea2b3 Query thread_id=9
exec_time=0 error_code=0
use `db02`/*!*/;
SET TIMESTAMP=1539342360/*!*/;
insert into t1 values (4,'jack',2000) 正常的更新数据,需要恢复
/*!*/;
# at 310
#181012 19:06:00 server id 1 end_log_pos 341 CRC32 0xa5a0cfe4 Xid = 1103
COMMIT/*!*/;
# at 341 //insert语句的结束位置
#181012 19:06:32 server id 1 end_log_pos 420 CRC32 0xc2c81cd3 Query thread_id=9
exec_time=0 error_code=0
SET TIMESTAMP=1539342392/*!*/;
BEGIN
/*!*/;
# at 420
#181012 19:06:32 server id 1 end_log_pos 519 CRC32 0x7b3404e2 Query thread_id=9
exec_time=0 error_code=0
SET TIMESTAMP=1539342392/*!*/;
delete from t1 where id=3 误操作的数据,不需要恢复
/*!*/;
# at 519
#181012 19:06:32 server id 1 end_log_pos 550 CRC32 0x5bbc4007 Xid = 1105
COMMIT/*!*/;
-----------------------------mysqld-bin.000003.sql二进制日志文件 结尾--------------------------
############################## 第三步 3.备份二进制日志文件 ######################################
3.备份二进制文件
方法1:直接拷贝二进制物理文件
# cp mysqld-bin.000003 /tmp/
方法2:备份改变过的数据(SQL)
# mysqlbinlog --start-post=120 mysqld-bin.000003 > /tmp/mysqld-bin.000003.sql
--start-position=# 开始的位置(POS)
--stop-position=# 结束的位置
############################## 第四步 4. 模拟故障(删除库) ######################################
进入到数据库中,将3个业务库全部删掉
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db01 |
| db02 |
| myblog |
| mysql |
| performance_schema |
| test |
+--------------------+
7 rows in set (0.00 sec)
mysql> drop database db01;
Query OK, 7 rows affected (0.04 sec)
mysql> drop database db02;
Query OK, 1 row affected (0.01 sec)
mysql> drop database myblog;
Query OK, 12 rows affected (1.62 sec)
############################## 第五步 5. 还原全库 ######################################
5.还原
1)全库恢复
[root@db01 data]# mysql -p < /backup/all.sql
结果:当前数据库恢复到了11:05分的状态
2)增量恢复
######################### 5.1. 通过mysqld-bin.000003.sql恢复到(最新状态)#####################
进入mysql客户端
> source /tmp/mysqld-bin.000003.sql
最新状态,没有回复delete误操作,恢复数据成功
mysql> select * from t1;
+------+-------+----------+
| id | name | sal |
+------+-------+----------+
| 1 | harry | 12000.33 |
| 2 | tom | 15000.00 |
| 4 | jack | 2000.00 |
+------+-------+----------+
3 rows in set (0.00 sec)
最新状态,没有回复delete误操作
################################### end #######################################
------------------------------------------------------------------------------------
查看(读懂)二进制日志文件
----------------------mysqld-bin.000003.sql二进制日志文件 开始------------------------------
# at 120 起始位置
#181012 19:06:00 server id 1 end_log_pos 199 CRC32 0xc4a7306e Query thread_id=9
exec_time=0 error_code=0
SET TIMESTAMP=1539342360/*!*/;
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/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET
@@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8
/*!*/;
SET @@session.lc_time_names=0/*!*/;
# at 199
#181012 19:06:00 server id 1 end_log_pos 310 CRC32 0x764ea2b3 Query thread_id=9
exec_time=0 error_code=0
use `db02`/*!*/;
SET TIMESTAMP=1539342360/*!*/;
insert into t1 values (4,'jack',2000) 正常的更新数据,需要恢复
/*!*/;
# at 310
#181012 19:06:00 server id 1 end_log_pos 341 CRC32 0xa5a0cfe4 Xid = 1103
COMMIT/*!*/;
# at 341 //insert语句的结束位置
#181012 19:06:32 server id 1 end_log_pos 420 CRC32 0xc2c81cd3 Query thread_id=9
exec_time=0 error_code=0
SET TIMESTAMP=1539342392/*!*/;
BEGIN
/*!*/;
# at 420
#181012 19:06:32 server id 1 end_log_pos 519 CRC32 0x7b3404e2 Query thread_id=9
exec_time=0 error_code=0
SET TIMESTAMP=1539342392/*!*/;
delete from t1 where id=3 误操作的数据,不需要恢复
/*!*/;
# at 519
#181012 19:06:32 server id 1 end_log_pos 550 CRC32 0x5bbc4007 Xid = 1105
COMMIT/*!*/;
-----------------------------mysqld-bin.000003.sql二进制日志文件 结尾--------------------------
############################## 5.2. 通过mysqlbinlog 恢复到(误删除位置) ##########################
使用二进制日志文件恢复到我想要的状态
# mysqlbinlog --start-position=120 --stop-position=341 /tmp/mysqld-bin.000003|mysql -uroot -p
Enter password:
验证:
mysql> use db02;
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
mysql> select * from t1;
+------+---------+----------+
| id | name | sal |
+------+---------+----------+
| 1 | harry | 12000.33 |
| 2 | tom | 15000.00 |
| 3 | harryyy | NULL |
| 4 | jack | 2000.00 | //第4条记录产生说明恢复成功
+------+---------+----------+
4 rows in set (0.00 sec)
最新状态,没有回复delete误操作
############################### 数据备份恢复完成 ###########################################
网友评论