Mysql的备份还原

模拟环境准备

创建一个名为school的数据库,创建一个名为Stuent的学生信息表

mysql> create database school;
Query OK, 1 row affected (0.00 sec)

mysql> use school;
Database changed
mysql> CREATE TABLE `Student` (
    -> `Sno` int(10) NOT NULL COMMENT '学号',  `Sname` varchar(16) NOT NULL COMMENT '姓名',
    -> `Ssex` char(2) NOT NULL COMMENT '性别',  `Sage` tinyint(2) NOT NULL DEFAULT '0' COMMENT '学生年龄',
    -> `Sdept` varchar(16) DEFAULT 'NULL' COMMENT '学生所在系别',  PRIMARY KEY (`Sno`)
    -> ) ;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> INSERT INTO `Student` VALUES (1, '陆亚', '男', 24, '计算机网络'),(2, 'tom', '男', 26, '英语'),(3, '张阳', '男', 21, '物流管理'), (4, 'alex', '女', 22, '电 子商务');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

初始内容如下

完全备份

使用mysqldump命令进行完全备份

[root@openEuler-node3 mysqlbak]# mysqldump -u"root" -p"Root" --opt -B school > school.sql
# 其中 root和Root 分别为账号密码的意思,这样直接把密码写在命令行会警告行为不安全,实际应用中最好直接用-p

模拟增量备份环境

在Student表中插入数据,并把school库删除

mysql> INSERT INTO Student values(0005,'xumubin','男',29,'中文专业'),(0006,'wangzhao','男',21,'导弹专业');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql>  drop database school;
Query OK, 1 row affected (0.01 sec)

做完这些就立刻刷新日志,来保存日志文件,并将日志文件备份防止二次破坏

mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)

mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       501 | No        |
| binlog.000002 |      1816 | No        |
| binlog.000003 |       157 | No        |
+---------------+-----------+-----------+
3 rows in set (0.00 sec)


mysql> system cp /var/lib/mysql/binlog.000001 /mysqlbak

查看二进制日志获得时间点和位置点

#注意5.7版本,insert语句已经加密,默认看不到,查看时加上选项 --base64-output=DECODE-ROWS -vv

[root@openEuler-node3 mysqlbak]# mysqlbinlog binlog.000001 --base64-output=DECODE-ROWS -vv

BEGIN
/*!*/;
# at 1386
#240227 19:52:53 server id 1  end_log_pos 1456 CRC32 0xfa30a0c3 	Table_map: `school`.`Student` mapped to number 90
# has_generated_invisible_primary_key=0
# at 1456
#240227 19:52:53 server id 1  end_log_pos 1554 CRC32 0xf298ba46 	Write_rows: table id 90 flags: STMT_END_F
### INSERT INTO `school`.`Student`
### SET
###   @1=5 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xumubin' /* VARSTRING(64) meta=64 nullable=0 is_null=0 */
###   @3='男' /* STRING(8) meta=65032 nullable=0 is_null=0 */
###   @4=29 /* TINYINT meta=0 nullable=0 is_null=0 */
###   @5='中文专业' /* VARSTRING(64) meta=64 nullable=1 is_null=0 */
### INSERT INTO `school`.`Student`
### SET
###   @1=6 /* INT meta=0 nullable=0 is_null=0 */
###   @2='wangzhao' /* VARSTRING(64) meta=64 nullable=0 is_null=0 */
###   @3='男' /* STRING(8) meta=65032 nullable=0 is_null=0 */
###   @4=21 /* TINYINT meta=0 nullable=0 is_null=0 */
###   @5='导弹专业' /* VARSTRING(64) meta=64 nullable=1 is_null=0 */
# at 1554
#240227 19:52:53 server id 1  end_log_pos 1585 CRC32 0xb5de1e01 	Xid = 43
COMMIT/*!*/;
# at 1585
#240227 19:53:01 server id 1  end_log_pos 1662 CRC32 0xafbdf1bc 	Anonymous_GTID	last_committed=4	sequence_number=5	rbr_only=no	original_committed_timestamp=1709034781101072	immediate_commit_timestamp=1709034781101072	transaction_length=187

# 我只截取了日志中增量部分方便截取时间点和位置点

1.基于时间点回复

查看二进制文件02,并截取需要增量备份的起止时间,用mysqlbinlog完成时间点增量回复

[root@openEuler-node3 mysqlbak]# mysqlbinlog binlog.000002 --start-datetime="2024-02-27 19:52:53" --stop-datetime="2024-02-27 19:53:01" -r time1.sql

先完全备份

mysql> source /mysqlbak/school.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)

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, 1 row affected (0.00 sec)

Database changed
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.01 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, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

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.01 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)

Query OK, 0 rows affected (0.00 sec)

mysql> select * from school.Student;
+-----+--------+------+------+-----------------+
| Sno | Sname  | Ssex | Sage | Sdept           |
+-----+--------+------+------+-----------------+
|   1 | 陆亚   | 男   |   24 | 计算机网络      |
|   2 | tom    | 男   |   26 | 英语            |
|   3 | 张阳   | 男   |   21 | 物流管理        |
|   4 | alex   | 女   |   22 | 电子商务        |
+-----+--------+------+------+-----------------+
4 rows in set (0.00 sec)

还原增量备份

mysql> source /mysqlbak/time1.sql
Query OK, 0 rows affected, 1 warning (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)

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)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Charset changed
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, 1 warning (0.00 sec)

Query OK, 0 rows affected, 1 warning (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.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select * from school.Student;
+-----+----------+------+------+-----------------+
| Sno | Sname    | Ssex | Sage | Sdept           |
+-----+----------+------+------+-----------------+
|   1 | 陆亚     | 男   |   24 | 计算机网络      |
|   2 | tom      | 男   |   26 | 英语            |
|   3 | 张阳     | 男   |   21 | 物流管理        |
|   4 | alex     | 女   |   22 | 电子商务        |
|   5 | xumubin  | 男   |   29 | 中文专业        |
|   6 | wangzhao | 男   |   21 | 导弹专业        |
+-----+----------+------+------+-----------------+
6 rows in set (0.00 sec)

表的内容完整,回复完成

2.基于位置点恢复

准备工作:先把库删了

然后用mysqlbiblog --start-postions 命令进行位置点回复

[root@openEuler-node3 mysqlbak]# mysqlbinlog binlog.000002 --start-position=1386 --stop-position=1585 -r pos1.sql

先在数据库回复完全备份,再回复增量备份

mysql>  drop database school;
Query OK, 1 row affected (0.01 sec)

mysql> source /mysqlbak/school.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)

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, 1 row affected (0.00 sec)

Database changed
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.01 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, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

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.01 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)

mysql> select * from school.Student;
+-----+--------+------+------+-----------------+
| Sno | Sname  | Ssex | Sage | Sdept           |
+-----+--------+------+------+-----------------+
|   1 | 陆亚   | 男   |   24 | 计算机网络      |
|   2 | tom    | 男   |   26 | 英语            |
|   3 | 张阳   | 男   |   21 | 物流管理        |
|   4 | alex   | 女   |   22 | 电子商务        |
+-----+--------+------+------+-----------------+
4 rows in set (0.00 sec)

mysql> source /mysqlbak/pos1.sql
Query OK, 0 rows affected, 1 warning (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.01 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, 1 warning (0.00 sec)

mysql> select * from school.Student;
+-----+----------+------+------+-----------------+
| Sno | Sname    | Ssex | Sage | Sdept           |
+-----+----------+------+------+-----------------+
|   1 | 陆亚     | 男   |   24 | 计算机网络      |
|   2 | tom      | 男   |   26 | 英语            |
|   3 | 张阳     | 男   |   21 | 物流管理        |
|   4 | alex     | 女   |   22 | 电子商务        |
|   5 | xumubin  | 男   |   29 | 中文专业        |
|   6 | wangzhao | 男   |   21 | 导弹专业        |
+-----+----------+------+------+-----------------+
6 rows in set (0.00 sec)

至此艺术大成

相关推荐
读心悦1 小时前
修改 MySQL 数据库中的唯一键
数据库·mysql
卓琢1 小时前
(九)Shell 脚本(四):正则表达式、sed 和 awk 详解
linux·mysql·正则表达式
qq_213157891 小时前
(c#)unity中sqlite多线程同时开启事务会导致非常慢
数据库·sqlite·c#
北极无雪1 小时前
Spring源码学习(拓展篇):SpringMVC中的异常处理
java·开发语言·数据库·学习·spring·servlet
666xiaoniuzi2 小时前
深入理解 C 语言中的内存操作函数:memcpy、memmove、memset 和 memcmp
android·c语言·数据库
正在走向自律2 小时前
3.使用条件语句编写存储过程(3/10)
数据库·存储过程·安全架构
YONG823_API2 小时前
电商平台数据批量获取自动抓取的实现方法分享(API)
java·大数据·开发语言·数据库·爬虫·网络爬虫
小小不董2 小时前
图文深入理解Oracle DB Scheduler
linux·运维·服务器·数据库·oracle
大拇指的约定3 小时前
数据库(MySQL):使用命令从零开始在Navicat创建一个数据库及其数据表(三),单表查询
数据库·mysql·oracle
阳光阿盖尔3 小时前
redis——哨兵机制
数据库·redis·缓存·主从复制·哨兵