Completed
Push — master ( 1d6e1a...19a3cd )
by Sebastian
03:35
created

Mysqldump::configureSourceTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Cmd;
5
use phpbu\App\Cli\Executable;
6
use phpbu\App\Cli\Process;
7
use phpbu\App\Exception;
8
9
/**
10
 * Mysqldump Executable class.
11
 *
12
 * @package    phpbu
13
 * @subpackage Backup
14
 * @author     Sebastian Feldmann <[email protected]>
15
 * @copyright  Sebastian Feldmann <[email protected]>
16
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
17
 * @link       http://phpbu.de/
18
 * @since      Class available since Release 1.0.0
19
 */
20
class Mysqldump extends Abstraction implements Executable
21
{
22
    use OptionMasker;
23
24
    /**
25
     * Host to connect to
26
     * --host <hostname>
27
     *
28
     * @var string
29
     */
30
    private $host;
31
32
    /**
33
     * User to connect with
34
     * --user <username>
35
     *
36
     * @var string
37
     */
38
    private $user;
39
40
    /**
41
     * Password to authenticate with
42
     * --password <password>
43
     *
44
     * @var string
45
     */
46
    private $password;
47
48
    /**
49
     * List of tables to backup
50
     * --tables array of strings
51
     *
52
     * @var array
53
     */
54
    private $tablesToDump = [];
55
56
    /**
57
     * List of databases to backup
58
     * --databases array of strings
59
     *
60
     * @var array
61
     */
62
    private $databasesToDump = [];
63
64
    /**
65
     * List of tables to ignore
66
     *
67
     * @var array
68
     */
69
    private $tablesToIgnore = [];
70
71
    /**
72
     * List of tables where only the table structure is stored
73
     *
74
     * @var array
75
     */
76
    private $structureOnly = [];
77
78
    /**
79
     * Use mysqldump quick mode
80
     * -q
81
     *
82
     * @var bool
83
     */
84
    private $quick = false;
85
86
    /**
87
     *
88
     * Lock tables option
89
     * --lock-tables
90
     *
91
     * @var bool
92
     */
93
    private $lockTables;
94
95
    /**
96
     * Use mysqldump with compression
97
     * -C
98
     *
99
     * @var bool
100
     */
101
    private $compress = false;
102
103
    /**
104
     * Dump only table structures
105
     * --no-data
106
     *
107
     * @var boolean
108
     */
109
    private $noData = false;
110
111
    /**
112
     * Table separated data files
113
     * --tab
114
     *
115
     * @var bool
116
     */
117
    private $filePerTable;
118
119
    /**
120
     * Use mysqldump extended insert mode
121
     * -e, --extended-insert
122
     *
123
     * @var boolean
124
     */
125
    private $extendedInsert = false;
126
127 14
    /**
128
     * Dump blob fields as hex.
129 14
     * --hex-blob
130 14
     *
131 14
     * @var boolean
132
     */
133
    private $hexBlob = false;
134
135
    /**
136
     * Path to dump file
137
     *
138
     * @var string
139
     */
140 3
    private $dumpPathname;
141
142 3
    /**
143 3
     * Constructor.
144 3
     *
145
     * @param string $path
146
     */
147
    public function __construct($path = null)
148
    {
149
        $this->setup('mysqldump', $path);
150
        $this->setMaskCandidates(['password']);
151
    }
152
153 3
    /**
154
     * Set the mysql credentials.
155 3
     *
156 3
     * @param  string $user
157
     * @param  string $password
158
     * @return \phpbu\App\Cli\Executable\Mysqldump
159
     */
160
    public function credentials($user = null, $password = null)
161
    {
162
        $this->user     = $user;
163
        $this->password = $password;
164
        return $this;
165 3
    }
166
167 3
    /**
168 3
     * Set the mysql hostname.
169
     *
170
     * @param  string $host
171
     * @return \phpbu\App\Cli\Executable\Mysqldump
172
     */
173
    public function useHost($host)
174
    {
175
        $this->host = $host;
176
        return $this;
177 3
    }
178
179 3
    /**
180 3
     * Use '-q' quick mode.
181
     *
182
     * @param  boolean $bool
183
     * @return \phpbu\App\Cli\Executable\Mysqldump
184
     */
185
    public function useQuickMode($bool)
186
    {
187
        $this->quick = $bool;
188
        return $this;
189 4
    }
190
191 4
    /**
192 4
     * Use '--lock-tables' option.
193
     *
194
     * @param  boolean $bool
195
     * @return \phpbu\App\Cli\Executable\Mysqldump
196
     */
197
    public function lockTables($bool)
198
    {
199
        $this->lockTables = $bool;
200
        return $this;
201 4
    }
202
203 4
    /**
204 4
     * Use '-C' compress mode.
205
     *
206
     * @param  boolean $bool
207
     * @return \phpbu\App\Cli\Executable\Mysqldump
208
     */
209
    public function useCompression($bool)
210
    {
211
        $this->compress = $bool;
212
        return $this;
213 4
    }
214
215 4
    /**
216 4
     * Use '-e' extended insert mode.
217
     *
218
     * @param  boolean $bool
219
     * @return \phpbu\App\Cli\Executable\Mysqldump
220
     */
221
    public function useExtendedInsert($bool)
222
    {
223
        $this->extendedInsert = $bool;
224
        return $this;
225 4
    }
226
227 4
    /**
228 4
     * Use '--hex-blob' to encode binary fields.
229
     *
230
     * @param  boolean $bool
231
     * @return \phpbu\App\Cli\Executable\Mysqldump
232
     */
233
    public function dumpBlobsHexadecimal($bool)
234
    {
235
        $this->hexBlob = $bool;
236
        return $this;
237 4
    }
238
239 4
    /**
240 4
     * Set tables to dump.
241
     *
242
     * @param  array $tables
243
     * @return \phpbu\App\Cli\Executable\Mysqldump
244
     */
245
    public function dumpTables(array $tables)
246
    {
247
        $this->tablesToDump = $tables;
248
        return $this;
249 4
    }
250
251 4
    /**
252 4
     * Set databases to dump.
253
     *
254
     * @param  array $databases
255
     * @return \phpbu\App\Cli\Executable\Mysqldump
256
     */
257
    public function dumpDatabases(array $databases)
258
    {
259
        $this->databasesToDump = $databases;
260
        return $this;
261 4
    }
262
263 4
    /**
264 4
     * Set tables to ignore.
265
     *
266
     * @param  array $tables
267
     * @return \phpbu\App\Cli\Executable\Mysqldump
268
     */
269
    public function ignoreTables(array $tables)
270
    {
271
        $this->tablesToIgnore = $tables;
272
        return $this;
273 3
    }
274
275 3
    /**
276 3
     * Set tables where only table structure should be dumped.
277
     *
278
     * @param  array $tables
279
     * @return \phpbu\App\Cli\Executable\Mysqldump
280
     */
281
    public function dumpStructureOnly(array $tables)
282 11
    {
283
        $this->structureOnly = $tables;
284 11
        return $this;
285 11
    }
286 11
287
    /**
288
     * Dump no table data at all.
289 11
     *
290 11
     * @param  boolean $bool
291
     * @return \phpbu\App\Cli\Executable\Mysqldump
292 11
     */
293 11
    public function dumpNoData($bool)
294 11
    {
295 11
        $this->noData = $bool;
296 11
        return $this;
297 11
    }
298 11
299 11
    /**
300
     * Produce table separated data files.
301 11
     *
302 1
     * @param  bool $bool
303 1
     * @return \phpbu\App\Cli\Executable\Mysqldump
304 10
     */
305 1
    public function produceFilePerTable($bool)
306 1
    {
307 9
        $this->filePerTable = $bool;
308
        return $this;
309
    }
310
311 11
    /**
312 1
     * Set the dump target path.
313 1
     *
314 1
     * @param  string $path
315 1
     * @return \phpbu\App\Cli\Executable\Mysqldump
316 11
     */
317 1
    public function dumpTo($path)
318 1
    {
319 10
        $this->dumpPathname = $path;
320 1
        return $this;
321 1
    }
322 1
323 1
    /**
324 1
     * Process generator
325 1
     */
326 1
    protected function createProcess()
327 1
    {
328 1
        $process = new Process();
329 1
        $cmd     = new Cmd($this->binary);
330
        $process->addCommand($cmd);
331 11
332 11
        $cmd->addOptionIfNotEmpty('--user', $this->user);
333
        $cmd->addOptionIfNotEmpty('--password', $this->password);
334
        $cmd->addOptionIfNotEmpty('--host', $this->host);
335
        $cmd->addOptionIfNotEmpty('--lock-tables', $this->lockTables, false);
336
        $cmd->addOptionIfNotEmpty('-q', $this->quick, false);
337
        $cmd->addOptionIfNotEmpty('-C', $this->compress, false);
338
        $cmd->addOptionIfNotEmpty('-e', $this->extendedInsert, false);
339
        $cmd->addOptionIfNotEmpty('--hex-blob', $this->hexBlob, false);
340
341
        $this->configureSourceData($cmd);
342
        $this->configureIgnoredTables($cmd);
343
344
        if ($this->filePerTable) {
345
            $cmd->addOption('--tab', $this->dumpPathname);
346
        }
347
348
        if ($this->noData) {
349
            $cmd->addOption('--no-data');
350
        } else {
351
            if (count($this->structureOnly)) {
352
                $cmd2 = clone($cmd);
353
                foreach ($this->structureOnly as $table) {
354
                    $cmd2->addOption('--ignore-table', $table);
355
                }
356
                $cmd2->addOption('--skip-add-drop-table');
357
                $cmd2->addOption('--no-create-db');
358
                $cmd2->addOption('--no-create-info');
359
                $cmd->addOption('--no-data');
360
                $process->addCommand($cmd2);
361
            }
362
        }
363
        $this->configureOutput($process);
364
        return $process;
365
    }
366
367
    /**
368
     * Configure source data (tables, databases).
369
     *
370
     * @param  \phpbu\App\Cli\Cmd $cmd
371
     * @throws \phpbu\App\Exception
372
     */
373
    private function configureSourceData(Cmd $cmd)
374
    {
375
        if (count($this->tablesToDump)) {
376
            $this->configureSourceTables($cmd);
377
        } else {
378
            $this->configureSourceDatabases($cmd);
379
        }
380
    }
381
382
    /**
383
     * Configure source tables.
384
     *
385
     * @param  \phpbu\App\Cli\Cmd $cmd
386
     * @throws \phpbu\App\Exception
387
     */
388
    private function configureSourceTables(Cmd $cmd)
389
    {
390
        if (count($this->databasesToDump) !== 1) {
391
            throw new Exception('mysqldump --tables needs exactly one database');
392
        }
393
        $cmd->addArgument($this->databasesToDump[0]);
394
        $cmd->addOption('--tables', $this->tablesToDump);
395
    }
396
397
    /**
398
     * Configure source databases.
399
     *
400
     * @param  \phpbu\App\Cli\Cmd $cmd
401
     */
402
    private function configureSourceDatabases(Cmd $cmd)
403
    {
404
        $databasesToDump = count($this->databasesToDump);
405
406
        // different handling for different ammounts of databases
407
        if ($databasesToDump == 1) {
408
            // single database use argument
409
            $cmd->addArgument($this->databasesToDump[0]);
410
        } elseif ($databasesToDump > 1) {
411
            // multiple databases add list with --databases
412
            $cmd->addOption('--databases', $this->databasesToDump);
413
        } else {
414
            // no databases set dump all databases
415
            $cmd->addOption('--all-databases');
416
        }
417
    }
418
419
    /**
420
     * Add --ignore-table options
421
     *
422
     * @param \phpbu\App\Cli\Cmd $cmd
423
     */
424
    private function configureIgnoredTables(Cmd $cmd)
425
    {
426
        if (count($this->tablesToIgnore)) {
427
            foreach ($this->tablesToIgnore as $table) {
428
                $cmd->addOption('--ignore-table', $table);
429
            }
430
        }
431
    }
432
433
    /**
434
     * Configure output redirect.
435
     *
436
     * @param \phpbu\App\Cli\Process $process
437
     */
438
    private function configureOutput(Process $process)
439
    {
440
        if (!$this->filePerTable) {
441
            $process->redirectOutputTo($this->dumpPathname);
442
        }
443
    }
444
}
445