Roland
Roland

Reputation: 9731

MySql Command mysqldump not executing?

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.

Upvotes: 1

Views: 672

Answers (1)

VolkerK
VolkerK

Reputation: 96179

"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.

Upvotes: 2

Related Questions