LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2016, 03:57 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
Using mysqldump via PHP system($command, $retval) does not work.


Including code for two versions of mysqldump.

I run file1 as php -f file1.php and it works just fine.

I run file2 through an AJAX call to file2.php and I get a retval of 2 from the system() call.

I have set permissions on the directory and files to 777 so this shouldn't be a file permission problem.

Appreciate help in fixing this so I can use the file2 version.

Thanks, R

Code:
// =============== file1 ====================


<?php

//Show errors
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('html_errors', 'On');

$dbhost='localhost';
$dbuser='rick';
$dbpass='rick';
$dbname='pizzidb';

//$dbname=trim($_POST['db']); // call in progress through $.ajax
//$dir=trim($_POST['dir']);

$db='pizzidb';
$dir='/home/rick/DB-Web/pizzidb';

$format="m.d.y_G.i.s"; // month, day, year _ hr, min, sec

$backupFile=$dir. "/".rtrim(strtoupper($db))."_DUMP_". date($format).".sql.gz";

$command = "/usr/bin/mysqldump --opt --add-drop-database --databases -h". $dbhost. " -u". $dbuser. " -p".$dbpass.
" ". $dbname. " | gzip > ". $backupFile;

$bak_result = system($command, $retval);

echo "retval = ".$retval."\n\n";

?>

// =========================== file2 =======================


<?php

//Show errors
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('html_errors', 'On');

$dbhost='localhost';
$dbuser='rick';
$dbpass='rick';

$dbname=trim($_POST['db']); // call in progress through $.ajax
$dir=trim($_POST['dir']);

$format="m.d.y_G.i.s"; // month, day, year _ hr, min, sec

$backupFile=$dir. "/".rtrim(strtoupper($dbname))."_DUMP_". date($format).".sql.gz";

$command = "/usr/bin/mysqldump --opt --add-drop-database --databases -h". $dbhost. " -u". $dbuser. " -p".$dbpass.
" ". $dbname. " | gzip > ". $backupFile;

$bak_result = system($command, $retval);

if ( $retval != 0) {

echo "bad";
}
else {

echo "File is stored in: ". $backupFile."\n";
}


?>
 
Old 05-15-2016, 06:29 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
In my humble opinion, mysqldump is not a tool that should properly be executed by an HTTP web-server in response to an AJAX request.
 
Old 05-16-2016, 03:49 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: you could help explicitly showing the difference between the two programs. If it is about '$retval' being zero or not, use var_dump to print its value.

And, indeed, PHP should not be involved in database-dumping; use ssh to login, and some shell to parametrize program mysqldump.
 
Old 05-16-2016, 11:43 AM   #4
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks,

I'll think about not using PHP for this in the future.

R.
 
Old 05-17-2016, 12:33 PM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Found my problem. I had all the files with permission 777 but NOT the directory they were located in. I set the directory to 777 and it now works.

NOW, I've thought about using a bash script (backup.sh) to do this and in the reading I have done, it appears you can't call say, bakup.sh, from html (at least easily). Also I don't really understand why my plan to use PHP to do this is not the thing to do.

I am doing this so I can have a menu with Radio Buttons, 'Backup Database' and 'Restore Database', in my HTML file. This eliminates grubbing around in my files looking for a shell script to backup my data.

In fact this whole security thing is freaking me out. Here are several questions that I hope you will answer.
  • If my computer is connected to the internet (wifi) and not browsing any particular site is it open to attack?
  • If I am connected to some particular internet site (wifi) is my computer open to attack?
  • If my computer is connected to the internet (wifi) but 'hibernating' is it open to attack?

All my use is in regards to my 'Home Database'. However, I am presently encrypting portions of my data in MySql and setting up passwords to access my html database programs.

Appreciate additional help on this.

R
 
Old 05-17-2016, 04:14 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Any connected computer is open to attack, by using GNU/Linux OS you raise the difficulty to do so, by using a firewall you raise security a little more etc...

The problem using mysqldump remotely with php is connection wise I think, how stable is this connection, how secure and how much time php is allowed to keep the connection (max_execution_time)
 
Old 05-19-2016, 04:47 PM   #7
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks keefaz,

I am running Ubuntu 14.04. The programs and data are contained on my computer. I have LAMP set up and use a virtual host with Apache2. I am in the process of encrypting the data in my MySql databases. The actual dump of my data takes milleseconds.

R
 
Old 05-19-2016, 08:15 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Another security concern is that the javascript is executed on client side so the path to php script will be known, opening access to a ddos atack for example (executing mysqldump many times).

Last edited by keefaz; 05-19-2016 at 08:16 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP/MYSQL - mysqldump creates file with commented out instructions. pizzipie Programming 4 09-11-2013 01:25 PM
transferring mysql database without mysqldump command alejandroye Linux - Server 7 06-18-2013 08:19 PM
Ping using PHP exec("ping -n 1 $ip",$output[],$retval); bianchi77 Linux - Networking 5 04-25-2010 09:11 PM
[Mysql] mysqldump doesn't work when restoring data Kunsheng Linux - Software 1 09-25-2009 11:24 PM
Mysqldump Command line question wh33t Linux - General 1 05-05-2006 06:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration