終于讓服務(wù)器每天早上備份一次 mysql 數(shù)據(jù)庫(kù)并自動(dòng)打包,同時(shí)刪除 5 天前的備份文件. 分享如下.
1. 環(huán)境: windows server 2003 + apache 2.0 + php5 + mysql 4.0.26 .
2. 假設(shè) php 安裝目錄為 d:/php ,mysql 安裝目錄為 d:/mysql.
3. 在 d:/php 下建立目錄 winrar, 把你 winrar 安裝目錄下的 winrar.exe 和 rarreg.key 復(fù)制到 d:/php/winrar .
4. d:/php 下建立文件 mysql_backup.php:
<?
/*/////////////////////////
#filename: mysql_backup.php
#author: faisun
#website: http://www.softpure.com
////////////////////////*/
//保存目錄,路徑要用反斜杠.您需要手動(dòng)建立它.
$store_folder = 'd:/databse_backup';
//用戶名和密碼
//該帳號(hào)須有操作[所有]的數(shù)據(jù)庫(kù)及file的權(quán)限
//否則有些數(shù)據(jù)庫(kù)不能備份.
$db_username = "root";
$db_password = "";
$time=time();
$nowdir = "$store_folder//".date("ymd",$time)."";
if(file_exists("$nowdir.rar")) die("file exists./r/n");
@mkdir($nowdir);
mysql_connect("localhost","$db_username","$db_password");
$query=mysql_list_dbs();
$command = '';
while($result=mysql_fetch_array($query)){
$command .= dirname(__file__).'/../mysql/bin/mysqldump --opt '."$result[database] -u{$db_username} ".($db_password?"-p{$db_password}":"")." > $nowdir//$result[database].sql /r/n";
$command .= "echo dumping database `$result[database]`... /r/n";
}
$command .= "echo winrar loading.../r/n";
$command .= dirname(__file__)."http://winrar//winrar.exe a -ep1 -r -o+ -m5 -df /"$nowdir.rar/" /"$nowdir/" /r/n";
$command .= "echo ok!/r/n";
$command .= "del mysqldumping_temp.bat/r/n";
$fp = fopen('mysqldumping_temp.bat','w');
fwrite($fp,$command);
fclose($fp);
//刪除 5 天前的文件
@unlink("$store_folder//".date("ymd",$time-86400*5).".rar");
?>
5. d:/php 下建立文件 mysql_backup.bat,內(nèi)容為:
@echo off
php.exe mysql_backup.php
if exist mysqldumping_temp.bat call mysqldumping_temp.bat
6. 雙擊該 bat 文件運(yùn)行,如果能備份了,ok,下一步添加任務(wù)計(jì)劃.
7. 把 d:/php/mysql_backup 添加到任務(wù)計(jì)劃,時(shí)間選每天. 根據(jù)服務(wù)器的監(jiān)測(cè)結(jié)果,每天早上 5-8 時(shí)為流量低峰期. 由于 5-7 時(shí)有些數(shù)據(jù)庫(kù)的清理工作,我把時(shí)間定在了早上 8 點(diǎn)整.
新聞熱點(diǎn)
疑難解答
圖片精選