實(shí)現(xiàn) win2003 下 mysql 數(shù)據(jù)庫(kù)每天自動(dòng)備份
1. 環(huán)境: windows server 2003 + PHP5 + MySQL 5.1.6.
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:
代碼如下:
<? //保存目錄,路徑要用反斜杠.您需要手動(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__)."\\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"); ?>
或者用下面的代碼:
<? //保存目錄,路徑要用反斜杠.您需要手動(dòng)建立它. $store_folder = ‘D:\database_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.\n”); @mkdir($nowdir); mysql_connect(“l(fā)ocalhost”,”$db_username”,”$db_password”); $query=mysql_list_dbs(); while($result=mysql_fetch_array($query)){ system (dirname(__FILE__).'\..\mysql\bin\mysqldump –opt ‘.”$result[Database] -u{$db_username} “.($db_password?”-p{$db_password}”:””).” > $nowdir\\$result[Database].sql”); echo “dumping database `$result[Database]`…\n”; } echo “\nWinrar loading…\n”; system( dirname(__FILE__).”\\WinRAR\\WinRAR.exe a -ep1 -r -o+ -m5 -df \”$nowdir.rar\” \”$nowdir\” ” ); //刪除 5 天前的文件 @unlink(“$store_folder\\”.date(“Ymd”,$time-86400*5).”.rar”); echo “\nOK!\n”; ?>
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)整.
原作者:faisun
Website: http://www.softpure.com
相關(guān)文章
PHP實(shí)現(xiàn)數(shù)組遞歸轉(zhuǎn)義的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組遞歸轉(zhuǎn)義的方法,包含了數(shù)組的遞歸調(diào)用與字符串的轉(zhuǎn)義方法,需要的朋友可以參考下2014-08-08PHP中Header使用的HTTP協(xié)議及常用方法小結(jié)
這篇文章主要介紹了PHP中Header使用的HTTP協(xié)議及常用方法,包含了各種錯(cuò)誤編碼類(lèi)型及其含義,需要的朋友可以參考下2014-11-11php中的路徑問(wèn)題與set_include_path使用介紹
這篇文章主要介紹了php中的路徑問(wèn)題與set_include_path,需要的朋友可以參考下2014-02-02PHP顯示今天、今月、上月、今年的起點(diǎn)/終點(diǎn)時(shí)間戳的代碼
PHP顯示今天、今月、上月、今年的起點(diǎn)/終點(diǎn)時(shí)間戳的代碼,需要的朋友可以參考下。2011-05-05