1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Source; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Target; |
5
|
|
|
use phpbu\App\Cli\Executable; |
6
|
|
|
use phpbu\App\Exception; |
7
|
|
|
use phpbu\App\Result; |
8
|
|
|
use phpbu\App\Util; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Mysqldump source class. |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
* @since Class available since Release 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class Mysqldump extends SimulatorExecutable implements Simulator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Path to executable. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $pathToMysqldump; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Host to connect to |
32
|
|
|
* --host <hostname> |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $host; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* User to connect with |
40
|
|
|
* --user <username> |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $user; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Password to authenticate with |
48
|
|
|
* --password <password> |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $password; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* List of tables to backup |
56
|
|
|
* --tables array of strings |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $tables; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* List of databases to backup |
64
|
|
|
* --databases array of strings |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $databases; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* List of tables to ignore |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
private $ignoreTables; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* List of tables where only the table structure is stored |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
private $structureOnly; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Table separated data files |
86
|
|
|
* --tab |
87
|
|
|
* |
88
|
|
|
* @var boolean |
89
|
|
|
*/ |
90
|
|
|
private $filePerTable; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The compressor cmd |
94
|
|
|
* |
95
|
|
|
* @var boolean |
96
|
|
|
*/ |
97
|
|
|
private $compressor; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Use mysqldump quick mode |
101
|
|
|
* -q |
102
|
|
|
* |
103
|
|
|
* @var boolean |
104
|
|
|
*/ |
105
|
|
|
private $quick; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* Lock tables option |
110
|
|
|
* --lock-tables |
111
|
|
|
* |
112
|
|
|
* @var bool |
113
|
|
|
*/ |
114
|
|
|
private $lockTables; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Use mysqldump with compression |
118
|
|
|
* -C |
119
|
|
|
* |
120
|
|
|
* @var boolean |
121
|
|
|
*/ |
122
|
|
|
private $compress; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Use mysqldump with extended insert |
126
|
|
|
* -e |
127
|
|
|
* |
128
|
|
|
* @var boolean |
129
|
|
|
*/ |
130
|
|
|
private $extendedInsert; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Dump blob fields as hex. |
134
|
|
|
* --hex-blob |
135
|
|
|
* |
136
|
|
|
* @var boolean |
137
|
|
|
*/ |
138
|
|
|
private $hexBlob; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Dump only table structures |
142
|
|
|
* --no-data |
143
|
|
|
* |
144
|
|
|
* @var boolean |
145
|
|
|
*/ |
146
|
5 |
|
private $noData; |
147
|
|
|
|
148
|
5 |
|
/** |
149
|
|
|
* Setup. |
150
|
5 |
|
* |
151
|
5 |
|
* @see \phpbu\App\Backup\Source |
152
|
5 |
|
* @param array $conf |
153
|
5 |
|
* @throws \phpbu\App\Exception |
154
|
5 |
|
*/ |
155
|
5 |
|
public function setup(array $conf = []) |
156
|
5 |
|
{ |
157
|
5 |
|
$this->setupSourceData($conf); |
158
|
5 |
|
|
159
|
5 |
|
$this->pathToMysqldump = Util\Arr::getValue($conf, 'pathToMysqldump'); |
160
|
5 |
|
$this->host = Util\Arr::getValue($conf, 'host'); |
161
|
|
|
$this->user = Util\Arr::getValue($conf, 'user'); |
162
|
|
|
$this->password = Util\Arr::getValue($conf, 'password'); |
163
|
|
|
$this->hexBlob = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false); |
164
|
|
|
$this->quick = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false); |
165
|
|
|
$this->lockTables = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false); |
166
|
|
|
$this->compress = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false); |
167
|
5 |
|
$this->extendedInsert = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false); |
168
|
|
|
$this->noData = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false); |
169
|
5 |
|
$this->filePerTable = Util\Str::toBoolean(Util\Arr::getValue($conf, 'filePerTable', ''), false); |
170
|
5 |
|
$this->compressor = Util\Arr::getValue($conf, 'compressor', ''); |
171
|
5 |
|
|
172
|
5 |
|
// this doesn't fail, but it doesn't work, so throw an exception so the user understands |
173
|
5 |
|
if ($this->filePerTable && count($this->structureOnly)) { |
174
|
|
|
throw new Exception('\'structureOnly\' can not be used with the \'filePerTable\' option'); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get tables and databases to backup. |
180
|
|
|
* |
181
|
|
|
* @param array $conf |
182
|
|
|
*/ |
183
|
|
View Code Duplication |
protected function setupSourceData(array $conf) |
184
|
2 |
|
{ |
185
|
|
|
$this->tables = Util\Str::toList(Util\Arr::getValue($conf, 'tables')); |
186
|
|
|
$this->databases = Util\Str::toList(Util\Arr::getValue($conf, 'databases')); |
187
|
2 |
|
$this->ignoreTables = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables')); |
188
|
2 |
|
$this->structureOnly = Util\Str::toList(Util\Arr::getValue($conf, 'structureOnly')); |
189
|
|
|
} |
190
|
2 |
|
|
191
|
|
|
/** |
192
|
2 |
|
* Execute the backup. |
193
|
1 |
|
* |
194
|
|
|
* @see \phpbu\App\Backup\Source |
195
|
|
|
* @param \phpbu\App\Backup\Target $target |
196
|
1 |
|
* @param \phpbu\App\Result $result |
197
|
|
|
* @return \phpbu\App\Backup\Source\Status |
198
|
|
|
* @throws \phpbu\App\Exception |
199
|
|
|
*/ |
200
|
|
|
public function backup(Target $target, Result $result) |
201
|
|
|
{ |
202
|
|
|
// create the writable dump directory for tables files |
203
|
|
|
if ($this->filePerTable && !is_dir($this->getDumpTarget($target))) { |
204
|
|
|
$old = umask(0); |
205
|
5 |
|
mkdir($this->getDumpTarget($target), 0777, true); |
206
|
|
|
umask($old); |
207
|
5 |
|
} |
208
|
3 |
|
|
209
|
3 |
|
$mysqldump = $this->execute($target); |
210
|
3 |
|
|
211
|
3 |
|
$result->debug($this->getExecutable($target)->getCommandLinePrintable()); |
212
|
3 |
|
|
213
|
3 |
|
if (!$mysqldump->wasSuccessful()) { |
214
|
3 |
|
throw new Exception('mysqldump failed:' . $mysqldump->getStdErr()); |
215
|
3 |
|
} |
216
|
3 |
|
|
217
|
3 |
|
return $this->createStatus($target); |
218
|
3 |
|
} |
219
|
3 |
|
|
220
|
3 |
|
/** |
221
|
3 |
|
* Create the Executable to run the mysqldump command. |
222
|
3 |
|
* |
223
|
5 |
|
* @param \phpbu\App\Backup\Target $target |
224
|
|
|
* @return \phpbu\App\Cli\Executable |
225
|
|
|
*/ |
226
|
|
|
public function getExecutable(Target $target) |
227
|
|
|
{ |
228
|
|
|
if (null == $this->executable) { |
229
|
|
|
$this->executable = new Executable\Mysqldump($this->pathToMysqldump); |
230
|
|
|
$this->executable->credentials($this->user, $this->password) |
231
|
|
|
->useHost($this->host) |
232
|
|
|
->useQuickMode($this->quick) |
233
|
|
|
->lockTables($this->lockTables) |
234
|
|
|
->dumpBlobsHexadecimal($this->hexBlob) |
235
|
|
|
->useCompression($this->compress) |
236
|
|
|
->useExtendedInsert($this->extendedInsert) |
237
|
|
|
->dumpTables($this->tables) |
238
|
|
|
->dumpDatabases($this->databases) |
239
|
|
|
->ignoreTables($this->ignoreTables) |
240
|
|
|
->produceFilePerTable($this->filePerTable) |
241
|
|
|
->dumpNoData($this->noData) |
242
|
|
|
->dumpStructureOnly($this->structureOnly) |
243
|
|
|
->dumpTo($this->getDumpTarget($target)); |
244
|
|
|
|
245
|
|
|
if (!empty($this->compressor)) { |
246
|
|
|
$this->executable->getProcess()->setCompression($this->compressor); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
return $this->executable; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Create backup status. |
254
|
|
|
* |
255
|
|
|
* @param \phpbu\App\Backup\Target |
256
|
|
|
* @return \phpbu\App\Backup\Source\Status |
257
|
|
|
*/ |
258
|
|
|
protected function createStatus(Target $target) |
259
|
|
|
{ |
260
|
|
|
return $this->filePerTable |
261
|
|
|
? Status::create()->uncompressedDirectory($this->getDumpTarget($target)) |
262
|
|
|
: Status::create()->uncompressedFile($this->getDumpTarget($target)); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Return dump target path. |
267
|
|
|
* |
268
|
|
|
* @param \phpbu\App\Backup\Target $target |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
|
|
private function getDumpTarget(Target $target) |
272
|
|
|
{ |
273
|
|
|
return $target->getPathnamePlain() . ($this->filePerTable ? '.dump' : ''); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|