Completed
Pull Request — master (#218)
by
unknown
02:57
created

Mysqldump::skipTriggers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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