Completed
Push — master ( 00823b...b48c50 )
by Sebastian
12s
created

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