mysqldunp增量恢復過程及詳解


Mysql增量恢復必備條件mysql

1.開啓mysql log-bin日誌功能sql

MySQL數據庫開啓了log-bin 參數記錄binlog日誌功能下:數據庫

小結:增量恢復的條件:vim

存在一份全備加上全備以後的時刻到出問題時刻的全部增量binlog文件備份。ide

建立模擬環境ui

[root@mysql-db02 3306]# grep log-bin /data/3306/my.cnfspa

log-bin = /data/3306/mysql-bin日誌

[root@mysql-db02 3306]# orm

mkdir -p /server/backup/server

備份全量數據數據

[root@mysql-db02 backup]# mysqldump -S /data/3306/mysql.sock -B -F -R -x --master-data=2  

test |gzip >/server/backup/test_$(date +%F).sql.gz

(Xx區別:X爲導出xml格式文件  x鎖表,筆者在這坑了好久,告請你們值得注意)

[root@mysql-db02 backup]# ls

test_2017-05-23.sql.gz

[root@mysql-db02 backup]#

插入新增數據數據 

mysql> select *  from cc;

+----+------+

| id | name |

+----+------+

|  1 | cc1  |

|  2 | cc2  |

+----+------+

2 rows in set (0.00 sec)

 

mysql> insert into cc values(3,'xuexue');

Query OK, 1 row affected (0.00 sec)

 

mysql> insert into cc values(4,'chenpeng');

Query OK, 1 row affected (0.00 sec)

 

mysql> insert into cc values(5,'zhangya');

Query OK, 1 row affected (0.00 sec)

 

mysql> select *  from cc;

+----+----------+

| id | name     |

+----+----------+

|  1 | cc1      |

|  2 | cc2      |

|  3 | xuexue   |

|  4 | chenpeng |

|  5 | zhangya  |

+----+----------+

5 rows in set (0.00 sec)

 

mysql> quit

 

模擬更改時間刪除數據

[root@mysql-db02 backup]# date -s '2017/5/23 10:00:00'

Tue May 23 10:00:00 CST 2017

[root@mysql-db02 backup]# mysql -S /data/3306/mysql.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 23

Server version: 5.6.34-log Source distribution

 

Copyright (c) 2000, 2016, 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> drop database test;

Query OK, 1 row affected (0.01 sec)

mysql> show  databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| cc2                |

| mysql              |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

 

mysql> quit

發現錯誤查看binlog文件 若是不知binlog文件是哪一個 可採起如下措施

[root@mysql-db02 backup]# gzip -d test_2017-05-23.sql.gz

[root@mysql-db02 backup]# grep CHANGE test_2017-05-23.sql.gz

[root@mysql-db02 backup]# grep CHANGE test_2017-05-23.sql

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000027', MASTER_LOG_POS=120;

[root@mysql-db02 backup]#

故障以後必定要將binlog拿走 要不會寫入增量備份的sql

增量備份條件須要全備與增備 條件具有才能夠玩 將增量備份拷入backup目錄下:

[root@mysql-db02 backup]# cp /data/3306/mysql-bin.000027 .

[root@mysql-db02 backup]# ls

mysql-bin.000027  test_2017-05-23.sql  

因爲數據庫沒了  新生成的binlog也不會寫入  能夠對新生成的binlog不進行關注

生成binlog備份 注意binlog順序

[root@mysql-db02 backup]# mysqlbinlog -d test mysql-bin.000027 >27bin.sql (若有不少能夠列在後面,太多的話能夠採用匹配的方式)

[root@mysql-db02 backup]# ls

27bin.sql         test_2017-05-23.sql

mysql-bin.000027  test_2017-05-24.sql.gz

[root@mysql-db02 backup]#

[root@mysql-db02 backup]# vim 27bin.sql

[root@mysql-db02 backup]#

刪除裏面drop語句 若是不刪除  又恢復到刪除前狀態

 

恢復過程

先恢復全備

mysql -uroot -poldboy123 -S /data/3306/mysql.sock <test_2017-05-23.sql

mysql> select *  from cc;

+----+------+

| id | name |

+----+------+

|  1 | cc1  |

|  2 | cc2  |

+----+------+

2 rows in set (0.00 sec)

再恢復增倍

mysql -uroot -poldboy123 -S /data/3306/mysql.sock test <mysql-bin.000027

mysql> select *  from cc;

+----+----------+

| id | name     |

+----+----------+

|  1 | cc1      |

|  2 | cc2      |

|  3 | xuexue   |

|  4 | chenpeng |

|  5 | zhangya  |

+----+----------+

5 rows in set (0.00 sec)

至此增量備份完成

 

Mysqldump重要參數!

-B 導出建表語句 備份多個庫

-A 備份全部的數據庫

--master-data={1|2} 添加一條語句,change master

刷新binlog

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=1137;

 

single-transaction 在備份時間 會增長一個新的進程  將備份期間寫入庫的內容放到新日誌裏,備份結束後再寫入

innodb的引擎支持

相關文章
相關標籤/搜索