Page 1 of 1
PHP - Backup MySQL code
Posted: Fri Sep 25, 2009 10:06 am
by koolsamule
Hi guys,
I have a simple script that backup the contents of a database and place the file on the server.
The problem is that this script works fine on my Apache server, but not on the IIS server (HTTP 500 error).
Code: Select all
<?php
$backupFile = "../DBJobs_" . date("Y-m-d");
$command = "mysqldump --opt -h localhost -u (user) -p(password) dbjobs > $backupFile.sql";
system($command);
if ($command) {
$mess = "<p>Backup file created!</p>";
}
else {
$mess = "<p>There was a problem with the backup routine.</p>";
}
?>
Do I have to change the $command variable or the system function?
Any help would be very appreciated.
Re: PHP - Backup MySQL code
Posted: Fri Sep 25, 2009 11:36 am
by Eran
try providing the full path to the mysqldump executable, its probably not on the include paths. also, check that you are using the right credentials for the server
Re: PHP - Backup MySQL code
Posted: Fri Sep 25, 2009 11:53 am
by koolsamule
Cool, will try that. . .
Credentials are correct, as I've tested it by adding, editing and removing records from a mysql database on the server.
Could you give me an example of the full path for the mysqldump?
Many thanks for your help
Re: PHP - Backup MySQL code
Posted: Fri Sep 25, 2009 12:08 pm
by Mirge
$backupFile = "../DBJobs_" . date("Y-m-d");
That is a relative path. A full path would be (for example):
$backupFile = "/this/is/the/full/path/to/your/mysqldump/files/DBJobs_" . date("Y-m-d");
Re: PHP - Backup MySQL code
Posted: Fri Sep 25, 2009 12:56 pm
by Eran
no, as I said - full path to the mysqldump executable.
Code: Select all
$command = "c:\path\to\dump\command\mysqldump --opt -h localhost -u(user) -p(password) dbjobs > $backupFile.sql";
You can also provide full path to the backup file, but it shouldn't prevent the command from completing.
Also notice that the user parameter needs to be adjacent to the -u parameter (you had a space there).
Re: PHP - Backup MySQL code
Posted: Fri Sep 25, 2009 1:08 pm
by Mirge
pytrin wrote:no, as I said - full path to the mysqldump executable.
Code: Select all
$command = "c:\path\to\dump\command\mysqldump --opt -h localhost -u(user) -p(password) dbjobs > $backupFile.sql";
You can also provide full path to the backup file, but it shouldn't prevent the command from completing.
Also notice that the user parameter needs to be adjacent to the -u parameter (you had a space there).
doh i mis-read. I'd still specify a full path to the dump path regardless though.
BTW, you'll need to escape your backslashes.
Re: PHP - Backup MySQL code
Posted: Mon Sep 28, 2009 1:18 pm
by koolsamule
Hi Chaps,
Thanks for your help, I have the correct credentials and have tested the mysqldump function on the server manually and it writes to the directory no problem.
Is there anything I can test to see if the system function is working correctly, e.g. replace the $command variable with something simple?
Cheers
Re: PHP - Backup MySQL code
Posted: Mon Sep 28, 2009 1:29 pm
by Eran
Generally I use exec() and not system(). Either way, the process running it (probably apache) might not have sufficient permissions. Check the output from the system() function call for more information on what's going on. Try running it with a basic command such as 'ls'