Mysql數據庫的備份與恢復

Mysql數據庫的備份與恢復

數據備份的重要性

在生產環境中,數據的安全性是至關重要的,任何數據的丟失都可能產生嚴重的後果
造成數據丟失的原因
程序錯誤
人爲錯誤(常事)
計算機失敗
磁盤失敗
災難

數據庫備份的分類

物理備份
對數據庫操作系統的物理文件(如數據文件,日誌文件等)的本份
物理備份又可以分爲脫機備份(冷備份)和聯機備份(熱備份)
冷備份:實在關閉數據庫的時候進行的
熱備份:數據庫處於運行狀態,這種備份方法依賴於數據庫的日誌文件
邏輯備份:對數據庫邏輯組件(如表等數據庫對象)的備份
從數據庫的備份策略角度,備份可分爲
完全備份:每次對數據進行完整的備份
差異備份:備份那些自從上次完全備份之後被修改的文件
增量備份:只有那些在上次完全備份或者增量備份後被修改的文件纔會被備份
增量備份,第一點在完全備份基礎之上,a,b,c增量修改之後會就b+,c+增量備份。在這兩個文件增量備份之後,再備份修改,b+b+

完全備份

完全備份是對整個數據庫的備份,數據庫結構和文件結構的備份
完全備份保存的是備份完成時刻的數據庫
完全備份是增量備份的基礎
完全備份的優缺點
優點:備份與恢復操作簡單方便
缺點:數據存在大量的重複,佔用大量的備份空間,備份與恢復時間長

mysqldump備份庫

Mysql數據庫的備份可以採用多種方式
直接打包數據庫文件夾,如/usr/local/mysal/data
使用專用備份工具mysqldump
mysqldump命令
mysql自帶的備份工具,相當方便對mysql進行備份
通過命令工具可以將指定額庫,表或全部的庫導出爲SQL腳本,在需要恢復時可進行數據恢復

mysql增量備份

特點
沒有重複數據,備份量不大,時間短
恢復麻煩:需要上次完全備份及完全備份之後所有的增量備份才能恢復,而且要對所有增量備份進行逐個反推恢復
可以通過mysql提供的二進制日誌(binary logs) 間接實現增量備份

mysql數據庫增量恢復

一般恢復
添加數據——進行完全備份——錄入新的數據——進行增量備份——模擬故障——恢復操作

基於位置恢復
就是將某個起始時間的二進制日誌導入數據庫中,從而跳過某個發生錯誤的時間點實現數據的恢復

基於時間點恢復
使用基於時間點的恢復,可能會出現在一個時間點裏既同時存在正確的操作又存在錯誤的操作,所以我們需要一種更爲精確的恢復方式

我們先創建一個數據庫和表格來做實驗

mysql> create database shcool; #創建shcool庫
Query OK, 1 row affected (0.51 sec)

mysql> use shcool; #進入shcool庫中
Database changed
mysql> create table info (  #創建info表
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> score decimal(4,1) not null);
Query OK, 0 rows affected (0.21 sec)

mysql> desc info; #查看錶結構
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(4)       | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)  | NO   |     | NULL    |                |
| score | decimal(4,1) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> insert into info (name,score) values ('stu01',88),('stu02',77); #插入數據
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

mysql> select * from info limit 1; #查看數據表的開頭的一行
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
+----+-------+-------+
1 row in set (0.00 sec)

mysql> quit #退出來

完整備份

物理備份

[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin      COPYING-test  docs     lib  mysqld.pid  mysql.sock.lock  README       share          usr
COPYING  data          include  man  mysql.sock  mysql-test       README-test  support-files
[root@localhost mysql]# cd data/
[root@localhost data]# ls
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  shcool  sys
[root@localhost data]# cd shcool/
[root@localhost shcool]# cd ../../
[root@localhost mysql]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/
tar: Removing leading `/' from member names
/usr/local/mysql/data/
/usr/local/mysql/data/ibdata1
/usr/local/mysql/data/ib_logfile1
[root@localhost mysql]# ls /opt/
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh

邏輯備份

[root@localhost mysql]# cd data/
[root@localhost data]# mysqldump -u root -p shcool > /opt/shcool.sql
Enter password: 
[root@localhost data]# ls /opt/
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql

對多個數據庫進行完整性備份

[root@localhost opt]# mysqldump -u root -p123123 --databases shcool mysql > /opt/db_shcool_mysql.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]# ls
db_shcool_mysql.sql  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql

對所有數據庫進行備份

[root@localhost opt]# mysqldump -u root -p123123 --opt --all-databases > /opt/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]# ls
all.sql  db_shcool_mysql.sql  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql

針對某一張表進行備份

[root@localhost opt]# mysqldump -u root -p123123 shcool info > /opt/shcool_info.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

對某張表的結構進行備份

[root@localhost opt]# mysqldump -u root -p123123 -d shcool info > /opt/shcool_info_secret.sql

完整備份恢復

mysql> use shcool;
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> drop table info;
Query OK, 0 rows affected (0.03 sec)
mysql> source /opt/shcool.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)
mysql> show tables;
+------------------+
| Tables_in_shcool |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)
mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

在控制檯下恢復數據庫

mysql> drop table info;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
Empty set (0.00 sec)
mysql> quit

[root@localhost opt]# mysql -u root -p123123 shcool < /opt/shcool.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

進數據庫去驗證一下

[root@localhost opt]# mysql -u root -p123123

mysql> use shcool;
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> show tables;
+------------------+
| Tables_in_shcool |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

增量備份和恢復

我們先把原來的刪掉

[root@localhost opt]# rm -rf *.sql
[root@localhost opt]# ls
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh

先要開啓二進制日誌文件

[root@localhost opt]# vim /etc/my.cnf

我們在[mysqld]區域中加上這一行開啓二進制日誌文件

log-bin=mysql-bin

[root@localhost opt]# systemctl restart mysqld.service 
[root@localhost opt]# cd /usr/local/mysql/data/
[root@localhost data]# ls
auto.cnf        ibdata1      ib_logfile1  mysql             mysql-bin.index     shcool
ib_buffer_pool  ib_logfile0  ibtmp1       mysql-bin.000001  performance_schema  sys
[root@localhost data]# mysqldump -u root -p123123 shcool > /opt/shcool.sql #增量備份是建立在完整備份的基礎上
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]# ls /opt/
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql
[root@localhost data]# mysqladmin -uroot -p123123 flush-logs  #產生增量備份的日誌文件
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]# ls #02就是我們的二進制日誌文件,你接下來的操作會生成到02當中
auto.cnf        ibdata1      ib_logfile1  mysql             mysql-bin.000002  performance_schema  sys
ib_buffer_pool  ib_logfile0  ibtmp1       mysql-bin.000001  mysql-bin.index   shcool

[root@localhost data]# mysql -u root -p123123

mysql> use shcool;
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 info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

mysql> insert into info (name,score) values ('test01',66); #正常插入數據
Query OK, 1 row affected (0.01 sec)

mysql> delete from info where name='stu01'; #這是一個誤操作,我們不小心刪了一行數據
Query OK, 1 row affected (0.01 sec)

mysql> insert into info (name,score) values ('test02',99); #正常插入數據
Query OK, 1 row affected (0.00 sec)
mysql> select * from info;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
|  4 | test02 |  99.0 |
+----+--------+-------+
3 rows in set (0.00 sec)

我們想恢復stu01數據,它正好在兩條正常的操作語句中間

[root@localhost data]# mysqladmin -uroot -p123123 flush-logs 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]# ls #我們剛纔所有的操作都在02中包括誤操作
auto.cnf        ibdata1      ib_logfile1  mysql             mysql-bin.000002  mysql-bin.index     shcool
ib_buffer_pool  ib_logfile0  ibtmp1       mysql-bin.000001  mysql-bin.000003  performance_schema  sys
[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 > /opt/bak.txt
要用64解碼器輸出,按行進行讀取,顯示出來生成到bak.txt文件中
[root@localhost data]# cd /opt/
[root@localhost opt]# ls
bak.txt  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql
以時間點進行恢復

去日誌文件中找到刪除的那條語句,把時間寫下來

191124 19:18:21    --stop-datetime 代表從日誌文件頭部操作開始到這個時間點,後面不在執行操作
191124 19:18:33    --start-datatime 代表從這個時間點開始繼續進行操作

我們進數據庫先不進行時間點的恢復,我們嘗試一下增量備份的恢復

[root@localhost opt]# mysql -u root -p123123
mysql> drop table info;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from info;
ERROR 1146 (42S02): Table 'shcool.info' doesn't exist
mysql> source /opt/shcool.sql;
mysql> show tables;
+------------------+
| Tables_in_shcool |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

mysql> quit
Bye

第一個時間點恢復,我們那個時間點有一個插入了一行數據

[root@localhost opt]# mysqlbinlog --no-defaults --stop-datetime='2019-11-24 19:18:21' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123

我們去數據庫驗證一下

[root@localhost opt]# mysql -u root -p123123

mysql> use shcool;
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 info;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
+----+--------+-------+
3 rows in set (0.00 sec)

mysql> quit

第二個時間點恢復,我們等於跳過我們之前的誤操作

[root@localhost opt]# mysqlbinlog --no-defaults --start-datetime='2019-11-24 19:18:33' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123

我們再去數據庫驗證一下

[root@localhost opt]# mysql -u root -p123123
mysql> use shcool;
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 info;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
|  4 | test02 |  99.0 |
+----+--------+-------+
4 rows in set (0.00 sec)

按照at位置恢復

先進行誤操作把test的刪掉

mysql> delete from info where name='test01';
Query OK, 1 row affected (0.00 sec)

mysql> delete from info where name='test02';
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)
[root@localhost opt]# cd /opt/
[root@localhost opt]# ls
bak.txt  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql
[root@localhost opt]# vim bak.txt 

at 568  --stop-position   #跟前面一樣,568是從日誌頭部到568結束
at 672  --start-position  #從672開始到結束

568位置恢復

[root@localhost opt]# mysqlbinlog --no-defaults --stop-position='568' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123mysql: [Warning] Using a password on the command line interface can be insecure.

我們去數據庫驗證一下

[root@localhost opt]# mysql -u root -p123123
mysql> use shcool;
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 info;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
+----+--------+-------+
3 rows in set (0.00 sec)

mysql> quit
Bye

672位置恢復,就跳過了我們之前的誤操作

[root@localhost opt]# mysqlbinlog --no-defaults --start-position='672' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123

再去數據庫驗證一下

[root@localhost opt]# mysql -u root -p123123
mysql> use shcool;
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 info;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
|  4 | test02 |  99.0 |
+----+--------+-------+
4 rows in set (0.00 sec)

以上就是我們全部的內容了,謝謝收看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章