简体   繁体   中英

backup mysql database in php using mysqldump

I have this code, that uses mysqldump to backup mysql database. The problem is I'm getting this fatal error:

Fatal error: Maximum execution time of 60 seconds exceeded in C:\\wamp\\www\\pos\\php\\backupdb.php on line 13

Line 13 is the final line.

<?php
$backupFile = 'c:\\onstor'. date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump --opt -u root -p onstor > $backupFile";
system($command);
?>

What do I do, I think the code is okay since I've tried it in command prompt and it worked. Is it bad that I have put the path to mysql/bin to the environment variables.

The problem, as the error message states pretty plainly, is that your script is running for too long. Scripts executing through a web server are not meant to run longer than a few seconds. You can change that using set_time_limit , but what you should really do is let long running scripts run from the command line. Since the only thing you're doing is running a CLI command anyway, just ditch the PHP wrapper completely. Make it a shell script if necessary. Run this shell script regularly as a cron job/Windows Scheduler task (or whatever the Windows equivalent is called).

Your code looks okay. Dumping is taking lot of time. that's all. Read this Fatal error: Maximum execution time of 400 seconds exceeded . Do what is written there first and then look for any problem in your code.

The message say you reached out the 60 seconds excution time.

You can change it using the set_time_limit function, ie:

 set_time_limit(120); // 2 minutes

but I don't know why you would try to use mysqldump in PHP, it seems dangerous to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM