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)

至此艺术大成

相关推荐
小小药2 分钟前
009-spring-bean的实例化流程
java·数据库·spring
DashVector25 分钟前
如何通过HTTP API插入或更新Doc
大数据·数据库·数据仓库·人工智能·http·数据库架构·向量检索
hac132228 分钟前
瀚高数据库
数据库
小小小妮子~1 小时前
深入理解 MySQL 架构
数据库·mysql·架构
man20171 小时前
【2024最新】基于Python+Mysql+django的水果销售系统Lw+PPT
数据库·mysql·django
度假的小鱼1 小时前
01 Oracle 基本操作
数据库·oracle
张声录11 小时前
【ETCD】【实操篇(十)】基于 ETCD 实现一个简单的服务注册及发现功能
数据库·etcd
张声录11 小时前
【ETCD】【实操篇(十八)】ETCD监控实战:提升系统健康与集群调试效率
数据库·chrome·etcd
程序员-小李2 小时前
餐厅下单助手系统(Java+MySQL)
java·开发语言·mysql
SelectDB技术团队2 小时前
Apache Doris 创始人:何为“现代化”的数据仓库?
大数据·数据库·数据仓库·数据分析·doris