Completed
Push — master ( 6a1fad...afb84c )
by Sebastian
05:39
created

Mysqldump::getDumpTarget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace phpbu\App\Backup\Source;
3
4
use phpbu\App\Backup\Target;
5
use phpbu\App\Cli\Executable;
6
use phpbu\App\Exception;
7
use phpbu\App\Result;
8
use phpbu\App\Util;
9
10
/**
11
 * Mysqldump source class.
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 * @since      Class available since Release 1.0.0
20
 */
21
class Mysqldump extends SimulatorExecutable implements Simulator
22
{
23
    /**
24
     * Path to executable.
25
     *
26
     * @var string
27
     */
28
    private $pathToMysqldump;
29
30
    /**
31
     * Host to connect to
32
     * --host <hostname>
33
     *
34
     * @var string
35
     */
36
    private $host;
37
38
    /**
39
     * User to connect with
40
     * --user <username>
41
     *
42
     * @var string
43
     */
44
    private $user;
45
46
    /**
47
     * Password to authenticate with
48
     * --password <password>
49
     *
50
     * @var string
51
     */
52
    private $password;
53
54
    /**
55
     * List of tables to backup
56
     * --tables array of strings
57
     *
58
     * @var array
59
     */
60
    private $tables;
61
62
    /**
63
     * List of databases to backup
64
     * --databases array of strings
65
     *
66
     * @var array
67
     */
68
    private $databases;
69
70
    /**
71
     * List of tables to ignore
72
     *
73
     * @var array
74
     */
75
    private $ignoreTables;
76
77
    /**
78
     * List of tables where only the table structure is stored
79
     *
80
     * @var array
81
     */
82
    private $structureOnly;
83
84
    /**
85
     * Table separated data files
86
     * --tab
87
     *
88
     * @var boolean
89
     */
90
    private $filePerTable;
91
92
    /**
93
     * Use mysqldump quick mode
94
     * -q
95
     *
96
     * @var boolean
97
     */
98
    private $quick;
99
100
    /**
101
     * Lock tables option
102
     * --lock-tables
103
     *
104
     * @var bool
105
     */
106
    private $lockTables;
107
108
    /**
109
     * Single Transaction option
110
     * --single-transaction
111
     *
112
     * @var bool
113
     */
114
    private $singleTransaction;
115
116
    /**
117
     * Use mysqldump with compression
118
     * -C
119
     *
120
     * @var boolean
121
     */
122
    private $compress;
123
124
    /**
125
     * Use mysqldump with extended insert
126
     * -e
127
     *
128
     * @var boolean
129
     */
130
    private $extendedInsert;
131
132
    /**
133
     * Dump blob fields as hex.
134
     * --hex-blob
135
     *
136
     * @var boolean
137
     */
138
    private $hexBlob;
139
140
    /**
141
     * Dump only table structures
142
     * --no-data
143
     *
144
     * @var boolean
145
     */
146 5
    private $noData;
147
148 5
    /**
149
     * Setup.
150 5
     *
151 5
     * @see    \phpbu\App\Backup\Source
152 5
     * @param  array $conf
153 5
     * @throws \phpbu\App\Exception
154 5
     */
155 5
    public function setup(array $conf = [])
156 5
    {
157 5
        $this->setupSourceData($conf);
158 5
159 5
        $this->pathToMysqldump   = Util\Arr::getValue($conf, 'pathToMysqldump', '');
160 5
        $this->host              = Util\Arr::getValue($conf, 'host', '');
161
        $this->user              = Util\Arr::getValue($conf, 'user', '');
162
        $this->password          = Util\Arr::getValue($conf, 'password', '');
163
        $this->hexBlob           = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false);
164
        $this->quick             = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false);
165
        $this->lockTables        = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false);
166
        $this->singleTransaction = Util\Str::toBoolean(Util\Arr::getValue($conf, 'singleTransaction', ''), false);
167 5
        $this->compress          = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false);
168
        $this->extendedInsert    = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false);
169 5
        $this->noData            = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false);
170 5
        $this->filePerTable      = Util\Str::toBoolean(Util\Arr::getValue($conf, 'filePerTable', ''), false);
171 5
172 5
        // this doesn't fail, but it doesn't work, so throw an exception so the user understands
173 5
        if ($this->filePerTable && count($this->structureOnly)) {
174
            throw new Exception('\'structureOnly\' can not be used with the \'filePerTable\' option');
175
        }
176
    }
177
178
    /**
179
     * Get tables and databases to backup.
180
     *
181
     * @param array $conf
182
     */
183 View Code Duplication
    protected function setupSourceData(array $conf)
184 2
    {
185
        $this->tables        = Util\Str::toList(Util\Arr::getValue($conf, 'tables', ''));
186
        $this->databases     = Util\Str::toList(Util\Arr::getValue($conf, 'databases', ''));
187 2
        $this->ignoreTables  = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables', ''));
188 2
        $this->structureOnly = Util\Str::toList(Util\Arr::getValue($conf, 'structureOnly', ''));
189
    }
190 2
191
    /**
192 2
     * Execute the backup.
193 1
     *
194
     * @see    \phpbu\App\Backup\Source
195
     * @param  \phpbu\App\Backup\Target $target
196 1
     * @param  \phpbu\App\Result        $result
197
     * @return \phpbu\App\Backup\Source\Status
198
     * @throws \phpbu\App\Exception
199
     */
200
    public function backup(Target $target, Result $result) : Status
201
    {
202
        // create the writable dump directory for tables files
203
        if ($this->filePerTable && !is_dir($this->getDumpTarget($target))) {
204
            $old = umask(0);
205 5
            mkdir($this->getDumpTarget($target), 0777, true);
206
            umask($old);
207 5
        }
208 3
209 3
        $mysqldump = $this->execute($target);
210 3
211 3
        $result->debug($this->getExecutable($target)->getCommandPrintable());
212 3
213 3
        if (!$mysqldump->isSuccessful()) {
214 3
            throw new Exception('mysqldump failed:' . $mysqldump->getStdErr());
215 3
        }
216 3
217 3
        return $this->createStatus($target);
218 3
    }
219 3
220 3
    /**
221 3
     * Create the Executable to run the mysqldump command.
222 3
     *
223 5
     * @param  \phpbu\App\Backup\Target $target
224
     * @return \phpbu\App\Cli\Executable
225
     */
226
    protected function createExecutable(Target $target) : Executable
227
    {
228
        $executable = new Executable\Mysqldump($this->pathToMysqldump);
229
        $executable->credentials($this->user, $this->password)
230
                   ->useHost($this->host)
231
                   ->useQuickMode($this->quick)
232
                   ->lockTables($this->lockTables)
233
                   ->dumpBlobsHexadecimal($this->hexBlob)
234
                   ->useCompression($this->compress)
235
                   ->useExtendedInsert($this->extendedInsert)
236
                   ->dumpTables($this->tables)
237
                   ->singleTransaction($this->singleTransaction)
238
                   ->dumpDatabases($this->databases)
239
                   ->ignoreTables($this->ignoreTables)
240
                   ->produceFilePerTable($this->filePerTable)
241
                   ->dumpNoData($this->noData)
242
                   ->dumpStructureOnly($this->structureOnly)
243
                   ->dumpTo($this->getDumpTarget($target));
244
        // if compression is active and commands can be piped
245
        if ($this->isHandlingCompression($target)) {
246
            $executable->compressOutput($target->getCompression());
247
        }
248
        return $executable;
249
    }
250
251
    /**
252
     * Create backup status.
253
     *
254
     * @param  \phpbu\App\Backup\Target
255
     * @return \phpbu\App\Backup\Source\Status
256
     */
257
    protected function createStatus(Target $target) : Status
258
    {
259
        // file_per_table creates a directory with all the files
260
        if ($this->filePerTable) {
261
            return Status::create()->uncompressedDirectory($this->getDumpTarget($target));
262
        }
263
264
        // if compression is active and commands can be piped
265
        // compression is handled via pipe
266
        if ($this->isHandlingCompression($target)) {
267
            return Status::create();
268
        }
269
270
        // default create uncompressed dump file
271
        return Status::create()->uncompressedFile($this->getDumpTarget($target));
272
    }
273
274
    /**
275
     * Cann compression be handled via pipe operator.
276
     *
277
     * @param  \phpbu\App\Backup\Target $target
278
     * @return bool
279
     */
280
    private function isHandlingCompression(Target $target) : bool
281
    {
282
        return $target->shouldBeCompressed() && Util\Cli::canPipe() && $target->getCompression()->isPipeable();
283
    }
284
285
    /**
286
     * Return dump target path.
287
     *
288
     * @param  \phpbu\App\Backup\Target $target
289
     * @return string
290
     */
291
    private function getDumpTarget(Target $target) : string
292
    {
293
        return $target->getPathnamePlain() . ($this->filePerTable ? '.dump' : '');
294
    }
295
}
296