Back up the MySQL database every morning and automatically pack and delete the backup files 5 days ago. Share as follows.
1. Environment: Windows Server 2003 + Apache 2.0 + PHP5 + MySQL 4.0.26.
2. Suppose the PHP installation directory is d:/php, the MySQL installation directory is d:/mysql.
3. Create a directory WinRAR under d:/php and copy WinRAR.exe and Rarreg.key from your WinRAR installation directory to D:/php/winrar.
4. Establishment of document Mysql_backup.php under d:/php:
/*/////////////////////////
#FileName: mysql_backup.php #Author: Faisun #Website: Http://www.111cn.net
////////////////////////*/
Save the directory with a backslash. You need to build it manually. $store _folder = ' d:/databse_backup ';
User name and password
The account must have the right to operate [all] databases and file
Otherwise, some databases cannot be backed up.
The code is as follows |
Copy Code |
$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 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 only one sentence:
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. Due to the cleanup of some databases at 5-7, the time can be set at 8 o'clock in the morning.