1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Cli\Executable; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Target\Compression; |
5
|
|
|
use phpbu\App\Cli\Executable; |
6
|
|
|
use phpbu\App\Exception; |
7
|
|
|
use phpbu\App\Util\Cli; |
8
|
|
|
use SebastianFeldmann\Cli\CommandLine; |
9
|
|
|
use SebastianFeldmann\Cli\Command\Executable as Cmd; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Mysqldump Executable class. |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
19
|
|
|
* @link http://phpbu.de/ |
20
|
|
|
* @since Class available since Release 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Mysqldump extends Abstraction implements Executable |
23
|
|
|
{ |
24
|
|
|
use OptionMasker; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Host to connect to |
28
|
|
|
* --host <hostname> |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $host; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* User to connect with |
36
|
|
|
* --user <username> |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $user; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Password to authenticate with |
44
|
|
|
* --password <password> |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $password; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* List of tables to backup |
52
|
|
|
* --tables array of strings |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $tablesToDump = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* List of databases to backup |
60
|
|
|
* --databases array of strings |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $databasesToDump = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* List of tables to ignore |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private $tablesToIgnore = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* List of tables where only the table structure is stored |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
private $structureOnly = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Use mysqldump quick mode |
82
|
|
|
* -q |
83
|
|
|
* |
84
|
|
|
* @var bool |
85
|
|
|
*/ |
86
|
|
|
private $quick = false; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Lock tables option |
90
|
|
|
* --lock-tables |
91
|
|
|
* |
92
|
|
|
* @var bool |
93
|
|
|
*/ |
94
|
|
|
private $lockTables; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Issue a BEGIN SQL statement before dumping data from server |
98
|
|
|
* --single-transaction |
99
|
|
|
* |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
private $singleTransaction; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Use mysqldump with compression |
106
|
|
|
* -C |
107
|
|
|
* |
108
|
|
|
* @var bool |
109
|
|
|
*/ |
110
|
|
|
private $compress = false; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Dump only table structures |
114
|
|
|
* --no-data |
115
|
|
|
* |
116
|
|
|
* @var boolean |
117
|
|
|
*/ |
118
|
|
|
private $noData = false; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Whether to add SET @@GLOBAL.GTID_PURGED to output |
122
|
|
|
* |
123
|
|
|
* @var string |
124
|
|
|
*/ |
125
|
|
|
private $gtidPurged; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Table separated data files |
129
|
|
|
* --tab |
130
|
|
|
* |
131
|
|
|
* @var bool |
132
|
|
|
*/ |
133
|
|
|
private $filePerTable; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Use mysqldump extended insert mode |
137
|
|
|
* -e, --extended-insert |
138
|
|
|
* |
139
|
|
|
* @var boolean |
140
|
|
|
*/ |
141
|
|
|
private $extendedInsert = false; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Dump blob fields as hex. |
145
|
|
|
* --hex-blob |
146
|
|
|
* |
147
|
|
|
* @var boolean |
148
|
|
|
*/ |
149
|
|
|
private $hexBlob = false; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Path to dump file |
153
|
|
|
* |
154
|
|
|
* @var string |
155
|
|
|
*/ |
156
|
|
|
private $dumpPathname; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Compression command to pipe output to |
160
|
|
|
* |
161
|
|
|
* @var \phpbu\App\Backup\Target\Compression |
162
|
|
|
*/ |
163
|
|
|
private $compression; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Constructor. |
167
|
|
|
* |
168
|
|
|
* @param string $path |
169
|
|
|
*/ |
170
|
30 |
|
public function __construct(string $path = '') |
171
|
|
|
{ |
172
|
30 |
|
$this->setup('mysqldump', $path); |
173
|
30 |
|
$this->setMaskCandidates(['password']); |
174
|
30 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set the mysql credentials. |
178
|
|
|
* |
179
|
|
|
* @param string $user |
180
|
|
|
* @param string $password |
181
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
182
|
|
|
*/ |
183
|
14 |
|
public function credentials(string $user = '', string $password = '') : Mysqldump |
184
|
|
|
{ |
185
|
14 |
|
$this->user = $user; |
186
|
14 |
|
$this->password = $password; |
187
|
14 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set the mysql hostname. |
192
|
|
|
* |
193
|
|
|
* @param string $host |
194
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
195
|
|
|
*/ |
196
|
12 |
|
public function useHost(string $host) : Mysqldump |
197
|
|
|
{ |
198
|
12 |
|
$this->host = $host; |
199
|
12 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Use '-q' quick mode. |
204
|
|
|
* |
205
|
|
|
* @param boolean $bool |
206
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
207
|
|
|
*/ |
208
|
12 |
|
public function useQuickMode(bool $bool) : Mysqldump |
209
|
|
|
{ |
210
|
12 |
|
$this->quick = $bool; |
211
|
12 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Use '--lock-tables' option. |
216
|
|
|
* |
217
|
|
|
* @param bool $bool |
218
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
219
|
|
|
*/ |
220
|
13 |
|
public function lockTables(bool $bool) : Mysqldump |
221
|
|
|
{ |
222
|
13 |
|
$this->lockTables = $bool; |
223
|
13 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Use '--single-transaction' option. |
228
|
|
|
* |
229
|
|
|
* @param bool $bool |
230
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
231
|
|
|
*/ |
232
|
13 |
|
public function singleTransaction(bool $bool) : Mysqldump |
233
|
|
|
{ |
234
|
13 |
|
$this->singleTransaction = $bool; |
235
|
13 |
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Use '-C' compress mode. |
240
|
|
|
* |
241
|
|
|
* @param bool $bool |
242
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
243
|
|
|
*/ |
244
|
12 |
|
public function useCompression(bool $bool) : Mysqldump |
245
|
|
|
{ |
246
|
12 |
|
$this->compress = $bool; |
247
|
12 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Use '-e' extended insert mode. |
252
|
|
|
* |
253
|
|
|
* @param bool $bool |
254
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
255
|
|
|
*/ |
256
|
13 |
|
public function useExtendedInsert(bool $bool) : Mysqldump |
257
|
|
|
{ |
258
|
13 |
|
$this->extendedInsert = $bool; |
259
|
13 |
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Use '--hex-blob' to encode binary fields. |
264
|
|
|
* |
265
|
|
|
* @param bool $bool |
266
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
267
|
|
|
*/ |
268
|
13 |
|
public function dumpBlobsHexadecimal(bool $bool) : Mysqldump |
269
|
|
|
{ |
270
|
13 |
|
$this->hexBlob = $bool; |
271
|
13 |
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Set tables to dump. |
276
|
|
|
* |
277
|
|
|
* @param array $tables |
278
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
279
|
|
|
*/ |
280
|
14 |
|
public function dumpTables(array $tables) : Mysqldump |
281
|
|
|
{ |
282
|
14 |
|
$this->tablesToDump = $tables; |
283
|
14 |
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Set databases to dump. |
288
|
|
|
* |
289
|
|
|
* @param array $databases |
290
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
291
|
|
|
*/ |
292
|
15 |
|
public function dumpDatabases(array $databases) : Mysqldump |
293
|
|
|
{ |
294
|
15 |
|
$this->databasesToDump = $databases; |
295
|
15 |
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set tables to ignore. |
300
|
|
|
* |
301
|
|
|
* @param array $tables |
302
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
303
|
|
|
*/ |
304
|
13 |
|
public function ignoreTables(array $tables) : Mysqldump |
305
|
|
|
{ |
306
|
13 |
|
$this->tablesToIgnore = $tables; |
307
|
13 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set tables where only table structure should be dumped. |
312
|
|
|
* |
313
|
|
|
* @param array $tables |
314
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
315
|
|
|
*/ |
316
|
13 |
|
public function dumpStructureOnly(array $tables) : Mysqldump |
317
|
|
|
{ |
318
|
13 |
|
$this->structureOnly = $tables; |
319
|
13 |
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Dump no table data at all. |
324
|
|
|
* |
325
|
|
|
* @param bool $bool |
326
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
327
|
|
|
*/ |
328
|
13 |
|
public function dumpNoData(bool $bool) : Mysqldump |
329
|
|
|
{ |
330
|
13 |
|
$this->noData = $bool; |
331
|
13 |
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Add a general transaction ID statement to the dump file. |
336
|
|
|
* |
337
|
|
|
* @param string $purge |
338
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
339
|
|
|
*/ |
340
|
14 |
|
public function addGTIDStatement(string $purge) |
341
|
|
|
{ |
342
|
14 |
|
$this->gtidPurged = in_array($purge, ['ON', 'OFF', 'AUTO']) ? strtoupper($purge) : ''; |
343
|
14 |
|
return $this; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Produce table separated data files. |
348
|
|
|
* |
349
|
|
|
* @param bool $bool |
350
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
351
|
|
|
*/ |
352
|
12 |
|
public function produceFilePerTable(bool $bool) : Mysqldump |
353
|
|
|
{ |
354
|
12 |
|
$this->filePerTable = $bool; |
355
|
12 |
|
return $this; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Pipe compressor. |
360
|
|
|
* |
361
|
|
|
* @param \phpbu\App\Backup\Target\Compression $compression |
362
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
363
|
|
|
*/ |
364
|
3 |
|
public function compressOutput(Compression $compression) : Mysqldump |
365
|
|
|
{ |
366
|
3 |
|
$this->compression = $compression; |
367
|
3 |
|
return $this; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Set the dump target path. |
372
|
|
|
* |
373
|
|
|
* @param string $path |
374
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
375
|
|
|
*/ |
376
|
13 |
|
public function dumpTo(string $path) : Mysqldump |
377
|
|
|
{ |
378
|
13 |
|
$this->dumpPathname = $path; |
379
|
13 |
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Mysqldump CommandLine generator. |
384
|
|
|
* |
385
|
|
|
* @return \SebastianFeldmann\Cli\CommandLine |
386
|
|
|
*/ |
387
|
30 |
|
protected function createCommandLine() : CommandLine |
388
|
|
|
{ |
389
|
30 |
|
$process = new CommandLine(); |
390
|
30 |
|
$cmd = new Cmd($this->binary); |
391
|
30 |
|
$process->addCommand($cmd); |
392
|
|
|
|
393
|
30 |
|
$cmd->addOptionIfNotEmpty('--user', $this->user); |
394
|
30 |
|
$cmd->addOptionIfNotEmpty('--password', $this->password); |
395
|
30 |
|
$cmd->addOptionIfNotEmpty('--host', $this->host); |
396
|
30 |
|
$cmd->addOptionIfNotEmpty('--lock-tables', $this->lockTables, false); |
397
|
30 |
|
$cmd->addOptionIfNotEmpty('--single-transaction', $this->singleTransaction, false); |
398
|
30 |
|
$cmd->addOptionIfNotEmpty('-q', $this->quick, false); |
399
|
30 |
|
$cmd->addOptionIfNotEmpty('-C', $this->compress, false); |
400
|
30 |
|
$cmd->addOptionIfNotEmpty('-e', $this->extendedInsert, false); |
401
|
30 |
|
$cmd->addOptionIfNotEmpty('--hex-blob', $this->hexBlob, false); |
402
|
30 |
|
$cmd->addOptionIfNotEmpty('--set-gtid-purged', $this->gtidPurged); |
403
|
|
|
|
404
|
30 |
|
$this->configureSourceData($cmd); |
405
|
29 |
|
$this->configureIgnoredTables($cmd); |
406
|
|
|
|
407
|
29 |
|
if ($this->filePerTable) { |
408
|
2 |
|
$cmd->addOption('--tab', $this->dumpPathname); |
409
|
|
|
} |
410
|
|
|
|
411
|
29 |
|
if ($this->noData) { |
412
|
1 |
|
$cmd->addOption('--no-data'); |
413
|
|
|
} else { |
414
|
28 |
|
if (count($this->structureOnly)) { |
415
|
1 |
|
$cmd2 = clone($cmd); |
416
|
1 |
|
foreach ($this->structureOnly as $table) { |
417
|
1 |
|
$cmd2->addOption('--ignore-table', $table); |
418
|
|
|
} |
419
|
1 |
|
$cmd2->addOption('--skip-add-drop-table'); |
420
|
1 |
|
$cmd2->addOption('--no-create-db'); |
421
|
1 |
|
$cmd2->addOption('--no-create-info'); |
422
|
1 |
|
$cmd->addOption('--no-data'); |
423
|
1 |
|
$process->addCommand($cmd2); |
424
|
|
|
} |
425
|
|
|
} |
426
|
29 |
|
$this->configureCompression($process); |
427
|
29 |
|
$this->configureOutput($process); |
428
|
29 |
|
return $process; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Configure source data (tables, databases). |
433
|
|
|
* |
434
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
435
|
|
|
* @throws \phpbu\App\Exception |
436
|
|
|
*/ |
437
|
30 |
|
private function configureSourceData(Cmd $cmd) |
438
|
|
|
{ |
439
|
30 |
|
if (count($this->tablesToDump)) { |
440
|
2 |
|
$this->configureSourceTables($cmd); |
441
|
|
|
} else { |
442
|
28 |
|
$this->configureSourceDatabases($cmd); |
443
|
|
|
} |
444
|
29 |
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Configure source tables. |
448
|
|
|
* |
449
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
450
|
|
|
* @throws \phpbu\App\Exception |
451
|
|
|
*/ |
452
|
2 |
|
private function configureSourceTables(Cmd $cmd) |
453
|
|
|
{ |
454
|
2 |
|
if (count($this->databasesToDump) !== 1) { |
455
|
1 |
|
throw new Exception('mysqldump --tables needs exactly one database'); |
456
|
|
|
} |
457
|
1 |
|
$cmd->addArgument($this->databasesToDump[0]); |
458
|
1 |
|
$cmd->addOption('--tables', $this->tablesToDump); |
459
|
1 |
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Configure source databases. |
463
|
|
|
* |
464
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
465
|
|
|
*/ |
466
|
28 |
|
private function configureSourceDatabases(Cmd $cmd) |
467
|
|
|
{ |
468
|
28 |
|
$databasesToDump = count($this->databasesToDump); |
469
|
|
|
|
470
|
|
|
// different handling for different amounts of databases |
471
|
28 |
|
if ($databasesToDump == 1) { |
472
|
|
|
// single database use argument |
473
|
1 |
|
$cmd->addArgument($this->databasesToDump[0]); |
474
|
27 |
|
} elseif ($databasesToDump > 1) { |
475
|
|
|
// multiple databases add list with --databases |
476
|
1 |
|
$cmd->addOption('--databases', $this->databasesToDump); |
477
|
|
|
} else { |
478
|
|
|
// no databases set dump all databases |
479
|
26 |
|
$cmd->addOption('--all-databases'); |
480
|
|
|
} |
481
|
28 |
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Add --ignore-table options |
485
|
|
|
* |
486
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
487
|
|
|
*/ |
488
|
29 |
|
private function configureIgnoredTables(Cmd $cmd) |
489
|
|
|
{ |
490
|
29 |
|
if (count($this->tablesToIgnore)) { |
491
|
1 |
|
foreach ($this->tablesToIgnore as $table) { |
492
|
1 |
|
$cmd->addOption('--ignore-table', $table); |
493
|
|
|
} |
494
|
|
|
} |
495
|
29 |
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Add compressor pipe if set. |
499
|
|
|
* |
500
|
|
|
* @param \SebastianFeldmann\Cli\CommandLine $process |
501
|
|
|
*/ |
502
|
29 |
|
private function configureCompression(CommandLine $process) |
503
|
|
|
{ |
504
|
|
|
// if file per table isn't active and a compressor is set |
505
|
29 |
|
if (!$this->filePerTable && !empty($this->compression)) { |
506
|
3 |
|
$binary = Cli::detectCmdLocation($this->compression->getCommand(), $this->compression->getPath()); |
507
|
3 |
|
$cmd = new Cmd($binary); |
508
|
3 |
|
$process->pipeOutputTo($cmd); |
509
|
|
|
} |
510
|
29 |
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Configure output redirect. |
514
|
|
|
* |
515
|
|
|
* @param \SebastianFeldmann\Cli\CommandLine $process |
516
|
|
|
*/ |
517
|
29 |
|
private function configureOutput(CommandLine $process) |
518
|
|
|
{ |
519
|
|
|
// disable output redirection if files per table is active |
520
|
29 |
|
if (!$this->filePerTable) { |
521
|
27 |
|
$process->redirectOutputTo( |
522
|
27 |
|
$this->dumpPathname . (!empty($this->compression) ? '.' . $this->compression->getSuffix() : '') |
523
|
|
|
); |
524
|
|
|
} |
525
|
29 |
|
} |
526
|
|
|
} |
527
|
|
|
|