Passed
Pull Request — master (#230)
by
unknown
08:43
created

Mysqldump::useSslCa()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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