1. Environment: windows server 2003 + php5 + mysql 5.1.6.
2. Assumption PHP installation directory is D:/php ,MySQL install directory is D:/mysql.
3. in d :/php directory WinRAR, copy WinRAR.exe and RARReg.KEY from your winrar installation directory to D:/php/WinRAR .
4. d:/php The file mysql_backup.php:
code below:
?
Save the directory with a backslash. You need to build it manually.
$store _folder = ' D:\databse_backup ';
Username and password//The account must have operation [all] database and file permissions//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.\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); Delete the file @unlink 5 days ago ("$store _folder\\". Date ("Ymd", $time -86400*5). ".
RAR "); ?>
or use the following code:
?
Save the directory with a backslash. You need to build it manually.
$store _folder = ' D:\database_backup ';
Username and password
//The account must have operation [all] database and file permissions
//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.\n");
@mkdir ($nowdir);
mysql_connect ("localhost", "$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" ");
Delete the file @unlink 5 days ago
("$store _folder\\". Date ("Ymd", $time -86400*5). ". RAR ");
echo "\nok!\n";
? >
5. The d:/php establishes the document Mysql_backup.bat, the content is:
echo off
Php.exe mysql_backup.php
6. Double-click the bat file to run, if you can back up, OK, next add the task plan.
7. Add the D:/php/mysql_backup to the task schedule and select the time each day. According to the monitoring results of the server, 5-8 hours per morning for the low peak traffic. I set the time at 8 o'clock in the morning due to the cleanup of some databases at 5-7.
Original Author: Faisun
Website:http://www.softpure.com