一、前言:
當數據庫服務器建立好以后,我們首先要做的不是考慮要在這個支持數據庫的服務器運行哪些受MySQL提攜的程序,而是當數據庫遭到破壞后,怎樣安然恢復到最后一次正常的狀態,使得數據的損失達到最小。
或者說,僅僅是數據庫服務器的建立,只能說明它能做些什么,并不代表它能穩定的做些什么。災難恢復的效率及全面性,也是系統的穩定性的一個準因素,尤其對于一個服務器系統。
這一節,介紹數據庫自動備份以及數據庫被破壞后的恢復的方法。在這里,我們使用mysqlhotcopy,并且定義一段Shell腳本來實現數據庫的自動備份,并且,讓整個數據自動備份與數據恢復過程都基于Shell。
建立數據庫備份所需條件
[1] 建立自動備份腳本
在這里,為了使數據庫備份和恢復的符合我們的實際要求,用一段符合要求的Shell腳本來實現整個備份過程的自動化。
[root@CentOS ~]# vi mysql-backup.sh ← 建立數據庫自動備份腳本,如下: #!/bin/bash PATH=/usr/local/sbin:/usr/bin:/bin # The Directory of Backup BACKDIR=/backup/mysql # The Password of MySQL ROOTPASS=******** 此處請將星號替換成MySQL的root密碼 # Remake the Directory of Backup rm -rf $BACKDIR mkdir -p $BACKDIR # Get the Name of Database DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /` # Backup with Database for dbname in $DBLIST do mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy done |
[2] 運行數據庫自動備份腳本
[root@CentOS ~]# chmod 700 mysql-backup.sh 改變腳本屬性,讓其只能讓root用戶執行 [root@CentOS ~]# ./mysql-backup.sh 運行腳本 [root@CentOS ~]# ls -l /backup/mysql/ 確認一下是否備份成功 total 8 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服務器 Enter password: ← 輸入MySQL的root用戶密碼 Welcome to the MySQL monitor. Commands end with ; or /g. Your MySQL connection id is 8 to server version: 4.1.20 Type 'help;' or '/h' for help. Type '/c' to clear the buffer. mysql> create database test; ← 建立一個測試用的數據庫test Query OK, 1 row affected (0.00 sec) mysql> use test ← 連接到這個數據庫 Database changed mysql> create table test(num int, name varchar(50)); ← 在數據庫中建立一個表 Query OK, 0 rows affected (0.07 sec) mysql> insert into test values(1,'Hello,CentOS'); ← 插入一個值到這個表(這里以“Hello,CentOS”為例) Query OK, 1 row affected (0.02 sec) mysql> select * from test; ← 查看數據庫中的內容 +------+-----------------+ | num | name | +------+-----------------+ |1 | Hello,Centos | ← 確認剛剛插入到表中的值的存在 +------+------------------+ 1 row in set (0.01 sec) mysql> exit ← 退出MySQL服務器 Bye |
然后,運行剛才建立的數據庫備份腳本,備份剛剛建立的測試用的數據庫。
[root@sample ~]# cd ← 回到腳本所在的root用戶的根目錄 [root@sample ~]# ./mysql-backup.sh ← 運行腳本進行數據庫備份 |
接下來,我們再次登錄到MySQL服務器中,刪除剛剛建立的測試用的數據庫test,以便于測試數據恢復能否成功。
[root@Centos ~]# mysql -u root -p ← 用root登錄到MySQL服務器 Enter password: ← 輸入MySQL的root用戶密碼 Welcome to the MySQL monitor. Commands end with ; or /g. Your MySQL connection id is 13 to server version: 4.1.20 Type 'help;' or '/h' for help. Type '/c' to clear the buffer. mysql> use test ← 連接到測試用的test數據庫 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 test; ← 刪除數據中的表 Query OK, 0 rows affected (0.04 sec) mysql> drop database test; ← 刪除測試用數據庫test Query OK, 0 rows affected (0.01 sec) mysql> show databases; +---------------+ | Database | +---------------+ | mysql | ← 確認測試用的test數據庫已不存在、已被刪除 +---------------+ 1 row in set (0.01 sec) mysql> exit ← 退出MySQL服務器 Bye |
以上,我們就等于模擬了數據庫被破壞的過程。接下來,是數據庫被“破壞”后,用備份進行恢復的方法。
[root@Centos ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/ ← 復制備份的數據庫test到相應目錄 [root@Centos ~]# chown -R mysql:mysql /var/lib/mysql/test/ ← 改變數據庫test的歸屬為mysql [root@Centos ~]# chmod 700 /var/lib/mysql/test/ ← 改變數據庫目錄屬性為700 [root@Centos ~]# chmod 660 /var/lib/mysql/test/* ← 改變數據庫中數據的屬性為660 |
然后,再次登錄到MySQL服務器上,看是否已經成功恢復了數據庫。
[root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務器 Enter password: ← 輸入MySQL的root用戶密碼 Welcome to the MySQL monitor. Commands end with ; or /g. Your MySQL connection id is 14 to server version: 4.1.20 Type 'help;' or '/h' for help. Type '/c' to clear the buffer. mysql> show databases; ← 查看當前存在的數據庫 +-------------+ | Database | +-------------+ | mysql | | test | ← 確認剛剛被刪除的test數據庫已經成功被恢復回來! +------------+ 2 rows in set (0.00 sec) mysql> use test ← 連接到test數據庫 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; ← 查看test數據庫中存在的表 +-------------------+ | Tables_in_test | +-------------------+ | test | +-------------------+ 1 row in set (0.00 sec) mysql> select * from test; ← 查看數據庫中的內容 +------+---------------------+ | num | name | +------+---------------------+ | 1 | Hello,CentOS | ← 確認數據表中的內容與刪除前定義的“Hello,CentOS”一樣! +------+---------------------+ 1 row in set (0.01 sec) mysql> exit ← 退出MySQL服務器 Bye |
以上結果表示,數據庫被刪除后,用備份后的數據庫成功的將數據恢復到了刪除前的狀態。
新聞熱點
疑難解答