command line execution of mysqldump thru PHP

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
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

command line execution of mysqldump thru PHP

Post by psmshankar »

when i run the following in the browser, the progress bar keeps on moving but nothing is coming...

Code: Select all

<?php
$Path="C:\phpdev3\mysql3.23\bin";
$MySQLServer="localhost";
$MySQLUser="";
$MySQLPassword="";
$DBName="test";
$StorePath="C:\mysqlbackup\20030208"

$Command=$Path. "". "mysqldump -h".$MySQLServer." -u".$MySQLUser." -p".$MySQLPassword." --add-drop-table ".$DBName." > ".$StorePath. "" .$DBName.".sql";

passthru($Command,$result);
if ($result)
{
     echo "<br>Error! Database $DBName Not Backed Up";
}
else
{
     echo "Backup Successfull";
}
?>

when i comment the passthru statement then ok... i gave an echo statement to print the variable 'Command'

it printed the following:

c:\phpdev3\mysql3.23\bin\mysqldump -hlocalhost -u -p --add-drop-table test > c:\mysqlbackup\20030208\test.sql

i also tried with 'system' command...but nothing works..

when i typed the same thing in command prompt it works fine...
is there any thing i am missing out...

i am using win98/apache/mysql3.23/php 4
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it is obvious from the output (and the script, too) that $MySQLUser and $MySQLPassword are blank.
Therefor the commandline tool will prompt for user/password and so the process waits (and since there is no further input it waits forever or 'til it's terminated [timeout of php] )
Last edited by volka on Sun Feb 09, 2003 8:45 pm, edited 1 time in total.
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

means i cannot use empty password and username while using mysqldump.... is it?????
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

passing -u and/or -p means "hey mysqldump, here I give you user and password to use." Try without -u and -p

Why do you use --add-drop-table if you do not create tables?
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

no still the same.... it goes on ..on ...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if you copy and paste the echo-output directly to a commandline and execute it what happens?
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

there it is working fine...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I use (short version ;) )

Code: Select all

$user = 'volka';
$pass = '***';
$command = 'c:\\programme\\mysql\\bin\\mysqldump -hlocalhost -u '.$user.' -p'.$pass.' -c -r e:\\pm.sql -B phantasmorgia';
system($command, $retval);
to backup a database without problems.
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

whats that -r stands for???
is that command u specified is used dump in one single file alone? or multiple files ".sql" files will be created for each database?
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

whats that "phantasmorgia" in ur statement...
quite surprised that u didn't use any smileys in the first few posts...but later you did... :lol:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the command is used to dump the complete database phantasmorgia (only an example) to the file e:\pm.sql
mysqldump --help will show you a brief description of all available options.

Usually more smiles mean I had to think more about a text to write (and found nothing that can stand for its own without smiley)
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

when i executed ur command(with appropriate changes) in the command prompt, it says there is no option like '-r'

so i removed that -r option and then run...
this is the command i typed (test is my database):

Code: Select all

c:\phpdev3\mysql3.23\bin\mysqldump -hlocalhost -c c:\mysqlbackup\test.sql -B test
it told me unknown database "c:\mysqlbackup\test.sql"

so i changed the command to the following:

Code: Select all

c:\phpdev3\mysql3.23\bin\mysqldump -hlocalhost -c -B test c:\mysqlbackup\test.sql
here it showed me the entier dump in the command prompt itself rather than creating the test.sql file...

so again i changed the command to

Code: Select all

c:\phpdev3\mysql3.23\bin\mysqldump -hlocalhost -c -B test &gt; c:\mysqlbackup\test.sql
this time it is ok and created the dump file...

but the same thing i run thru php

Code: Select all

<?php
$command = "c:\phpdev3\mysql3.23\bin\mysqldump -hlocalhost -c -B test > c:\mysqlbackup\test.sql";
system($command, $retval);
echo "<br>retval = $retval";
?>
just these 3 lines.... as usual it keeps on running...nothing i get...
i think when i use system command it just not working...
not only the mysqldump command,ubut also when i try to call a notepad.exe thru this command it doesn't work..
is there anything i am missing out...is there anything i need to do in ini file?? wondering..
v
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a common pitfall is that programs that prompt something will hang system()/exec()/... and so will notepad.exe as it tries to create a gui that will not stop until it is manually closed.
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

ok if u say so...ok 5n..
i created a batch file with the name "MMLSDBBackup.bat"
it contains the following:

Code: Select all

REM ******************************************** 
REM ****************** BACKUP ****************** 
REM ******************************************** 

SET PATH="%PATH%";C:\phpdev3\mysql3.23\bin; 
mysqldump --opt test > c:\MMLSDump\Dump.sql 

REM ******************************************** 
REM **************** END BACKUP **************** 
REM ********************************************


and saved in "D:\Shankar" folder...

then in my program i called this to execute

Code: Select all

$command = "D:\\Shankar\\MMLSDBBackup.bat"; 
system($command, $retval); 
echo "<br>retval = $retval";
even then it hangs...didn't do anything...the icon keeps on moving in the browser...

so what do you think about this?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'm clueless but strongly suggest creating an account in mysql for mysqldump and use it

Code: Select all

mysqldump -u user -ppassword ...
Post Reply