1

So I'm using PHP to execute a mysql command to backup / restore a database. On the localhost it worked without problems because I had to pinpoint to the executable. But on the server it doesn't work. This is what I'm using as PHP code:

public function backup() {
    $backup = $this->backups.'/'.$this->db_name.'_backup_'.date('Y').'_'.date('m').'_'.date('d').'.sql';
    $cmd = "mysqldump --opt -h $this->db_host -p $this->db_pass -u $this->db_user $this->db_name > $backup";
    try {
        system($cmd);
        return $this->encode('Error',false,'Backup Successfuly Complete');
    } catch(PDOException $error) {
        return $this->encode('Error',true,$error->getMessage());
    } 
}

public function restore($backup) {
    $cmd = "mysql --opt -h $this->db_host -p $this->db_pass -u $this->db_user $this->db_name < $backup";
    try {
        exec($cmd);
        return $this->encode('Error',false,'Restore successfuly complete');
    } catch(PDOException $error) {
        return $this->encode('Error',true,$error->getMessage());
    } 
}

Please make abstraction of any variable that is there, I'm looking to find out why isn't the command working on server.

Also how do I check with PHP if the command was actually executed properly ? Because with try method I always get a positive answer.

4
  • Do you have the appropriate writing permissions in the server? Commented Mar 23, 2012 at 11:48
  • Print the $cmd value and try to run it manually on the server and report the error message here.. Commented Mar 23, 2012 at 11:49
  • @barsju - You mean in phpMyAdmin ? I'll try to run it there and see what will happen. Commented Mar 23, 2012 at 11:55
  • @wanstein - I'm not sure how to find that out. I'm using Godaddy's Shared Servers ( Linux CentOS ), but I don't see why I wouldn't be able to backup my database. Commented Mar 23, 2012 at 11:56

1 Answer 1

2

"Please make abstraction of any variable that is there" - anyway I hope you make good use of escapeshellarg()

mysqldump without an absolute path might not be found in the webserver process' context, e.g. because it simply isn't in its search-PATH (or isn't present at all).

You might want to try redirecting STDERR to STDOUT by appending 2>&1 to the command to get error messages.

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

1 Comment

Oh, I have found a solution here : serverfault.com/questions/109435/… , it was a matter of the way I sent the password, I had to wrap it in quotes.

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.