Solved

Use System Function to  execute a mysqldump

Posted on 2008-10-30
9
325 Views
Last Modified: 2013-12-13
I am trying to create a mysqldump in PHP:

----------
$command = "mysqldump -h localhost --user=me --password=mypass --no-create-info --opt --where=\"Id<12\" MyDatabase MyTable |sed -e \"s|INSERT INTO|REPLACE INTO|\" > ".$path."replacedump.sql";
system($command,$error);
-------------

The dump file is not being executed.

I can take the string for $command and put in straight to the command line and it works. Then I thought maybe it was the system() function, so I create system("ls",$error); in PHP and it worked, I got the file list in my browser.

Is there a reason why system() is not working with mysqldump. I should have permission to run it because I can do it on the command line. Does PHP read permissions differently? Or is there something wrong with my mysqldump?

I appreciate any help!
0
Comment
Question by:SaltyDawg
  • 5
  • 4
9 Comments
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22841444
I've also used exec() and passthru() and none worked.
0
 
LVL 26

Expert Comment

by:ushastry
ID: 22841631
This may help you..

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23025392.html

Also, can you tell me whether safe_mode is enabled in php.ini?

Thanks,
Umesh
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22841860
Hello Umesh
thanks again for your help

safe_mode and safe_mode_gid are off
and
safe_mode_exec_dir and safe_mode_include_dir have no value
and
sql.safe_mode is off


my error always returns 1
0
 
LVL 26

Expert Comment

by:ushastry
ID: 22842288
Can you please try this way... lets start one-by-one.. first will see whether it dumps or writes any error...

Once you execute below code.. then from command line check whether the file /tmp/mydump.sql is created...


<?php
 

$command = "mysqldump -h localhost --user=root --password=123 --no-create-info --opt --where=\"Id<12\" MyDatabase MyTable 2>&1 > /tmp/mydump.sql";

system($command,$error);
 

?>

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 26

Expert Comment

by:ushastry
ID: 22842411
Just checked below code and it works for me..

PHP details..

$ php -v
PHP 5.0.4 (cli) (built: Mar 17 2006 15:37:24)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies


Pls change required info from below...script.. hostname, user,pass, DBname, table etc
<?php
 

$command = "mysqldump -h xx.xx.xx.xx --user=me --password=secret --no-create-info --opt --where=\"id <12\" myDB myTable |sed -e \"s|INSERT INTO|REPLACE INTO|\" 2>&1 > /tmp/mydump.sql";

system($command,$error);
 

?>

Open in new window

0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22843139
Yes it worked for me to. Apparently from PHP that's only directory I can write to. I can't even write to my my Home dir. I'm guessing from PHP I'm not logged in as myself. Is there a command I can run to log in?
0
 
LVL 26

Accepted Solution

by:
ushastry earned 500 total points
ID: 22843271
Command...not sure will check and let you know...

BTW, you can do onething create a directory under your home or anywahere you want and chmod it to 777

chmod -R 777
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22843420
Yea I figured out the chmod, thanks
0
 
LVL 1

Author Closing Comment

by:SaltyDawg
ID: 31511676
Thanks again.

But keep an eye out for me again I may have one more thing to do regarding scp to another host with php
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now