1

Hye, I'm new in PHP and trying to use mysqldump using php script.
I already try using command and the dump process is success.
The situation is, when I tried dump using my local computer, the dump is succeed.
But when the code is transfer into the server, mysqldump doesn't work. I have tried almost the solution related to mysqldump topics, but still it doesn't work. I hope someone can guide me. TQ

<?php

/*-----------------------------------------------
MYSQLDUMP FOR SERVER
------------------------------------------*/


$dbhost = "*****";
$dbuser = "*****";
$dbpass = "*****";
$dbname = "*****";

//set date today
$today=date("d-m-Y");

//set file name
$filename = "leave_".$today.".sql";

//MYSQLDUMP 

//For Server 
$command = sprintf("/usr/bin/mysqldump --opt -h%s -u%s -p%s %s     >/var/www/html/leave/leave/backup/%s",

//for local
//$command = sprintf("c:\AppServ\MySQL\bin\mysqldump --opt -h%s -u%s -p%s %s > %s",

$dbhost,
$dbuser,
$dbpass,
$dbname,
$filename
);
system($command);



/*-------------------------------------------------------------
TO SAVE FILE SQL INTO LOCAL
---------------------------------------------------------------*/

$file = "leave_".$today.".sql";
if(!$file)
{
   // File doesn't exist, output error
      die('File not found');
}
else
{
// Set headers to ask user where to save file.
header('Pragma: anytextexeptno-cache', true);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=$file");

 // Read the file from disk
 readfile($file);


 }



 ?>
6
  • do you have any errors or anything at all? why would you like to do a mysqldump?, you know that for doing this you need a lot of privileges to be open on the server? Commented Nov 5, 2014 at 4:53
  • Check the permission whether system command can be executed from php or not. Many times, system and exec commands do not work in php files due permission problem. Commented Nov 5, 2014 at 4:55
  • @Saikios there is no error, it just there is no dump file at all. I have tried echo the the $command, still no error. for your 2nd question, I got task from my supervisor to backup database using php script. So i have tried mysqldump and query, the only one success is mysqldump,both when run at local. The problem occur when the script is transfer into server, of course i already change the username and password also. Commented Nov 5, 2014 at 5:00
  • @WisdmLabs I also tried to use shell_exec() and exec() command but the problem still exists. Commented Nov 5, 2014 at 5:02
  • what you want to do is not a backup through a php script, it's a backup through a bash script but you are masking it with php, anyways check this out ;) stackoverflow.com/questions/3751069/… Commented Nov 5, 2014 at 5:09

2 Answers 2

1

Okay, actually I already solve this issues a few days later after I post the question. Unfortunately, I didn't have enough reputation to post an answer.So here we go.

Here are a few things that need to TAKE NOTE:-

  1. Compulsory to use absolute path for MySQL dump.
  2. Try using --opt (default option for MySQL dump). Read more here.
  3. For the option, if use short form, no need to have (--). eg: --p. Use (--) when use full form. eg: --password. So use '-p' instead of '--p' (applied for others option too).
  4. try 'shell_exec' or 'system' if mysqldump doesnt work on 'exec'.
  5. try avoid have space between option and variable. Eg: -p$dbpass

*Also bear with mind, it involves with permission whether system command can be executed from php or not.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding the full path for mysqldump command was the change that worked for me, as I tried to systemd-automate a DB backup.
0

I assume your php permits execution of commands through system().

you are not running in safe mode, or, if you are, safe_mode_exec_dir contains all the commands you need

2 Comments

I dont know why this 'safe-mode' can affect the code but some articles state that the safe mode being 'on' can affect mysqldump. So, I check it, it is already in 'off' mode.. can you elaborate more about what you mention above?
If Safe mode is off than you could ignore my reply. I will further try to check and see if I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.