Completed
Push — master ( 80c8cd...281eb8 )
by Sebastian
05:00
created

Mysqldump::singleTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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