开发者

Need export query rather than creating file for mysqldump without triggers

开发者 https://www.devze.com 2022-12-29 09:10 出处:网络
I have code as $db_name = \"db\"; $outputfile = \"/somewhere\"; $new_db_name = \'newdb\'; $cmd = \'mysqldump --skip-triggers %s > %s2>&1\';

I have code as

$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 to import:

$cmd = 'mysql --databas开发者_运维问答e=%s < %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($new_db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret); 
//etc.

unlink($outputfile);

But here what should i do to get the export query, rather than creating a file everytime?

EDITED:

IN THE RESPONSE OF REPLY OF MATT

  • I am using Windows, will the code u have given work as I am not sure the code I already have will work in Windows as it seems to be linux commands?
  • Its my need to script in PHP its the process which is going to be triggered when installing a component in joomla
  • What is PIPE in PHP?


Do you have to do this in PHP? You can easily do this in a terminal with some basic piping (no need to create any files).

Two commands and you're done:

echo 'create database foo2' | mysql -uroot
mysqldump --skip-triggers -uroot foo | mysql -uroot foo2

This copies foo into new database foo2

Note that it is possible to pipe in php .. but .. really, you should just do this in shell (IMO).

Edit:

Oops, for some reason I thought you wanted triggers. Edited above to include --skip-triggers

Edit 2:

'Piping' refers to routing stdout and stdin.

Windows supports pipes. The above commands are working for me on Windows.

However, I am not able to get piping to work through PHP with Windows (at least not for mysql). It's like the streams from proc_open ignore my input.

So the alternative would be to take the commands I provided above, put them in a .bat file, and see if you can call that script via system()

Final edit:

You're going to need to do some research as I can't explain every small detail. Did you reference the system() documentation I pointed to? It lets you call system commands ..

My suggestion is you make a basic script, call it bla.bat:

@echo off
echo create database foo2 | mysql -uroot
mysqldump --skip-triggers -uroot foo | mysql -uroot foo2

If that's in the same folder as your script, then your script can do this:

<?php
system('cmd.exe /C ' . dirname(__FILE__).'\\bla.bat');

Viola. DB copied.

0

精彩评论

暂无评论...
验证码 换一张
取 消