| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_IncrementalBackup_Database_MysqlDumpDumper implements MWP_IncrementalBackup_Database_DumperInterface |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var MWP_IncrementalBackup_Database_Configuration |
| 16 |
*/ |
| 17 |
private $configuration; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var MWP_IncrementalBackup_Database_DumpOptions |
| 21 |
*/ |
| 22 |
private $options; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param MWP_IncrementalBackup_Database_Configuration $configuration |
| 26 |
* @param MWP_IncrementalBackup_Database_DumpOptions $options |
| 27 |
*/ |
| 28 |
public function __construct(MWP_IncrementalBackup_Database_Configuration $configuration, MWP_IncrementalBackup_Database_DumpOptions $options) |
| 29 |
{ |
| 30 |
$this->configuration = $configuration; |
| 31 |
$this->options = $options; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* {@inheritdoc} |
| 36 |
*/ |
| 37 |
public function dump($table, $realpath) |
| 38 |
{ |
| 39 |
if (!mwp_is_shell_available()) { |
| 40 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::SHELL_NOT_AVAILABLE, 'Shell not available.'); |
| 41 |
} |
| 42 |
|
| 43 |
$mysqldump = mwp_container()->getExecutableFinder()->find('mysqldump', 'mysqldump'); |
| 44 |
$processBuilder = $this->createProcessBuilder(array($table), $mysqldump); |
| 45 |
$processBuilder->add('--result-file='.$realpath); |
| 46 |
|
| 47 |
$process = $processBuilder->getProcess(); |
| 48 |
|
| 49 |
mwp_logger()->info('Database dumping process starting', array( |
| 50 |
'executable_location' => $mysqldump, |
| 51 |
'command_line' => $process->getCommandLine(), |
| 52 |
'table' => $table, |
| 53 |
'destination' => $realpath, |
| 54 |
)); |
| 55 |
|
| 56 |
$process->mustRun(); |
| 57 |
|
| 58 |
mwp_logger()->info('Database dump complete', array( |
| 59 |
'table' => $table, |
| 60 |
'destination' => $realpath, |
| 61 |
'size' => @filesize($realpath), |
| 62 |
)); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* {@inheritdoc} |
| 67 |
*/ |
| 68 |
public function createStream(array $tables = array()) |
| 69 |
{ |
| 70 |
if (!mwp_is_shell_available()) { |
| 71 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::SHELL_NOT_AVAILABLE, 'Shell not available.'); |
| 72 |
} |
| 73 |
|
| 74 |
$mysqldump = mwp_container()->getExecutableFinder()->find('mysqldump', 'mysqldump'); |
| 75 |
$processBuilder = $this->createProcessBuilder($tables, $mysqldump); |
| 76 |
|
| 77 |
$process = $processBuilder->getProcess(); |
| 78 |
|
| 79 |
mwp_logger()->info('Database dumping process starting', array( |
| 80 |
'executable_location' => $mysqldump, |
| 81 |
'command_line' => $process->getCommandLine(), |
| 82 |
)); |
| 83 |
|
| 84 |
return new MWP_Stream_ProcessOutput($process); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @param array $tables |
| 89 |
* @param $mysqldump |
| 90 |
* |
| 91 |
* @return Symfony_Process_ProcessBuilder |
| 92 |
*/ |
| 93 |
protected function createProcessBuilder(array $tables, $mysqldump) |
| 94 |
{ |
| 95 |
$processBuilder = Symfony_Process_ProcessBuilder::create() |
| 96 |
->setWorkingDirectory(untrailingslashit(ABSPATH)) |
| 97 |
->setTimeout(3600) |
| 98 |
->setPrefix($mysqldump) |
| 99 |
// Continue even if we get an SQL error. |
| 100 |
->add('--force') |
| 101 |
// User for login if not current user. |
| 102 |
->add('--user='.$this->configuration->getUsername()) |
| 103 |
// Password to use when connecting to server. If password is not given it's solicited on the tty. |
| 104 |
->add('--password='.$this->configuration->getPassword()) |
| 105 |
// Add a DROP TABLE before each create. |
| 106 |
->add('--add-drop-table'); |
| 107 |
|
| 108 |
if ($this->options->isSkipLockTables()) { |
| 109 |
// Don't lock all tables for read. |
| 110 |
$processBuilder->add('--lock-tables=false'); |
| 111 |
} |
| 112 |
|
| 113 |
if ($this->options->isSkipExtendedInsert()) { |
| 114 |
$processBuilder->add('--extended-insert=false'); |
| 115 |
} |
| 116 |
|
| 117 |
$processBuilder->add($this->configuration->getDatabase()); |
| 118 |
|
| 119 |
// Dump only specific tables |
| 120 |
foreach ($tables as $table) { |
| 121 |
$processBuilder->add($table); |
| 122 |
} |
| 123 |
|
| 124 |
if ($this->configuration->isSocket()) { |
| 125 |
$processBuilder->add('--socket='.$this->configuration->getSocketPath()); |
| 126 |
} else { |
| 127 |
$processBuilder->add('--host='.$this->configuration->getHost()); |
| 128 |
if (($port = $this->configuration->getPort()) !== null) { |
| 129 |
$processBuilder->add('--port='.$port); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return $processBuilder; |
| 134 |
} |
| 135 |
} |
| 136 |
|