mysql 資料破壞還原與資料自動備份

來源:互聯網
上載者:User

建立自動備份指令碼
在這裡,為了使資料庫教程備份和恢複的符合我們的實際要求,用一段符合要求的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] 讓Database Backup指令碼每天自動運行
[root@sample ~]# crontab -e  ← 編輯自動運行規則(然後會出現編輯視窗,操作同vi)
00 03 * * * /root/mysql-backup.sh   添加這一行到檔案中,讓Database Backup每天淩晨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

然後,運行剛才建立的Database Backup指令碼,備份剛剛建立的測試用的資料庫。
[root@sample ~]# cd ← 回到指令碼所在的root使用者的根目錄
[root@sample ~]# ./mysql-backup.sh  ← 運行指令碼進行Database Backup

接下來,我們再次登入到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

具體和上面所述的“資料庫被刪除後的恢複方法”相類似。這裡,測試用資料庫接著使用剛剛在前面用過的test。這裡為了使剛剛接觸資料庫的朋友不至於理解混亂,我們再次登入到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 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  |
+------------+
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|
+------+--------------------+
1 row in set (0.01 sec)

mysql> exit  ← 退出MySQL伺服器
Bye

然後,我們再次運行Database Backup指令碼,將目前狀態的資料庫,再做一次備份。
[root@CentOS ~]# cd  ← 回到指令碼所在的root使用者的根目錄
[root@CentOS ~]# ./mysql-backup.sh  ← 運行指令碼進行Database Backup

接下來,我們再次登入到MySQL伺服器中,對測試用的資料庫test進行一些修改,以便於測試資料恢複能否成功。
[root@sample ~]# 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 15 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> 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;  ← 確認test中的表被定義的值
+------+--------------------+
| num | name  |
+------+-------------------+
| 1 | Shit,Windows |  ← 確認已經將原test資料庫表中的值修改為新的值“Shit,Windows”
+------+-------------------+
1 row in set (0.00 sec)

mysql> exit  ← 退出MySQL伺服器
Bye

以上,我們就等於類比了資料庫被篡改的過程。接下來,是資料庫被“篡改”後,用備份進行恢複的方法。
[root@CentOS ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/  ← 複本備份的資料庫test到相應目錄

然後,再次登入到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 16 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> select * from test;  ← 查看資料庫中的內容
+------+----------------+
| num | name  |
+------+----------------+
| 1| Hello,CentOS | ← 確認資料表中的內容與被修改前定義的“Hello,CentOS”一樣!
+------+----------------+
1 row in set (0.01 sec)

mysql> exit  ← 退出MySQL伺服器
Bye

以上結果表示,資料庫被修改後,用備份後的資料庫成功的將資料恢複到了被“篡改”前的狀態。
測試後…
測試完成後,將測試用過的遺留資訊刪除。
[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 19 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;  ← 刪除test資料庫中的表
Query OK, 0 rows affected (0.01 sec)

mysql> drop database test;  ← 刪除測試用資料庫test
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;  ← 查看當前存在的資料庫
+-------------+
| Database |
+-------------+
| mysql |  ← 確認測試用資料庫test不存在、已被刪除
+-------------+
1 row in set (0.00 sec)

mysql> exit  ← 退出MySQL伺服器
Bye

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.