参考文献: https://www.cnblogs.com/clsn/p/8138015.html#_label2
新建数据库和数据(全量恢复)
drop database clsn;
CREATE DATABASE clsn;
USE `clsn`;
CREATE TABLE `test` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
INSERT INTO `test` VALUES (1,'clsn'),(2,'znix'),(3,'inca'),(4,'zuma'),(5,'kaka');
查看数据
SELECT * FROM `clsn`.`test` LIMIT 0, 1000
全量备份sql
生成的clsn_2022-04-07.sql.gz是clsn的全量数据,数据截止到(5,‘kaka’)
mysqldump -uroot -p'123456' -B --master-data=2 --single-transaction clsn|gzip>/home/mysqlbak/clsn_$(date +%F).sql.gz
新增数据(增量恢复)
insert into test values(6,'haha');
insert into test values(7,'hehe');
查看数据
SELECT * FROM `clsn`.`test` LIMIT 0, 1000
发现数据有问题,禁用数据库准备恢复 采用iptables防火墙屏蔽所有应用程序的写入
firewall-cmd --zone=public --remove-port=3306/tcp --permanent
firewall-cmd --reload
或采用mysql 配置参数,但是需要重启数据库
skip-networking
service mysqld restart
删除数据库
drop database clsn;
恢复
误删数据库第一时间发现立即去找最后一个binlog文件,查看第23行binlog的位置。
zcat clsn_2022-04-07.sql.gz >clsn_2022-04-07.sql
[root@129 mysqlbak]
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000026', MASTER_LOG_POS=1025;
mysqlbinlog -d clsn --start-position=1025 mysql-bin.000026 -r bin.sql
打开bin.sql文件,第78行是删除库的语句 第15行 # at 1025 第75行 # at 1706(避开第78行的删除语句)
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
ROLLBACK/*!*/;
BINLOG '
peBOYg+BAAAAegAAAH4AAAABAAQAOC4wLjI4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACl4E5iEwANAAgAAAAABAAEAAAAYgAEGggAAAAICAgCAAAACgoKKioAEjQA
CigAAcVeR9E=
'/*!*/;
/*!80001 SET @@session.original_commit_timestamp=1649338055258785*//*!*/;
/*!80014 SET @@session.original_server_version=80028*//*!*/;
/*!80014 SET @@session.immediate_server_version=80028*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
SET TIMESTAMP=1649338055/*!*/;
SET @@session.pseudo_thread_id=25/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113696/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=255,@@session.collation_connection=255,@@session.collation_server=255/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
/*!80011 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
BEGIN
/*!*/;
use `clsn`/*!*/;
SET TIMESTAMP=1649338055/*!*/;
insert into test values(6,'haha')
/*!*/;
COMMIT/*!*/;
/*!80001 SET @@session.original_commit_timestamp=1649338067910695*//*!*/;
/*!80014 SET @@session.original_server_version=80028*//*!*/;
/*!80014 SET @@session.immediate_server_version=80028*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
SET TIMESTAMP=1649338067/*!*/;
BEGIN
/*!*/;
SET TIMESTAMP=1649338067/*!*/;
insert into test values(7,'hehe')
/*!*/;
COMMIT/*!*/;
/*!80001 SET @@session.original_commit_timestamp=1649338922066652*//*!*/;
/*!80014 SET @@session.original_server_version=80028*//*!*/;
/*!80014 SET @@session.immediate_server_version=80028*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
SET TIMESTAMP=1649338922/*!*/;
DROP DATABASE `clsn`
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
全量恢复
mysql -uroot -p'123456' < clsn_2022-04-07.sql
增量恢复
根据position位置 第15行 # at 1025 第75行 # at 1706
mysqlbinlog --start-position=1025 --stop-position=1706 mysql-bin.000026 | mysql -uroot -p'123456'
到此以恢复全部数据,这里是特殊场景,实际应用灵活变通。
|