Skip to content

Commit

Permalink
fix #69, restore of mysql data not working with PHP>5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
k2s committed Mar 10, 2015
1 parent 89befc1 commit e1053e2
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions storage/Mysql/cliRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'drop-db' => array('switch' => array('drop-db'), 'type' => GETOPT_SWITCH, 'help' => 'will drop DB if exists'),
'no-data' => array('switch' => array('no-data'), 'type' => GETOPT_SWITCH, 'help' => 'skip data import'),
'force-local-server' => array('switch' => array('force-local-server'), 'type' => GETOPT_SWITCH, 'help' => 'force data load to use method optimal for local server'),
'use-mysql-client' => array('switch' => array('use-mysql-client'), 'type' => GETOPT_SWITCH, 'help' => 'data will be loaded by calling mysql client utility (see php bug #62889)'),
'actions' => array('switch' => array('a', 'actions'), 'type' => GETOPT_VAL, 'default' => 'u,f,t,i,d,r,v,p,tr,g', 'help' => <<<TXT
restore actions to execute (default is u,f,t,i,d,r,v,p,tr,g):
u - users
Expand Down Expand Up @@ -190,6 +191,10 @@ class RestoreMysql
* @var string
*/
protected $_originalDbName;
/**
* @var string
*/
protected $_mysqlCli;

const VALIDATE_RESTORE = 0;
const VALIDATE_DECOMPRESS = 1;
Expand Down Expand Up @@ -337,10 +342,14 @@ protected function _connectMysql()
if ($this->_opts['socket']) {
// connect over socket
$dsn = "mysql:unix_socket=" . $this->_opts['socket'] . ";dbname=mysql";
$this->_mysqlCli = "mysql -S " . $this->_opts['socket'];
} else {
// connect over TCP/IP
$dsn = "mysql:host=" . $this->_opts['host'] . ";port=" . $this->_opts['port'] . ";dbname=mysql";
$this->_mysqlCli = "mysql -h " . $this->_opts['host'] . " -P " . $this->_opts['port'];
}
$this->_mysqlCli .= " --local-infile -u " . $this->_opts['user'] . " -p" . $this->_opts['password'];

$this->_db = new DbWrapper(
new PDO(
$dsn,
Expand Down Expand Up @@ -512,8 +521,8 @@ public function restore()
// prepare shorter variable names
$log = $this->_log;
$db = $this->_db;
$opts = & $this->_opts;
$folder = & $this->_backupFolder;
$opts = &$this->_opts;
$folder = &$this->_backupFolder;

// create users
$log->start("USERS create");
Expand Down Expand Up @@ -541,7 +550,7 @@ public function restore()

// drop database if requested
if ($opts['drop-db']) {
$log->start("DB drop");
$log->start("DB drop database $opts[database]");
$this->_db->exec("DROP DATABASE IF EXISTS `$opts[database]`");
}

Expand All @@ -561,6 +570,7 @@ public function restore()

// change to DB
$db->exec("use `$opts[database]`");
$this->_mysqlCli .= " $opts[database]";

// prepare import
$sql = <<<SQL
Expand Down Expand Up @@ -766,6 +776,8 @@ function importDataFromFolderToLocalServer($subPath, $truncate = true)

function importDataFromFolder($isLocalHost, $subPath, $truncate = true)
{
$directClient = $this->_opts['use-mysql-client'];

$path = $this->_backupFolder . $subPath . DIRECTORY_SEPARATOR;
if (file_exists($path) && false !== ($handle = opendir($path))) {
while (false !== ($fn = readdir($handle))) {
Expand Down Expand Up @@ -795,10 +807,14 @@ function importDataFromFolder($isLocalHost, $subPath, $truncate = true)
// TODO LINES TERMINATED BY should maybe be configurable on command line or stored in/db/_config
// ALTER TABLE `$fn` DISABLE KEYS; ALTER TABLE `$fn` ENABLE KEYS;
$local = $isLocalHost ? "" : "LOCAL";
$this->_db->exec(<<<SQL
$sql = <<<SQL
LOAD DATA $local INFILE '$fullFn' INTO TABLE `$fn` CHARACTER SET UTF8 LINES TERMINATED BY '\r\n';
SQL
);
SQL;
if ($directClient) {
exec("echo " . escapeshellarg($sql) . " | " . $this->_mysqlCli);
} else {
$this->_db->exec($sql);
}
$task->end();

// maybe delete some files after import
Expand Down

0 comments on commit e1053e2

Please sign in to comment.