PHP - Backup MySQL code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

PHP - Backup MySQL code

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP - Backup MySQL code

Post 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
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: PHP - Backup MySQL code

Post 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
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: PHP - Backup MySQL code

Post 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");
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP - Backup MySQL code

Post 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).
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: PHP - Backup MySQL code

Post 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.
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: PHP - Backup MySQL code

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP - Backup MySQL code

Post 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'
Post Reply