Completed
Push — master ( 57c54d...9135e7 )
by Sebastian
03:47
created

Mysqldump::configureCompression()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
crap 12
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Backup\Target\Compression;
5
use phpbu\App\Cli\Cmd;
6
use phpbu\App\Cli\Executable;
7
use phpbu\App\Cli\Process;
8
use phpbu\App\Exception;
9
use phpbu\App\Util\Cli;
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
     *
90
     * Lock tables option
91
     * --lock-tables
92
     *
93
     * @var bool
94
     */
95
    private $lockTables;
96
97
    /**
98
     * Use mysqldump with compression
99
     * -C
100
     *
101
     * @var bool
102
     */
103
    private $compress = false;
104
105
    /**
106
     * Dump only table structures
107
     * --no-data
108
     *
109
     * @var boolean
110
     */
111
    private $noData = false;
112
113
    /**
114
     * Table separated data files
115
     * --tab
116
     *
117
     * @var bool
118
     */
119
    private $filePerTable;
120
121
    /**
122
     * Use mysqldump extended insert mode
123
     * -e, --extended-insert
124
     *
125
     * @var boolean
126
     */
127 14
    private $extendedInsert = false;
128
129 14
    /**
130 14
     * Dump blob fields as hex.
131 14
     * --hex-blob
132
     *
133
     * @var boolean
134
     */
135
    private $hexBlob = false;
136
137
    /**
138
     * Path to dump file
139
     *
140 3
     * @var string
141
     */
142 3
    private $dumpPathname;
143 3
144 3
    /**
145
     * Compression command to pipe output to
146
     *
147
     * @var \phpbu\App\Backup\Target\Compression
148
     */
149
    private $compression;
150
151
    /**
152
     * Constructor.
153 3
     *
154
     * @param string $path
155 3
     */
156 3
    public function __construct($path = null)
157
    {
158
        $this->setup('mysqldump', $path);
159
        $this->setMaskCandidates(['password']);
160
    }
161
162
    /**
163
     * Set the mysql credentials.
164
     *
165 3
     * @param  string $user
166
     * @param  string $password
167 3
     * @return \phpbu\App\Cli\Executable\Mysqldump
168 3
     */
169
    public function credentials($user = null, $password = null)
170
    {
171
        $this->user     = $user;
172
        $this->password = $password;
173
        return $this;
174
    }
175
176
    /**
177 3
     * Set the mysql hostname.
178
     *
179 3
     * @param  string $host
180 3
     * @return \phpbu\App\Cli\Executable\Mysqldump
181
     */
182
    public function useHost($host)
183
    {
184
        $this->host = $host;
185
        return $this;
186
    }
187
188
    /**
189 4
     * Use '-q' quick mode.
190
     *
191 4
     * @param  boolean $bool
192 4
     * @return \phpbu\App\Cli\Executable\Mysqldump
193
     */
194
    public function useQuickMode($bool)
195
    {
196
        $this->quick = $bool;
197
        return $this;
198
    }
199
200
    /**
201 4
     * Use '--lock-tables' option.
202
     *
203 4
     * @param  boolean $bool
204 4
     * @return \phpbu\App\Cli\Executable\Mysqldump
205
     */
206
    public function lockTables($bool)
207
    {
208
        $this->lockTables = $bool;
209
        return $this;
210
    }
211
212
    /**
213 4
     * Use '-C' compress mode.
214
     *
215 4
     * @param  boolean $bool
216 4
     * @return \phpbu\App\Cli\Executable\Mysqldump
217
     */
218
    public function useCompression($bool)
219
    {
220
        $this->compress = $bool;
221
        return $this;
222
    }
223
224
    /**
225 4
     * Use '-e' extended insert mode.
226
     *
227 4
     * @param  boolean $bool
228 4
     * @return \phpbu\App\Cli\Executable\Mysqldump
229
     */
230
    public function useExtendedInsert($bool)
231
    {
232
        $this->extendedInsert = $bool;
233
        return $this;
234
    }
235
236
    /**
237 4
     * Use '--hex-blob' to encode binary fields.
238
     *
239 4
     * @param  boolean $bool
240 4
     * @return \phpbu\App\Cli\Executable\Mysqldump
241
     */
242
    public function dumpBlobsHexadecimal($bool)
243
    {
244
        $this->hexBlob = $bool;
245
        return $this;
246
    }
247
248
    /**
249 4
     * Set tables to dump.
250
     *
251 4
     * @param  array $tables
252 4
     * @return \phpbu\App\Cli\Executable\Mysqldump
253
     */
254
    public function dumpTables(array $tables)
255
    {
256
        $this->tablesToDump = $tables;
257
        return $this;
258
    }
259
260
    /**
261 4
     * Set databases to dump.
262
     *
263 4
     * @param  array $databases
264 4
     * @return \phpbu\App\Cli\Executable\Mysqldump
265
     */
266
    public function dumpDatabases(array $databases)
267
    {
268
        $this->databasesToDump = $databases;
269
        return $this;
270
    }
271
272
    /**
273 3
     * Set tables to ignore.
274
     *
275 3
     * @param  array $tables
276 3
     * @return \phpbu\App\Cli\Executable\Mysqldump
277
     */
278
    public function ignoreTables(array $tables)
279
    {
280
        $this->tablesToIgnore = $tables;
281
        return $this;
282 11
    }
283
284 11
    /**
285 11
     * Set tables where only table structure should be dumped.
286 11
     *
287
     * @param  array $tables
288
     * @return \phpbu\App\Cli\Executable\Mysqldump
289 11
     */
290 11
    public function dumpStructureOnly(array $tables)
291
    {
292 11
        $this->structureOnly = $tables;
293 11
        return $this;
294 11
    }
295 11
296 11
    /**
297 11
     * Dump no table data at all.
298 11
     *
299 11
     * @param  boolean $bool
300
     * @return \phpbu\App\Cli\Executable\Mysqldump
301 11
     */
302 1
    public function dumpNoData($bool)
303 1
    {
304 10
        $this->noData = $bool;
305 1
        return $this;
306 1
    }
307 9
308
    /**
309
     * Produce table separated data files.
310
     *
311 11
     * @param  bool $bool
312 1
     * @return \phpbu\App\Cli\Executable\Mysqldump
313 1
     */
314 1
    public function produceFilePerTable($bool)
315 1
    {
316 11
        $this->filePerTable = $bool;
317 1
        return $this;
318 1
    }
319 10
320 1
    /**
321 1
     * Pipe compressor.
322 1
     *
323 1
     * @param  \phpbu\App\Backup\Target\Compression $compression
324 1
     * @return \phpbu\App\Cli\Executable\Mysqldump
325 1
     */
326 1
    public function compressOutput(Compression $compression)
327 1
    {
328 1
        $this->compression = $compression;
329 1
        return $this;
330
    }
331 11
332 11
    /**
333
     * Set the dump target path.
334
     *
335
     * @param  string $path
336
     * @return \phpbu\App\Cli\Executable\Mysqldump
337
     */
338
    public function dumpTo($path)
339
    {
340
        $this->dumpPathname = $path;
341
        return $this;
342
    }
343
344
    /**
345
     * Process generator
346
     */
347
    protected function createProcess()
348
    {
349
        $process = new Process();
350
        $cmd     = new Cmd($this->binary);
351
        $process->addCommand($cmd);
352
353
        $cmd->addOptionIfNotEmpty('--user', $this->user);
354
        $cmd->addOptionIfNotEmpty('--password', $this->password);
355
        $cmd->addOptionIfNotEmpty('--host', $this->host);
356
        $cmd->addOptionIfNotEmpty('--lock-tables', $this->lockTables, false);
357
        $cmd->addOptionIfNotEmpty('-q', $this->quick, false);
358
        $cmd->addOptionIfNotEmpty('-C', $this->compress, false);
359
        $cmd->addOptionIfNotEmpty('-e', $this->extendedInsert, false);
360
        $cmd->addOptionIfNotEmpty('--hex-blob', $this->hexBlob, false);
361
362
        $this->configureSourceData($cmd);
363
        $this->configureIgnoredTables($cmd);
364
365
        if ($this->filePerTable) {
366
            $cmd->addOption('--tab', $this->dumpPathname);
367
        }
368
369
        if ($this->noData) {
370
            $cmd->addOption('--no-data');
371
        } else {
372
            if (count($this->structureOnly)) {
373
                $cmd2 = clone($cmd);
374
                foreach ($this->structureOnly as $table) {
375
                    $cmd2->addOption('--ignore-table', $table);
376
                }
377
                $cmd2->addOption('--skip-add-drop-table');
378
                $cmd2->addOption('--no-create-db');
379
                $cmd2->addOption('--no-create-info');
380
                $cmd->addOption('--no-data');
381
                $process->addCommand($cmd2);
382
            }
383
        }
384
        $this->configureCompression($process);
385
        $this->configureOutput($process);
386
        return $process;
387
    }
388
389
    /**
390
     * Configure source data (tables, databases).
391
     *
392
     * @param  \phpbu\App\Cli\Cmd $cmd
393
     * @throws \phpbu\App\Exception
394
     */
395
    private function configureSourceData(Cmd $cmd)
396
    {
397
        if (count($this->tablesToDump)) {
398
            $this->configureSourceTables($cmd);
399
        } else {
400
            $this->configureSourceDatabases($cmd);
401
        }
402
    }
403
404
    /**
405
     * Configure source tables.
406
     *
407
     * @param  \phpbu\App\Cli\Cmd $cmd
408
     * @throws \phpbu\App\Exception
409
     */
410
    private function configureSourceTables(Cmd $cmd)
411
    {
412
        if (count($this->databasesToDump) !== 1) {
413
            throw new Exception('mysqldump --tables needs exactly one database');
414
        }
415
        $cmd->addArgument($this->databasesToDump[0]);
416
        $cmd->addOption('--tables', $this->tablesToDump);
417
    }
418
419
    /**
420
     * Configure source databases.
421
     *
422
     * @param  \phpbu\App\Cli\Cmd $cmd
423
     */
424
    private function configureSourceDatabases(Cmd $cmd)
425
    {
426
        $databasesToDump = count($this->databasesToDump);
427
428
        // different handling for different ammounts of databases
429
        if ($databasesToDump == 1) {
430
            // single database use argument
431
            $cmd->addArgument($this->databasesToDump[0]);
432
        } elseif ($databasesToDump > 1) {
433
            // multiple databases add list with --databases
434
            $cmd->addOption('--databases', $this->databasesToDump);
435
        } else {
436
            // no databases set dump all databases
437
            $cmd->addOption('--all-databases');
438
        }
439
    }
440
441
    /**
442
     * Add --ignore-table options
443
     *
444
     * @param \phpbu\App\Cli\Cmd $cmd
445
     */
446
    private function configureIgnoredTables(Cmd $cmd)
447
    {
448
        if (count($this->tablesToIgnore)) {
449
            foreach ($this->tablesToIgnore as $table) {
450
                $cmd->addOption('--ignore-table', $table);
451
            }
452
        }
453
    }
454
455
    /**
456
     * Add compressor pipe if set.
457
     *
458
     * @param \phpbu\App\Cli\Process $process
459
     */
460
    private function configureCompression(Process $process)
461
    {
462
        // if file per table isn't active and a compressor is set
463
        if (!$this->filePerTable && !empty($this->compression)) {
464
            $binary = Cli::detectCmdLocation($this->compression->getCommand(), $this->compression->getPath());
465
            $cmd    = new Cmd($binary);
466
            $process->pipeOutputTo($cmd);
467
        }
468
    }
469
470
    /**
471
     * Configure output redirect.
472
     *
473
     * @param \phpbu\App\Cli\Process $process
474
     */
475
    private function configureOutput(Process $process)
476
    {
477
        // disable output redirection if files per table is active
478
        if (!$this->filePerTable) {
479
            $process->redirectOutputTo(
480
                $this->dumpPathname . (!empty($this->compression) ? '.' . $this->compression->getSuffix() : '')
481
            );
482
        }
483
    }
484
}
485