Forum Moderators: coopster

Message Too Old, No Replies

DB Backup with PHP and mysqldump

         

jackvull

10:02 am on Jul 28, 2005 (gmt 0)

10+ Year Member



Hi I'm running this from PHP but it doesn't seem to put anything into the dump.sql file. Any ideas on what could be wrong?
I have set up permissions to write to the file, no error messages are encountered and all the username and password settings for the mysqldump command are correct:

<?php

ob_start();

$username = "";
$password = "";
$hostname = "localhost";
$sConnString = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

//create a connection object
$connection = mysql_select_db("dbname",$sConnString)
or die("Could not select DB");

$command = "mysqldump --add-drop-table --host=localhost --user='' --password='' dbname";
system($command);

$dump = ob_get_contents();
ob_end_clean();

//The database dump now exists in the $dump variable... write it to a file using...

$fp = fopen("dump.sql", "w");
fputs($fp, $dump);
fclose($fp);

?>

By the way, the --user='' bits are single quotes - I don't think this shows up on the post very well...might look like double quotes.
Thanks

jatar_k

3:26 pm on Jul 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume you have tested your dump command from the commandline to be sure it works

are you getting anything at all in $dump?

I would try echo'ing it to the browser until it is working. system may only return the last line

you might want to look at passthru as another option

jackvull

4:14 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



Do you mean the command line from mysql?

jatar_k

4:20 pm on Jul 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



mainly I am wondering if yoou have tried that command to make sure it works and then you can cross that off the 'possible problems' list

kepplah

6:45 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



Make sure that your php script has proper permissions to allow the user that apache is running as (often nobody) to execute shell permissions. So set the owner to nobody and set nobody to have executable permission for that script.

jackvull

3:03 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Nothing the $dump variable at all when I try to echo it.
How do I get access to the command line from a Windows OS? Is it the MySQL command line.
I'm thinking the system() bit in the PHP might need the path to mysqldump?

kepplah

3:44 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Try including the path to the mysqldump program. And to get a prompt in windows press the windows key and r (for a run prompt) and type cmd.

jackvull

4:05 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



ah...that command line.
I have gone to the path by navigating to the directory cd mysql, etc.
I issue the mysqldump command with those parameters and get an error:
mysqldump: Got error: 1146: Table 'dbname.;' doesn't exist when doing lock tables.

That doesn't make sense as dbname is the database name...

jackvull

4:11 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Sorry, that's because I've got the ; symbol at the end, which isn't needed in this case.
Works from the command line so I'll retry from the script with paths included.

jackvull

8:44 am on Aug 1, 2005 (gmt 0)

10+ Year Member



Right I have tried this from the command line and a backup file is produced successfully:
mysqldump --add-drop-table --host=$hostname --databases dbname > tester.sql

However when I put this into the PHP script, nothing happens, so I'm wondering if there's something with my path format or if this isn't picked up by the system() function:
$command = "\Program Files\MySQL\MySQL Server 4.1\bin\mysqldump --add-drop-table --host=$hostname --databases dbname > tester.sql";
system($command);

Thanks

jatar_k

2:44 pm on Aug 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you maybe tried passthru or exec instead to see if it is just the system function?

jackvull

9:19 am on Aug 3, 2005 (gmt 0)

10+ Year Member



Tried both of these and that doesn't work.
It must be something to do with the path but I can't work out what.
Providing that path to the system function should run it directly shouldn't it (the $command variable)?
When I bring up the command line manually it defaults to a C:\Windows path so I have to do cd.. to get back to C: and then put in the proper program files path to mysqldump.
Could this cause the problem from my $command variable and if so, is there a way round it?

jatar_k

4:26 pm on Aug 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you run it in a command window, which should give you the default path. Try getting it to work with out doing the cd, that should allow it to work with your script.