Implementation of Win2003 MySQL database daily automatic backup _php skills

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.