How can we use mysqldump on another db using a PHP script so that triggers don't get dumped along with it?

How can we use mysqldump on another DB with a php script so that triggers don't get dumped along with it?

+2


a source to share


1 answer


Something like this should work:

$db_name = "db";
$outputfile = "/somewhere";
$new_db_name = 'newdb';

$cmd = 'mysqldump --skip-triggers %s > %s  2>&1';
$cmd = sprintf($cmd, escapeshellarg($db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret); 
if ($ret !=0 ) {
    //log error message in $output
}

      

Then for import:

$cmd = 'mysql --database=%s < %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($new_db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret); 
//etc.

unlink($outputfile);

      



Please note that you first need to create a new database. You will also likely need to provide a username and password for each command.

change

You can also do it with a single command, eg.

exec('mysqldump --skip-triggers sourcedb | mysql --database targetdb 2>&1', $output, $return);

      

+1


a source







All Articles