I am trying to make a backup of a mysql database from php. The file is created but it's empty. I can't figure out why. This is the php script I use.

$db_host = 'localhost';
$db_name = '********';
$db_user = '********';
$db_pass = '********';
$conn = mysql_connect($db_host, $db_user, $db_pass)or die('Error connecting to mysql');
mysql_select_db($db_name);
$command = "mysqldump -u $db_user -p $db_pass $db_name > 'backupfile.sql'";
system($command);

    does it work from a shell prompt?

      This code change fixed my problem.

      $db_host = 'localhost';
      $db_name = '********';
      $db_user = '********';
      $db_pass = '********';
      $conn = mysql_connect($db_host, $db_user, $db_pass)or die('Error connecting to mysql');
      mysql_select_db($db_name);
      $filename = $db_name . "_" . date("Y-m-d_H-i-s") . '.sql.gz';
      $command = "/usr/local/mysql/bin/mysqldump --opt --skip-extended-insert --complete-insert --host=".$db_host." --user=".$db_user." --password=".$db_pass." ".$db_name." | gzip > ".$filename;
      system($command);

        Out of curiosity, why do you have these lines:

        $conn = mysql_connect($db_host, $db_user, $db_pass)or die('Error connecting to mysql'); 
        mysql_select_db($db_name); 

        ?

          Well, the correct answer here is. I don't need them. Thanks for pointing that out :-)

            Write a Reply...