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);


 }



 ?>
Salehin Rafi
  • 351
  • 2
  • 6
  • 16
  • 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? – Saikios Nov 05 '14 at 04: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. – Domain Nov 05 '14 at 04: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. – Salehin Rafi Nov 05 '14 at 05:00
  • @WisdmLabs I also tried to use shell_exec() and exec() command but the problem still exists. – Salehin Rafi Nov 05 '14 at 05: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 ;) http://stackoverflow.com/questions/3751069/backup-a-mysql-database-and-download-as-a-file – Saikios Nov 05 '14 at 05:09
  • @Saikios haha..sorry for the wrong term. Anyway thank you, I will check it first. ^^ – Salehin Rafi Nov 05 '14 at 06:01

2 Answers2

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.

Salehin Rafi
  • 351
  • 2
  • 6
  • 16
  • Adding the full path for mysqldump command was the change that worked for me, as I tried to systemd-automate a DB backup. – MikeW May 05 '20 at 11:38
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

Kumar Sambhav Pandey
  • 1,713
  • 4
  • 22
  • 33
  • 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? – Salehin Rafi Nov 05 '14 at 06:06
  • If Safe mode is off than you could ignore my reply. I will further try to check and see if I could help. – Kumar Sambhav Pandey Nov 05 '14 at 06:38