国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql數據庫自動備份且恢復破壞數據方法

2024-07-24 12:39:07
字體:
來源:轉載
供稿:網友

介紹數據庫自動備份以及數據庫被破壞后的恢復的方法,在這里,我們使用mysqlhotcopy,并且定義一段Shell腳本來實現數據庫的自動備份,并且,讓整個數據自動備份與數據恢復過程都基于Shell.

建立數據庫備份所需條件

[1] 建立自動備份腳本

在這里,為了使數據庫備份和恢復的符合我們的實際要求,用一段符合要求的Shell腳本來實現整個備份過程的自動化,代碼如下:

[root@CentOS ~]# vi mysql-backup.sh ← 建立數據庫自動備份腳本,如下:

  1. #!/bin/bash  
  2. PATH=/usr/local/sbin:/usr/bin:/bin  
  3. # The Directory of Backup  
  4. BACKDIR=/backup/mysql  
  5. # The Password of MySQL  
  6. ROOTPASS=******** 此處請將星號替換成MySQL的root密碼  
  7. # Remake the Directory of Backup  
  8. rm -rf $BACKDIR  
  9. mkdir -p $BACKDIR  
  10. # Get the Name of Database  
  11. DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /`  
  12. # Backup with Database  
  13. for dbname in $DBLIST  
  14. do  
  15. mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy  
  16. done  

[2] 運行數據庫自動備份腳本,代碼如下:

  1. [root@CentOS ~]# chmod 700 mysql-backup.sh 改變腳本屬性,讓其只能讓root用戶執行  
  2. [root@CentOS ~]# ./mysql-backup.sh 運行腳本  
  3. [root@CentOS ~]# ls -l /backup/mysql/ 確認一下是否備份成功  
  4. total 8  
  5. drwxr-x--- 2 mysql mysql 4096 Sep 1 16:54 mysql 已成功備份到/backup/mysql目錄中 

[3] 讓數據庫備份腳本每天自動運行

[root@sample ~]# crontab -e ← 編輯自動運行規則,然后會出現編輯窗口,操作同vi.

00 03 * * * /root/mysql-backup.sh 添加這一行到文件中,讓數據庫備份每天凌晨3點進行.

測試自動備份正常運轉與否,備份恢復的方法,這里,以通過實際操作的過程來介紹問題出現后的恢復方法.

[1] 當數據庫被刪除后的恢復方法.

首先建立一個測試用的數據庫,代碼如下:

[root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務器:

  1. Enter password: ← 輸入MySQL的root用戶密碼  
  2. Welcome to the MySQL monitor. Commands end with ; or g.  
  3. Your MySQL connection id is 8 to server version: 4.1.20  
  4. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
  5. mysql> create database test; ← 建立一個測試用的數據庫test  
  6. Query OK, 1 row affected (0.00 sec)  
  7. mysql> use test ← 連接到這個數據庫  
  8. Database changed  
  9. mysql> create table test(num intname varchar(50)); ← 在數據庫中建立一個表  
  10. Query OK, 0 rows affected (0.07 sec)  
  11. mysql> insert into test values(1,'Hello,CentOS'); ← 插入一個值到這個表(這里以“Hello,CentOS”為例)  
  12. Query OK, 1 row affected (0.02 sec)  
  13. mysql> select * from test; ← 查看數據庫中的內容  
  14. +------+-----------------+  
  15. | num | name |  
  16. +------+-----------------+  
  17. |1 | Hello,Centos | ← 確認剛剛插入到表中的值的存在  
  18. +------+------------------+  
  19. 1 row in set (0.01 sec)  
  20. mysql> exit ← 退出MySQL服務器  
  21. Bye  

然后,運行剛才建立的數據庫備份腳本,備份剛剛建立的測試用的數據庫.

[root@sample ~]# cd ← 回到腳本所在的root用戶的根目錄 

[root@sample ~]# ./mysql-backup.sh ← 運行腳本進行數據庫備份

接下來,我們再次登錄到MySQL服務器中,刪除剛剛建立的測試用的數據庫test,以便于測試數據恢復能否成功,代碼如下:

  1. [root@Centos ~]# mysql -u root -p ← 用root登錄到MySQL服務器  
  2. Enter password: ← 輸入MySQL的root用戶密碼  
  3. Welcome to the MySQL monitor. Commands end with ; or g.  
  4. Your MySQL connection id is 13 to server version: 4.1.20  
  5. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
  6. mysql> use test ← 連接到測試用的test數據庫  
  7. Reading table information for completion of table and column names  
  8. You can turn off this feature to get a quicker startup with -A  
  9. Database changed  
  10. mysql> drop table test; ← 刪除數據中的表  
  11. Query OK, 0 rows affected (0.04 sec)  
  12. mysql> drop database test; ← 刪除測試用數據庫test  
  13. Query OK, 0 rows affected (0.01 sec)  
  14. mysql> show databases;  
  15. +---------------+  
  16. Database |  
  17. +---------------+  
  18. | mysql | ← 確認測試用的test數據庫已不存在、已被刪除  
  19. +---------------+  
  20. 1 row in set (0.01 sec)  
  21. mysql> exit ← 退出MySQL服務器  
  22. Bye  

以上,我們就等于模擬了數據庫被破壞的過程,接下來,是數據庫被“破壞”后,用備份進行恢復的方法.

  1. [root@Centos ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/ ← 復制備份的數據庫test到相應目錄  
  2.  
  3. [root@Centos ~]# chown -R mysql:mysql /var/lib/mysql/test/ ← 改變數據庫test的歸屬為mysql  
  4.  
  5. [root@Centos ~]# chmod 700 /var/lib/mysql/test/ ← 改變數據庫目錄屬性為700  
  6. [root@Centos ~]# chmod 660 /var/lib/mysql/test/* ← 改變數據庫中數據的屬性為660 

然后,再次登錄到MySQL服務器上,看是否已經成功恢復了數據庫,代碼如下:

  1. [root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務器  
  2. Enter password: ← 輸入MySQL的root用戶密碼  
  3. Welcome to the MySQL monitor. Commands end with ; or g.  
  4. Your MySQL connection id is 14 to server version: 4.1.20  
  5. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
  6. mysql> show databases; ← 查看當前存在的數據庫  
  7. +-------------+  
  8. Database |  
  9. +-------------+  
  10. | mysql |  
  11. | test | ← 確認剛剛被刪除的test數據庫已經成功被恢復回來!  
  12. +------------+  
  13. rows in set (0.00 sec)  
  14. mysql> use test ← 連接到test數據庫  
  15. Reading table information for completion of table and column names  
  16. You can turn off this feature to get a quicker startup with -A  
  17. Database changed  
  18. mysql> show tables; ← 查看test數據庫中存在的表  
  19. +-------------------+  
  20. | Tables_in_test |  
  21. +-------------------+  
  22. | test |  
  23. +-------------------+  
  24. 1 row in set (0.00 sec)  
  25. mysql> select * from test; ← 查看數據庫中的內容  
  26. +------+---------------------+  
  27. | num | name |  
  28. +------+---------------------+  
  29. | 1 | Hello,CentOS | ← 確認數據表中的內容與刪除前定義的“Hello,CentOS”一樣!  
  30. +------+---------------------+  
  31. 1 row in set (0.01 sec)  
  32. mysql> exit ← 退出MySQL服務器  
  33. Bye 

以上結果表示,數據庫被刪除后,用備份后的數據庫成功的將數據恢復到了刪除前的狀態.

2] 當數據庫被修改后的恢復方法

數據庫被修改,可能存在著多方面的原因,被入侵、以及相應程序存在Bug等等,這里不作詳細介紹,這里將只介紹在數據庫被修改后,如果恢復到被修改前狀態的方法.

具體和上面所述的“數據庫被刪除后的恢復方法”相類似,這里,測試用數據庫接著使用剛剛在前面用過的test,這里為了使剛剛接觸數據庫的朋友不至于理解混亂,我們再次登錄到MySQL服務器上確認一下剛剛建立的測試用的數據庫test的相關信息,代碼如下:

  1. [root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務器  
  2. Enter password: ← 輸入MySQL的root用戶密碼  
  3. Welcome to the MySQL monitor. Commands end with ; or g.  
  4. Your MySQL connection id is 14 to server version: 4.1.20  
  5. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
  6. mysql> show databases; ← 查看當前存在的數據庫  
  7. +-------------+  
  8. Database |  
  9. +-------------+  
  10. | mysql |  
  11. | test |  
  12. +------------+  
  13. rows in set (0.00 sec)  
  14. mysql> use test ← 連接到test數據庫  
  15. Reading table information for completion of table and column names  
  16. You can turn off this feature to get a quicker startup with -A  
  17. Database changed  
  18. mysql> show tables; ← 查看test數據庫中存在的表  
  19. +-------------------+  
  20. | Tables_in_test |  
  21. +-------------------+  
  22. | test |  
  23. +-------------------+  
  24. 1 row in set (0.00 sec)  
  25. mysql> select * from test; ← 查看數據庫中的內容  
  26. +------+--------------------+  
  27. | num | name |  
  28. +------+--------------------+  
  29. | 1 | Hello,CentOS|  
  30. +------+--------------------+  
  31. 1 row in set (0.01 sec)  
  32. mysql> exit ← 退出MySQL服務器  
  33. Bye  

然后,我們再次運行數據庫備份腳本,將當前狀態的數據庫,再做一次備份.

[root@CentOS ~]# cd ← 回到腳本所在的root用戶的根目錄 

[root@CentOS ~]# ./mysql-backup.sh ← 運行腳本進行數據庫備份

接下來,我們再次登錄到MySQL服務器中,對測試用的數據庫test進行一些修改,以便于測試數據恢復能否成功,代碼如下:

  1. [root@sample ~]# mysql -u root -p ← 用root登錄到MySQL服務器  
  2. Enter password: ← 輸入MySQL的root用戶密碼  
  3. Welcome to the MySQL monitor. Commands end with ; or g.  
  4. Your MySQL connection id is 15 to server version: 4.1.20  
  5. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
  6. mysql> use test ← 連接到test數據庫  
  7. Reading table information for completion of table and column names  
  8. You can turn off this feature to get a quicker startup with -A  
  9. Database changed  
  10. mysql> update test set name='Shit,Windows'; ← 然后將test中表的值重新定義為“Shit,Windows”(原來為“Hello,CentOS”)  
  11. Query OK, 1 row affected (0.07 sec)  
  12. Rows matched: 1 Changed: 1 Warnings: 0  
  13. mysql> select * from test; ← 確認test中的表被定義的值  
  14. +------+--------------------+  
  15. | num | name |  
  16. +------+-------------------+  
  17. | 1 | Shit,Windows | ← 確認已經將原test數據庫表中的值修改為新的值“Shit,Windows”  
  18. +------+-------------------+  
  19. 1 row in set (0.00 sec)  
  20. mysql> exit ← 退出MySQL服務器  
  21. Bye  

以上,我們就等于模擬了數據庫被篡改的過程,接下來,是數據庫被“篡改”后,用備份進行恢復的方法.

[root@CentOS ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/ ← 復制備份的數據庫test到相應目錄.

然后,再次登錄到MySQL服務器上,看數據庫是否被恢復到了被“篡改”之前的狀態,代碼如下:

  1. [root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務器  
  2. Enter password: ← 輸入MySQL的root用戶密碼  
  3. Welcome to the MySQL monitor. Commands end with ; or g.  
  4. Your MySQL connection id is 16 to server version: 4.1.20  
  5. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
  6. mysql> use test ← 連接到test數據庫  
  7. Reading table information for completion of table and column names  
  8. You can turn off this feature to get a quicker startup with -A  
  9. Database changed  
  10. mysql> select * from test; ← 查看數據庫中的內容  
  11. +------+----------------+  
  12. | num | name |  
  13. +------+----------------+  
  14. | 1| Hello,CentOS | ← 確認數據表中的內容與被修改前定義的“Hello,CentOS”一樣!  
  15. +------+----------------+  
  16. 1 row in set (0.01 sec)  
  17. mysql> exit ← 退出MySQL服務器  
  18. Bye  

以上結果表示,數據庫被修改后,用備份后的數據庫成功的將數據恢復到了被“篡改”前的狀態。

測試后…

測試完成后,將測試用過的遺留信息刪除,代碼如下:

  1. [root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務器  
  2. Enter password: ← 輸入MySQL的root用戶密碼  
  3. Welcome to the MySQL monitor. Commands end with ; or g.  
  4. Your MySQL connection id is 19 to server version: 4.1.20  
  5. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
  6. mysql> use test ← 連接到test數據庫  
  7. Reading table information for completion of table and column names  
  8. You can turn off this feature to get a quicker startup with -A  
  9. Database changed  
  10. mysql> drop table test; ← 刪除test數據庫中的表  
  11. Query OK, 0 rows affected (0.01 sec)  
  12. mysql> drop database test; ← 刪除測試用數據庫test  
  13. Query OK, 0 rows affected (0.00 sec)  
  14. mysql> show databases; ← 查看當前存在的數據庫  
  15. +-------------+  
  16. Database |  
  17. +-------------+  
  18. | mysql | ← 確認測試用數據庫test不存在、已被刪除  
  19. +-------------+  
  20. 1 row in set (0.00 sec)  
  21. mysql> exit ← 退出MySQL服務器  
  22. Bye   --Vevb.com 

以上介紹了用我們自己建立的一段Shell腳本,通過mysqlhotcopy來備份數據庫的方法.

對于許多個人愛好者來說,組建服務器可能不是很考慮數據被破壞以及數據被破壞后的恢復工作,但不能不說,對于服務器來說,數據破壞后的恢復效率也是區 別業余和專業的因素之一,所以筆者建議,在您配置好了Web服務器以及MySQL服務器等等的時候,千萬不要急于應用它,而要想辦法在有限的(硬件、軟件)條件下使它“堅不可摧”之后,再考慮應用的問題.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定西市| 新巴尔虎右旗| 杭锦后旗| 榆社县| 清新县| 满城县| 潜山县| 淄博市| 肥乡县| 西吉县| 十堰市| 宁津县| 喀什市| 莱州市| 乌兰察布市| 安康市| 偏关县| 扶风县| 深泽县| 通榆县| 安仁县| 无为县| 江北区| 巨野县| 九龙县| 临汾市| 抚宁县| 喀什市| 河曲县| 奎屯市| 房产| 织金县| 泰安市| 夹江县| 额济纳旗| 涟源市| 依兰县| 康平县| 安达市| 安达市| 胶州市|