標籤:centos 6.4下mysql 備份及還原詳情
Centos 6.4下MySQL 備份及還原詳情
我們前面文中中介紹了centos 6.4下MySQL的安裝及配置,安裝後為了保證資料的安全性所以我們同樣也要學會備份及還原,備份及還原作業在我們的真實工作環境中是必不可少的,由於是對資料庫資料備份,所以今天我們就隨之介紹一下,Centos 6.4下的MySQL的備份及還原作業。我們這兩篇文章的介紹都是為了下面的Mysql 主從配置做鋪墊,所以環境跟上一篇的文章是一樣的,此次環境中還是上一篇文章中安裝的那兩台Cnetos .6.4+MySQL伺服器,今天主要介紹是準備將192.168.6.28上的DB1備份後,還原到192.168.6.38伺服器的DB上
我們前面說過了 mysql的資料檔案存放在
/var/lib/mysql下
650) this.width=650;" title="clip_image001" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image001" src="http://s3.51cto.com/wyfs02/M00/86/53/wKioL1e7-P2hqbBGAABwia4Vvgg079.png" height="110" />
一、首先是標準備份資料庫:
mysqldump -hlocalhost -uroot -p DB1 > DB1.sqlDB1為備份的資料庫名稱
650) this.width=650;" title="clip_image002" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image002" src="http://s3.51cto.com/wyfs02/M00/86/53/wKioL1e7-P6A9G9SAAB5CatI3Fs919.png" height="76" />
還原資料庫:
mysql -hlocalhost -uroot -p DB1 < DB1.sqlDB1為還原的資料庫名稱
650) this.width=650;" title="clip_image003" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image003" src="http://s3.51cto.com/wyfs02/M01/86/53/wKioL1e7-P6RBPqwAABezKr-0hs319.png" height="74" />
二、MySQL資料庫壓縮備份
mysqldump -hlocalhost -uroot -p DB1 | gzip > DB1.sql.gz
650) this.width=650;" title="clip_image004" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image004" src="http://s3.51cto.com/wyfs02/M02/86/53/wKioL1e7-P-wjULuAAB9lhWGdrk908.png" height="122" />
還原壓縮的MySQL資料庫gunzip < DB12016-08-23.sql.gz | mysql -uroot -p DB1
650) this.width=650;" title="clip_image005" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image005" src="http://s3.51cto.com/wyfs02/M00/86/54/wKiom1e7-QDy7hXxAABSR3gBzfo164.png" height="58" />
三、我們接下來使用指令碼壓縮備份
#!/bin/shDBName="DB1"Source="/root/MYSQLDB_BAK"date_str=`date +%Y-%m-%d_%H:%M:%S`mysqldump -h localhost -u root --password=beyondsoft -R -E -e --max_allowed_packet=1048576 --net_buffer_length=16384 $DBName | gzip > $Source/$DBName$date_str.sql.gzecho "$(date +%Y-%m-%d_%H:%M:%S) - Backup directory:DBName: $DBName to $Source/$DBName.sql.gz is Backup Sucess" >>/var/log/$DBName.log#!/bin/shDBName="DB1"Source="/root/MYSQLDB_BAK"#date_str=`date +%Y-%m-%d-%H:%M:%S`date_str=`date +%Y-%m-%d_%H-%M-%S`date_str=`date +%Y-%m-%d`mysqldump -h localhost -u root --password=beyondsoft -R -E -e --max_allowed_packet=1048576 --net_buffer_length=16384 $DBName | gzip > $Source/$DBName$date_str.sql.gzecho "$(date +%Y-%m-%d_%H:%M:%S) - Backup directory:DBName: $DBName to $Source/$DBName.sql.gz is Backup Sucess" >>/var/log/$DBName.log
添加了log,如果成功的話,會在/var/log/DB1.log產生一條log日誌
650) this.width=650;" title="clip_image006" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image006" src="http://s3.51cto.com/wyfs02/M00/86/53/wKioL1e7-QDQed2kAAC05VMzXJI307.png" height="73" />
執行後,備份成功
650) this.width=650;" title="clip_image007" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image007" src="http://s3.51cto.com/wyfs02/M02/86/53/wKioL1e7-QHw66mlAABJwQ7GrZc528.png" height="82" />
然後將備份的壓縮檔遠程拷貝到目標電腦進行還原
scp DB12016-08-22_16.46.17.sql.gz [email protected]:/
650) this.width=650;" title="clip_image008" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image008" src="http://s3.51cto.com/wyfs02/M00/86/53/wKioL1e7-QLBwuNwAAAp8b5Ngio580.png" height="32" />
還原資料庫;
我們需要注意的是我們192.168.6.38伺服器只安裝了mysql服務及配置了賬戶及密碼;所以我們只需要將192.168.6.28上的mysql備份資料還原到192.168.6.38上即可;
拷貝資料完成後,我們使用解壓的方式將壓縮檔解壓到資料庫資料檔案路勁即可;
我們在192.168.6.38上,進入資料庫資料檔案路勁
cd / var/lib/mysql
650) this.width=650;" title="clip_image009" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image009" src="http://s3.51cto.com/wyfs02/M01/86/53/wKioL1e7-QODuA4gAAGaSCOfGFs675.png" height="303" />
從壓縮檔直接恢複:
gunzip < DB12016-08-22_16.46.17.sql.gz | mysql -u root -p <databasename>
如果目標沒有備份的資料庫名稱的話,需要建立一個跟備份資料庫一樣的庫;
650) this.width=650;" title="clip_image010" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image010" src="http://s3.51cto.com/wyfs02/M01/86/54/wKiom1e7-QOxGHyDAAAy2x-4Vwo016.png" height="54" />
註:DB1是需要還原的資料庫資訊;同時需要吧還原檔案拷貝到資料庫檔案路勁
ls /var/lib/mysql/
650) this.width=650;" title="clip_image011" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image011" src="http://s3.51cto.com/wyfs02/M02/86/54/wKiom1e7-QSiiDCMAABouu_AUgE492.png" height="94" />
gunzip < DB12016-08-23.sql.gz | mysql -uroot -p DB1gunzip < DB12016-08-22_16.46.17.sql.gz | mysql -u root -p <databasename>
650) this.width=650;" title="clip_image012" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="clip_image012" src="http://s3.51cto.com/wyfs02/M02/86/53/wKioL1e7-QSTZwQlAABSS9Yy9p0416.png" height="68" />
最後我們查詢一下
select * from info;
650) this.width=650;" title="clip_image013" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image013" src="http://s3.51cto.com/wyfs02/M00/86/53/wKioL1e7-QXiFr1HAAC3UCX4gWg219.png" height="292" />
本文出自 “高文龍” 部落格,謝絕轉載!
Centos 6.4下MySQL 備份及還原詳情