09-Mysql-全量+增量恢复数据

181 阅读3分钟

1.创建备份目录

 mkdir -p /mysql_3396_backup

2.全量备份的语法

mysqldump 命令用于备份 MySQL 数据库,具体参数的解释如下:

  • mysqldump: MySQL 的数据备份工具。
  • -uroot: 使用 root 用户进行连接。
  • -psummer66443: 使用密码 summer622226 (注意,-p 后没有空格)。
  • -F: 一定加这个参数 自动刷新所有binlog日志。
  • -A: 备份所有数据库。
  • --master-data=2: 在输出中包含二进制日志位置的注释,适用于主从复制。
  • --single-transaction: 以事务的方式进行备份,确保数据一致性,适用于 InnoDB 存储引擎。
  • -R: 包括存储过程和函数的备份。
  • -E: 包括事件的备份。
  • >: 将输出重定向到文件。
  • /mysql_3306_backup/full_db_$(date +%F).sql: 备份文件的存储路径和名称,$(date +%F) 会被替换为当前日期(格式为 YYYY-MM-DD)。

总结:这条命令备份所有数据库,包括存储过程、函数和事件,确保数据一致性,并将备份文件保存到指定路径,文件名中包含当前日期。

语法示例


mysqldump -uroot -psummer66655 -F -A --master-data=2 --single-transaction  -R -E > /mysql_3306_backup/full_db_$(date +%F).sql

展示所有日志

 mysql> show binary logs;
+----------------------+-----------+
| Log_name             | File_size |
+----------------------+-----------+
| mysql-log-bin.000001 |       535 |
| mysql-log-bin.000002 |      3262 |
| mysql-log-bin.000003 |      1680 |
| mysql-log-bin.000004 |       361 |
| mysql-log-bin.000005 |       375 |
| mysql-log-bin.000006 |       606 |
| mysql-log-bin.000007 |      1536 |
| mysql-log-bin.000008 |       194 |
+----------------------+-----------+
8 rows in set (0.00 sec)

模拟数据

  • 插入表数据
mysql> insert into test_table values(777),(888),(999);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> drop table test_table;
Query OK, 0 rows affected (0.03 sec)

mysql> show master status;
+----------------------+----------+--------------+------------------+------------------------------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+----------------------+----------+--------------+------------------+------------------------------------------+
| mysql-log-bin.000008 |      681 |              |                  | fc8cecdf-65e8-11ef-ba5e-525400f2542b:1-9 |
+----------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

常用命令

  • 解密查看binlog日志
mysqlbinlog --base64-output=decode-rows -vv /mysql_log/log_bin_3306/mysql-log-bin.000008 > tmp/chakan08.log
  • 基于GTID恢复binlog日志,导出sql文件,用于后面恢复
mysqlbinlog --skip-gtids --include-gtids='fc8cecdf-65e8-11ef-ba5e-525400f2542b:8' mysql-log-bin.000008 > /mysql_3306_backup/zengliangquanling.sql
  • 恢复binlog日志的时候,务必关闭 binlog记录,恢复完成之后
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)

  • 加载binlog恢复的日志
mysql> source /mysql_3306_backup/zengliangquanling.sql

  • 显示当前正在用的binlog
mysql> show master status;
  • 显示当前binlog 记录
mysql> show binlog events in  'mysql-log-bin.000008';

思路

1、关闭binlog记录

mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

2、导入全量数据

3、显示当前正在用的binlog

mysql> show master status;
+----------------------+----------+--------------+------------------+------------------------------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+----------------------+----------+--------------+------------------+------------------------------------------+
| mysql-log-bin.000008 |      681 |              |                  | fc8cecdf-65e8-11ef-ba5e-525400f2542b:1-9 |
+----------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

4、解密当前binlog,导出sql文件,

[root@VM-4-8-centos log_bin_3306]# mysqlbinlog --skip-gtids --include-gtids='fc8cecdf-65e8-11ef-ba5e-525400f2542b:8' mysql-log-bin.000008 > /mysql_3306_backup/zengliangquanling.sql

5、加载binlog恢复的日志

mysql> source /mysql_3306_backup/zengliangquanling.sql

6、开启binlog记录

mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)