亚洲韩日午夜视频,欧美日韩在线精品一区二区三区,韩国超清无码一区二区三区,亚洲国产成人影院播放,久草新在线,在线看片AV色

您好,歡迎來到思海網(wǎng)絡(luò),我們將竭誠為您提供優(yōu)質(zhì)的服務(wù)! 誠征網(wǎng)絡(luò)推廣 | 網(wǎng)站備案 | 幫助中心 | 軟件下載 | 購買流程 | 付款方式 | 聯(lián)系我們 [ 會員登錄/注冊 ]
促銷推廣
客服中心
業(yè)務(wù)咨詢
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352289
點(diǎn)擊這里給我發(fā)消息  81721488
有事點(diǎn)擊這里…  376585780
有事點(diǎn)擊這里…  872642803
有事點(diǎn)擊這里…  459248018
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  380791050
技術(shù)支持
有事點(diǎn)擊這里…  714236853
有事點(diǎn)擊這里…  719304487
有事點(diǎn)擊這里…  1208894568
有事點(diǎn)擊這里…  61352289
在線客服
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  983054746
有事點(diǎn)擊這里…  893984210
當(dāng)前位置:首頁 >> 技術(shù)文章 >> 文章瀏覽
技術(shù)文章

MySQL數(shù)據(jù)庫的自動備份與數(shù)據(jù)庫被破壞后的恢復(fù)

添加時(shí)間:2014-4-12 16:01:45  添加: 思海網(wǎng)絡(luò) 

在數(shù)據(jù)庫服務(wù)器建立好以后,我們首先要做的不是考慮要在這個(gè)支持?jǐn)?shù)據(jù)庫的服務(wù)器運(yùn)行哪些受MySQL提攜的程序,而是當(dāng)數(shù)據(jù)庫遭到破壞后,怎樣安然恢復(fù)到最后一次正常的狀態(tài),使得數(shù)據(jù)的損失達(dá)到最小。

或者說,僅僅是數(shù)據(jù)庫服務(wù)器的建立,只能說明它能做些什么,并不代表它能穩(wěn)定的做些什么。災(zāi)難恢復(fù)的效率及全面性,也是系統(tǒng)的穩(wěn)定性的一個(gè)準(zhǔn)因素,尤其對于一個(gè)服務(wù)器系統(tǒng)。

這一節(jié),介紹數(shù)據(jù)庫自動備份以及數(shù)據(jù)庫被破壞后的恢復(fù)的方法。在這里,我們使用mysqlhotcopy,并且定義一段Shell腳本來實(shí)現(xiàn)數(shù)據(jù)庫的自動備份,并且,讓整個(gè)數(shù)據(jù)自動備份與數(shù)據(jù)恢復(fù)過程都基于Shell。

建立數(shù)據(jù)庫備份所需條件

[1] 建立自動備份腳本

在這里,為了使數(shù)據(jù)庫備份和恢復(fù)的符合我們的實(shí)際要求,用一段符合要求的Shell腳本來實(shí)現(xiàn)整個(gè)備份過程的自動化。

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

#!/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] 運(yùn)行數(shù)據(jù)庫自動備份腳本

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

[3] 讓數(shù)據(jù)庫備份腳本每天自動運(yùn)行

[root@sample ~]# crontab -e  ← 編輯自動運(yùn)行規(guī)則(然后會出現(xiàn)編輯窗口,操作同vi)
00 03 * * * /root/mysql-backup.sh   添加這一行到文件中,讓數(shù)據(jù)庫備份每天凌晨3點(diǎn)進(jìn)行

測試自動備份正常運(yùn)轉(zhuǎn)與否(備份恢復(fù)的方法)

這里,以通過實(shí)際操作的過程來介紹問題出現(xiàn)后的恢復(fù)方法。

[1] 當(dāng)數(shù)據(jù)庫被刪除后的恢復(fù)方法

首先建立一個(gè)測試用的數(shù)據(jù)庫。

[root@CentOS ~]# mysql -u root -p   ← 用root登錄到MySQL服務(wù)器 
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;  ← 建立一個(gè)測試用的數(shù)據(jù)庫test 
Query OK, 1 row affected (0.00 sec) 

mysql> use test  ← 連接到這個(gè)數(shù)據(jù)庫 
Database changed 

mysql> create table test(num int, name varchar(50));  ← 在數(shù)據(jù)庫中建立一個(gè)表 
Query OK, 0 rows affected (0.07 sec) 

mysql> insert into test values(1,'Hello,CentOS');  ← 插入一個(gè)值到這個(gè)表(這里以“Hello,CentOS”為例) 
Query OK, 1 row affected (0.02 sec) 

mysql> select * from test;  ← 查看數(shù)據(jù)庫中的內(nèi)容 
+------+-----------------+ 
num name 
+------+-----------------+ 
1  Hello,Centos   ← 確認(rèn)剛剛插入到表中的值的存在 
+------+------------------+ 
1 row in set (0.01 sec) 

mysql> exit  ← 退出MySQL服務(wù)器 
Bye

然后,運(yùn)行剛才建立的數(shù)據(jù)庫備份腳本,備份剛剛建立的測試用的數(shù)據(jù)庫。

[root@sample ~]# cd ← 回到腳本所在的root用戶的根目錄 
[root@sample ~]# ./mysql-backup.sh  ← 運(yùn)行腳本進(jìn)行數(shù)據(jù)庫備份

接下來,我們再次登錄到MySQL服務(wù)器中,刪除剛剛建立的測試用的數(shù)據(jù)庫test,以便于測試數(shù)據(jù)恢復(fù)能否成功。

[root@Centos ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器 
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數(shù)據(jù)庫 
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;  ← 刪除數(shù)據(jù)中的表 
Query OK, 0 rows affected (0.04 sec) 

mysql> drop database test;  ← 刪除測試用數(shù)據(jù)庫test 
Query OK, 0 rows affected (0.01 sec) 

mysql> show databases; 
+---------------+ 
Database 
+---------------+ 
mysql   ← 確認(rèn)測試用的test數(shù)據(jù)庫已不存在、已被刪除 
+---------------+ 
1 row in set (0.01 sec) 

mysql> exit  ← 退出MySQL服務(wù)器 
Bye

以上,我們就等于模擬了數(shù)據(jù)庫被破壞的過程。接下來,是數(shù)據(jù)庫被“破壞”后,用備份進(jìn)行恢復(fù)的方法。

[root@Centos ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/  ← 復(fù)制備份的數(shù)據(jù)庫test到相應(yīng)目錄 
[root@Centos ~]# chown -R mysql:mysql /var/lib/mysql/test/  ← 改變數(shù)據(jù)庫test的歸屬為mysql 
[root@Centos ~]# chmod 700 /var/lib/mysql/test/  ← 改變數(shù)據(jù)庫目錄屬性為700 
[root@Centos ~]# chmod 660 /var/lib/mysql/test/*  ← 改變數(shù)據(jù)庫中數(shù)據(jù)的屬性為660

然后,再次登錄到MySQL服務(wù)器上,看是否已經(jīng)成功恢復(fù)了數(shù)據(jù)庫。

[root@CentOS ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器 
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;  ← 查看當(dāng)前存在的數(shù)據(jù)庫 
+-------------+ 
Database 
+-------------+ 
mysql 
test    ← 確認(rèn)剛剛被刪除的test數(shù)據(jù)庫已經(jīng)成功被恢復(fù)回來! 
+------------+ 
2 rows in set (0.00 sec) 

mysql> use test  ← 連接到test數(shù)據(jù)庫 
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數(shù)據(jù)庫中存在的表 
+-------------------+ 
Tables_in_test 
+-------------------+ 
test   
+-------------------+ 
1 row in set (0.00 sec) 

mysql> select * from test;  ← 查看數(shù)據(jù)庫中的內(nèi)容 
+------+---------------------+ 
num name  
+------+---------------------+ 
1 Hello,CentOS   ← 確認(rèn)數(shù)據(jù)表中的內(nèi)容與刪除前定義的“Hello,CentOS”一樣! 
+------+---------------------+ 
1 row in set (0.01 sec) 

mysql> exit  ← 退出MySQL服務(wù)器 
Bye

以上結(jié)果表示,數(shù)據(jù)庫被刪除后,用備份后的數(shù)據(jù)庫成功的將數(shù)據(jù)恢復(fù)到了刪除前的狀態(tài)。

[2] 當(dāng)數(shù)據(jù)庫被修改后的恢復(fù)方法

數(shù)據(jù)庫被修改,可能存在著多方面的原因,被入侵、以及相應(yīng)程序存在Bug等等,這里不作詳細(xì)介紹。這里將只介紹在數(shù)據(jù)庫被修改后,如果恢復(fù)到被修改前狀態(tài)的方法。

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

[root@CentOS ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器 
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;  ← 查看當(dāng)前存在的數(shù)據(jù)庫 
+-------------+ 
Database 
+-------------+ 
mysql 
test  
+------------+ 
2 rows in set (0.00 sec) 

mysql> use test  ← 連接到test數(shù)據(jù)庫 
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數(shù)據(jù)庫中存在的表 
+-------------------+ 
Tables_in_test 
+-------------------+ 
test   
+-------------------+ 
1 row in set (0.00 sec) 

mysql> select * from test;  ← 查看數(shù)據(jù)庫中的內(nèi)容 
+------+--------------------+ 
num name  
+------+--------------------+ 
1 Hello,CentOS 
+------+--------------------+ 
1 row in set (0.01 sec) 

mysql> exit  ← 退出MySQL服務(wù)器 
Bye

然后,我們再次運(yùn)行數(shù)據(jù)庫備份腳本,將當(dāng)前狀態(tài)的數(shù)據(jù)庫,再做一次備份。

[root@CentOS ~]# cd  ← 回到腳本所在的root用戶的根目錄 
[root@CentOS ~]# ./mysql-backup.sh  ← 運(yùn)行腳本進(jìn)行數(shù)據(jù)庫備份

接下來,我們再次登錄到MySQL服務(wù)器中,對測試用的數(shù)據(jù)庫test進(jìn)行一些修改,以便于測試數(shù)據(jù)恢復(fù)能否成功。

[root@sample ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器 
Enter password:  ← 輸入MySQL的root用戶密碼 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 15 to server version: 4.1.20 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 

mysql> use test  ← 連接到test數(shù)據(jù)庫 
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> update test set name='Shit,Windows';  ← 然后將test中表的值重新定義為“Shit,Windows”(原來為“Hello,CentOS”) 
Query OK, 1 row affected (0.07 sec) 
Rows matched: 1 Changed: 1 Warnings: 0 

mysql> select * from test;  ← 確認(rèn)test中的表被定義的值 
+------+--------------------+ 
num name   
+------+-------------------+ 
1 Shit,Windows   ← 確認(rèn)已經(jīng)將原test數(shù)據(jù)庫表中的值修改為新的值“Shit,Windows” 
+------+-------------------+ 
1 row in set (0.00 sec) 

mysql> exit  ← 退出MySQL服務(wù)器 
Bye

以上,我們就等于模擬了數(shù)據(jù)庫被篡改的過程。接下來,是數(shù)據(jù)庫被“篡改”后,用備份進(jìn)行恢復(fù)的方法。

[root@CentOS ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/  ← 復(fù)制備份的數(shù)據(jù)庫test到相應(yīng)目錄

然后,再次登錄到MySQL服務(wù)器上,看數(shù)據(jù)庫是否被恢復(fù)到了被“篡改”之前的狀態(tài)。

[root@CentOS ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器 
Enter password:  ← 輸入MySQL的root用戶密碼 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 16 to server version: 4.1.20 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 

mysql> use test  ← 連接到test數(shù)據(jù)庫 
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 test;  ← 查看數(shù)據(jù)庫中的內(nèi)容 
+------+----------------+ 
num name  
+------+----------------+ 
1 Hello,CentOS  ← 確認(rèn)數(shù)據(jù)表中的內(nèi)容與被修改前定義的“Hello,CentOS”一樣! 
+------+----------------+ 
1 row in set (0.01 sec) 

mysql> exit  ← 退出MySQL服務(wù)器 
Bye

以上結(jié)果表示,數(shù)據(jù)庫被修改后,用備份后的數(shù)據(jù)庫成功的將數(shù)據(jù)恢復(fù)到了被“篡改”前的狀態(tài)。

測試后…

測試完成后,將測試用過的遺留信息刪除。

[root@CentOS ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器 
Enter password:  ← 輸入MySQL的root用戶密碼 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 19 to server version: 4.1.20 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 

mysql> use test  ← 連接到test數(shù)據(jù)庫 
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;  ← 刪除test數(shù)據(jù)庫中的表 
Query OK, 0 rows affected (0.01 sec) 

mysql> drop database test;  ← 刪除測試用數(shù)據(jù)庫test 
Query OK, 0 rows affected (0.00 sec) 

mysql> show databases;  ← 查看當(dāng)前存在的數(shù)據(jù)庫 
+-------------+ 
Database 
+-------------+ 
mysql   ← 確認(rèn)測試用數(shù)據(jù)庫test不存在、已被刪除 
+-------------+ 
1 row in set (0.00 sec) 

mysql> exit  ← 退出MySQL服務(wù)器 
Bye

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

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

而且,以上介紹的方法中提到的數(shù)據(jù)庫自動備份腳本雖然被設(shè)置為每天定時(shí)運(yùn)行,但當(dāng)您運(yùn)行某些與MySQL相關(guān)聯(lián)的程序(論壇、社區(qū)等等)時(shí),做一些可 能危及數(shù)據(jù)安全的操作的時(shí)候,運(yùn)行數(shù)據(jù)庫備份腳本即時(shí)備份當(dāng)前狀態(tài)數(shù)據(jù)庫,也是非常有幫助的,至少可以在出現(xiàn)問題后保證數(shù)據(jù)庫方面的可恢復(fù)性。

關(guān)鍵字:MySQL、數(shù)據(jù)庫、服務(wù)器

分享到:

頂部 】 【 關(guān)閉
版權(quán)所有:佛山思海電腦網(wǎng)絡(luò)有限公司 ©1998-2024 All Rights Reserved.
聯(lián)系電話:(0757)22630313、22633833
中華人民共和國增值電信業(yè)務(wù)經(jīng)營許可證: 粵B1.B2-20030321 備案號:粵B2-20030321-1
網(wǎng)站公安備案編號:44060602000007 交互式欄目專項(xiàng)備案編號:200303DD003  
察察 工商 網(wǎng)安 舉報(bào)有獎  警警  手機(jī)打開網(wǎng)站