Home >Backend Development >PHP Tutorial >Automatic daily backup of MySQL database under Windows 2003_PHP tutorial
Finally, let the server back up the MySQL database every morning and automatically package it, while deleting the backup files 5 days ago. Share as follows:
1. Environment: windows server 2003 + Apache 2.0 + PHP5 + MySQL 4.0.26.
2. Assume that the PHP installation directory is D:/php and the MySQL installation directory is D :/mysql.
3. Create the directory WinRAR under D:/php, 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:
/*/////////////////////////
#FileName: mysql_backup.php #Author: faisun #Website: http://www.softpure.com
/////////////////////////* /
//Save the directory, use backslashes in the path. You need to build it manually.
$store_folder = 'D:databse_backup';
//Username and password
//This account must have permission to operate [all] databases and FILE
//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__).'..mysqlbinmysqldump --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 files 5 days ago
@unlink(“$store_folder\ “.date(“Ymd“,$time-86400*5).“.rar“);
echo “nOK!n“; 〉
5. Create the file mysql_backup under D:/php. bat, the content is only one sentence:
php.exe mysql_backup.php
6. Double-click the bat file to run, if it can be backed up, OK, next step is to add a task plan.
7. Add D:/php/mysql_backup to the task plan, and select every day. According to the monitoring results of the server, 5-8 am is the low traffic peak period every morning. Since some database cleaning work is done between 5-7 am, you can The time was set at 8 o'clock in the morning.