1. Environment: windows server 2003 + PHP5 + MySQL 5.1.6.
2. Assume that the PHP installation directory is D:/php and the MySQL installation directory is D:/mysql.
3. Under D:/php Create the directory WinRAR, and copy WinRAR.exe and RARReg.KEY in your winrar installation directory to D:/php/WinRAR.
4. Create the file mysql_backup.php under D:/php:
The code is as follows:
//Save directory, the path must use backslashes. You need to create it manually.
$store_folder = 'D:databse_backup';
//Username and password
//This account must have permission to operate [all] databases and FILEs
//Otherwise some databases cannot be backed up.
$db_username = "root";
$db_password = "";
$time=time();
$nowdir = "$store_folder\".date("Ymd",$time)."";
if( file_exists("$nowdir.rar")) die("File exists.rn");
@mkdir($nowdir);
mysql_connect("localhost","$db_username","$db_password ");
$query=mysql_list_dbs();
$command = '';
while($result=mysql_fetch_array($query)){
$command .= dirname(__FILE__).'..mysqlbinmysqldump --opt '."$result[Database] -u{$db_username} ".($db_password?"-p{$db_password}":"")." > $nowdir \$result[Database].sql rn";
$command .= "echo dumping database `$result[Database]`... rn";
}
$command .= "echo Winrar loading ...rn";
$command .= dirname(__FILE__)."\WinRAR\WinRAR.exe a -ep1 -r -o+ -m5 -df "$nowdir.rar" "$nowdir" rn ";
$command .= "echo OK!rn";
$command .= "del mysqldumping_temp.batrn";
$fp = fopen('mysqldumping_temp. bat','w');
fwrite($fp,$command);
fclose($fp);
//Delete files 5 days ago
@ unlink("$store_folder\".date("Ymd",$time-86400*5).".rar");
?>
5. D:/php Create the file mysql_backup.bat, the content is:
echo off
php.exe mysql_backup.php
if exist mysqldumping_temp.bat call mysqldumping_temp.bat
6. Double-click the bat file to run, if it can be backed up , OK, next step is to add the task plan.
7. Add D:/php/mysql_backup to the task plan, and select the time every day. According to the monitoring results of the server, 5-8 am every morning is the low peak period of traffic. Because 5 - There is some database cleaning work at 7 o'clock, so I set the time at 8 o'clock in the morning.
Original author: faisun
Website: http://www.softpure.com