Solved

Use System Function to  execute a mysqldump

Posted on 2008-10-30
9
326 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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.

919 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

18 Experts available now in Live!

Get 1:1 Help Now