mysql差异备份

差异备份基本原理

  1. 差异备份所基于的是最近一次的完整备份。这称为差异“基准**”。差异备份仅包括自建立差异基准后更改的数据。

差异备份的大小取决于自建立差异基准后更改的数据量。通常,差异基准越旧,新的差异备份就越大。特定的差异备份将在创建备份时捕获已更改的区的状态。如果创建一系列差异备份,则频繁更新的区可能在每个差异中包含不同的数据。当差异备份的大小增大时,还原差异备份会显著延长还原数据库所需的时间。因此,建议按设定的间隔执行新的完整备份,以便为数据建立新的差异基准。例如,您可以每周执行一次整个数据库的完整备份(即完整数据库备份),然后在该周内执行一系列常规的差异数据库备份。

  1. “差异数据库备份”,只记录自上次完整数据库备份后更改的数据。此完整备份称为“差异基准”。差异数据库备份比完整数据库备份更小、更快。这会缩短备份时间,但将增加复杂程度。对于大型数据库,差异备份的间隔可以比完整数据库备份的间隔更短。这将降低工作丢失风险。

如果数据库的某个子集比该数据库的其余部分修改得更为频繁,则差异数据库备份特别有用。在这些情况下,使用差异数据库备份,您可以频繁执行备份,并且不会产生完整数据库备份的开销。

ms181092.note(zh-cn,SQL.90).gif注意:
当创建差异基准时,即使只有一个文件组是只读的,读/写数据库的差异备份也能正常执行。对于读/写数据库的任何差异备份,数据库引擎都是在主文件中记录差异基准。

3.差异备份的理解:
差异备份是基于上一次完整的备份基础之上的,备份过程中能发现那些数据改变了,就对那些数据备份,例如,在数据库中,我对数据库进行了完整备份,然后我又在数据库中添加了一张表,然后又进行了差异备份,差异备份只会备份数据库完整备份以后更改的数据,即备份那张表,
4. 差异备份的优势:
差异备份的优势是速度,备份数据库所需的时间很少,但差异备份要求事先已执行过一次完整备份。
与完整备份相比,创建差异备份的速度可能非常快。 差异备份只记录自差异备份所基于的完整备份后更改的数据。 这有助于频繁地进行数据备份,减少数据丢失的风险。 但是,在还原差异备份之前,必须先还原其基准。
差异备份能够还原至某一个时间点

参考资料:
https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2005/ms181092(v%3Dsql.90)
https://blog.youkuaiyun.com/whaxrl/article/details/226599

差异备份基本步骤

  1. 开启mysql服务器的二进制日志功能
[root@jun ~]# vim /etc/my.cnf
[root@jun ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@jun ~]# tail -15 /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

server-id = 1   // 设置服务器标识
log-bin = mysql_bin  //开启二进制功能

[root@jun ~]# 


数据库日志文件
[root@jun ~]# ll /opt/data/
-rw-r-----. 1 mysql mysql      154 2月  22 16:37 mysql_bin.000001
-rw-r-----. 1 mysql mysql       19 2月  22 16:37 mysql_bin.index

  1. 对数据库进行完全备份
[root@jun ~]# mysqldump -uroot -pming123 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-201902221646
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@jun ~]# ls
1.sh  4.sh              anaconda-ks.cfg  f                     ming-201902211543.sql  v
2.sh  all-201902211529  create           httpd-2.4.37          mysql.pam              w
3.sh  all-201902221646  e                httpd-2.4.37.tar.bz2  table-201902211540
[root@jun ~]# 

  1. 在数据数据库中增加类容
//进入数据库查表
mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

//在student中增加内容
mysql> insert into student (id,name,age)values (12,'ming',22),(13,'wbk',22);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | ming        |   22 |
| 13 | wbk         |   22 |
+----+-------------+------+
13 rows in set (0.00 sec)


//更改13行的年龄
mysql> update student set age = 25 where id = 13
    -> ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | ming        |   22 |
| 13 | wbk         |   25 |
+----+-------------+------+
13 rows in set (0.00 sec)

  1. 模拟删库
mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ming               |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database ming;
Query OK, 2 rows affected (0.00 sec)

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

  1. 刷新二进制文件
[root@jun ~]# ll /opt/data/
总用量 188532
-rw-r-----. 1 mysql mysql       56 2月  20 18:13 auto.cnf
-rw-r-----. 1 mysql mysql      565 2月  22 16:37 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月  22 18:16 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  22 18:16 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 18:13 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  22 16:47 ibtmp1
-rw-r-----. 1 mysql mysql    50392 2月  22 16:37 jun.err
drwxr-x---. 2 mysql mysql     4096 2月  21 16:17 mysql
-rw-r-----. 1 mysql mysql     1272 2月  22 18:16 mysql_bin.000002
-rw-r-----. 1 mysql mysql       19 2月  22 16:47 mysql_bin.index
-rw-r-----. 1 mysql mysql        5 2月  22 16:37 mysql.pid
drwxr-x---. 2 mysql mysql     8192 2月  20 18:13 performance_schema
drwxr-x---. 2 mysql mysql     8192 2月  20 18:13 sys
drwxr-x---. 2 mysql mysql    12288 2月  22 15:56 zabbix
[root@jun ~]# mysqladmin -uroot -pming123 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@jun ~]# ll /opt/data
总用量 188536
-rw-r-----. 1 mysql mysql       56 2月  20 18:13 auto.cnf
-rw-r-----. 1 mysql mysql      565 2月  22 16:37 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月  22 18:16 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  22 18:16 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 18:13 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  22 16:47 ibtmp1
-rw-r-----. 1 mysql mysql    50392 2月  22 16:37 jun.err
drwxr-x---. 2 mysql mysql     4096 2月  21 16:17 mysql
-rw-r-----. 1 mysql mysql     1319 2月  22 18:19 mysql_bin.000002
-rw-r-----. 1 mysql mysql      154 2月  22 18:19 mysql_bin.000003
-rw-r-----. 1 mysql mysql       38 2月  22 18:19 mysql_bin.index
-rw-r-----. 1 mysql mysql        5 2月  22 16:37 mysql.pid
drwxr-x---. 2 mysql mysql     8192 2月  20 18:13 performance_schema
drwxr-x---. 2 mysql mysql     8192 2月  20 18:13 sys
drwxr-x---. 2 mysql mysql    12288 2月  22 15:56 zabbix
[root@jun ~]# 

  1. 恢复完全备份
[root@jun ~]# mysql -uroot -p  < all-201902221646 
Enter password: 
[root@jun ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ming               |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)

mysql> 


// 查看表发现没有内容
mysql> select * from student;
Empty set (0.00 sec)

mysql> 


  1. 查看binlog 日志,发现1180为删库前,所以恢复到1180
mysql> show binlog events in 'mysql_bin.000002'\G
*************************** 1. row ***************************
   Log_name: mysql_bin.000002
        Pos: 4
 Event_type: Format_desc
  Server_id: 1
End_log_pos: 123
       Info: Server ver: 5.7.23-log, Binlog ver: 4
*************************** 2. row ***************************
   Log_name: mysql_bin.000002
        Pos: 123
 Event_type: Previous_gtids
  Server_id: 1
End_log_pos: 154
       Info: 
*************************** 18. row ***************************
   Log_name: mysql_bin.000002
        Pos: 1115
 Event_type: Anonymous_Gtid
  Server_id: 1
End_log_pos: 1180
       Info: SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
*************************** 19. row ***************************
   Log_name: mysql_bin.000002
        Pos: 1180
 Event_type: Query
  Server_id: 1
End_log_pos: 1272
       Info: drop database ming
*************************** 20. row ***************************

//恢复到删库前

[root@jun ~]# mysql -uroot -p  < all-201902221646 
Enter password: 
[root@jun ~]# 
[root@jun ~]# 
[root@jun ~]# 
[root@jun ~]# mysqlbinlog --stop-position=1180 /opt/data/mysql_bin.000002 |mysql -uroot -pming123
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@jun ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@jun ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ming               |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)

mysql> use ming;
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 student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | ming        |   22 |
| 13 | wbk         |   25 |
+----+-------------+------+
13 rows in set (0.00 sec)


  1. 查看binlog日志,发现1028为改年龄前,所以恢复到1028
*************************** 14. row ***************************
   Log_name: mysql_bin.000002
        Pos: 902
 Event_type: Query
  Server_id: 1
End_log_pos: 974
       Info: BEGIN
*************************** 15. row ***************************
   Log_name: mysql_bin.000002
        Pos: 974
 Event_type: Table_map
  Server_id: 1
End_log_pos: 1028
       Info: table_id: 110 (ming.student)
*************************** 16. row ***************************
   Log_name: mysql_bin.000002
        Pos: 1028
 Event_type: Update_rows
  Server_id: 1
End_log_pos: 1084
       Info: table_id: 110 flags: STMT_END_F
*************************** 17. row ***************************
   Log_name: mysql_bin.000002
        Pos: 1084
 Event_type: Xid
  Server_id: 1
End_log_pos: 1115
       Info: COMMIT /* xid=2532 */
*************************** 18. row ***************************

//恢复到1028,更改年龄前
[root@jun ~]# mysqlbinlog --stop-position=1028 /opt/data/mysql_bin.000002 |mysql -uroot -pming123
mysql: [Warning] Using a password on the command line interface can be insecure.
WARNING: The range of printed events ends with a row event or a table map event that does not have the STMT_END_F flag set. This might be because the last statement was not fully written to the log, or because you are using a --stop-position or --stop-datetime that refers to an event in the middle of a statement. The event(s) from the partial statement have not been written to output.
[root@jun ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use ming
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 student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | ming        |   22 |
| 13 | wbk         |   25 |
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | ming        |   22 |
| 13 | wbk         |   22 |
+----+-------------+------+
26 rows in set (0.00 sec)

mysql> 


  1. 另一种查binglog方式
mysql> show binlog events in 'mysql_bin.000002';
+------------------+------+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+------+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000002 |    4 | Format_desc    |         1 |         123 | Server ver: 5.7.23-log, Binlog ver: 4 |
| mysql_bin.000002 |  123 | Previous_gtids |         1 |         154 |                                       |
| mysql_bin.000002 |  154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000002 |  219 | Query          |         1 |         291 | BEGIN                                 |
| mysql_bin.000002 |  291 | Table_map      |         1 |         345 | table_id: 110 (ming.student)          |

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值