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
|
|
|
* Port to connect to |
40
|
|
|
* --port <port> |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $port; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* User to connect with |
48
|
|
|
* --user <username> |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $user; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Password to authenticate with |
56
|
|
|
* --password <password> |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $password; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* List of tables to backup |
64
|
|
|
* --tables array of strings |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $tables; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* List of databases to backup |
72
|
|
|
* --databases array of strings |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
private $databases; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* List of tables to ignore |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
private $ignoreTables; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* List of tables where only the table structure is stored |
87
|
|
|
* |
88
|
|
|
* @var array |
89
|
|
|
*/ |
90
|
|
|
private $structureOnly; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Table separated data files |
94
|
|
|
* --tab |
95
|
|
|
* |
96
|
|
|
* @var boolean |
97
|
|
|
*/ |
98
|
|
|
private $filePerTable; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Use mysqldump quick mode |
102
|
|
|
* -q |
103
|
|
|
* |
104
|
|
|
* @var boolean |
105
|
|
|
*/ |
106
|
|
|
private $quick; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Lock tables option |
110
|
|
|
* --lock-tables |
111
|
|
|
* |
112
|
|
|
* @var bool |
113
|
|
|
*/ |
114
|
|
|
private $lockTables; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Single Transaction option |
118
|
|
|
* --single-transaction |
119
|
|
|
* |
120
|
|
|
* @var bool |
121
|
|
|
*/ |
122
|
|
|
private $singleTransaction; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Use mysqldump with compression |
126
|
|
|
* -C |
127
|
|
|
* |
128
|
|
|
* @var boolean |
129
|
|
|
*/ |
130
|
|
|
private $compress; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Use mysqldump with extended insert |
134
|
|
|
* -e |
135
|
|
|
* |
136
|
|
|
* @var boolean |
137
|
|
|
*/ |
138
|
|
|
private $extendedInsert; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Dump blob fields as hex. |
142
|
|
|
* --hex-blob |
143
|
|
|
* |
144
|
|
|
* @var boolean |
145
|
|
|
*/ |
146
|
|
|
private $hexBlob; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Dump only table structures |
150
|
|
|
* --no-data |
151
|
|
|
* |
152
|
|
|
* @var boolean |
153
|
|
|
*/ |
154
|
|
|
private $noData; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add general transaction id statement. |
158
|
|
|
* --set-gids-purged |
159
|
|
|
* |
160
|
|
|
* @var string |
161
|
|
|
*/ |
162
|
|
|
private $gtidPurged; |
163
|
13 |
|
|
164
|
|
|
/** |
165
|
13 |
|
* Setup. |
166
|
|
|
* |
167
|
13 |
|
* @see \phpbu\App\Backup\Source |
168
|
13 |
|
* @param array $conf |
169
|
13 |
|
* @throws \phpbu\App\Exception |
170
|
13 |
|
*/ |
171
|
13 |
|
public function setup(array $conf = []) |
172
|
13 |
|
{ |
173
|
13 |
|
$this->setupSourceData($conf); |
174
|
13 |
|
|
175
|
13 |
|
$this->pathToMysqldump = Util\Arr::getValue($conf, 'pathToMysqldump', ''); |
176
|
13 |
|
$this->host = Util\Arr::getValue($conf, 'host', ''); |
177
|
13 |
|
$this->port = Util\Arr::getValue($conf, 'port', 0); |
178
|
13 |
|
$this->user = Util\Arr::getValue($conf, 'user', ''); |
179
|
13 |
|
$this->password = Util\Arr::getValue($conf, 'password', ''); |
180
|
|
|
$this->gtidPurged = Util\Arr::getValue($conf, 'gtidPurged', ''); |
181
|
|
|
$this->hexBlob = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false); |
182
|
13 |
|
$this->quick = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false); |
183
|
1 |
|
$this->lockTables = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false); |
184
|
|
|
$this->singleTransaction = Util\Str::toBoolean(Util\Arr::getValue($conf, 'singleTransaction', ''), false); |
185
|
12 |
|
$this->compress = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false); |
186
|
|
|
$this->extendedInsert = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false); |
187
|
|
|
$this->noData = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false); |
188
|
|
|
$this->filePerTable = Util\Str::toBoolean(Util\Arr::getValue($conf, 'filePerTable', ''), false); |
189
|
|
|
|
190
|
|
|
// this doesn't fail, but it doesn't work, so throw an exception so the user understands |
191
|
|
|
if ($this->filePerTable && count($this->structureOnly)) { |
192
|
13 |
|
throw new Exception('\'structureOnly\' can not be used with the \'filePerTable\' option'); |
193
|
|
|
} |
194
|
13 |
|
} |
195
|
13 |
|
|
196
|
13 |
|
/** |
197
|
13 |
|
* Get tables and databases to backup. |
198
|
13 |
|
* |
199
|
|
|
* @param array $conf |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
protected function setupSourceData(array $conf) |
202
|
|
|
{ |
203
|
|
|
$this->tables = Util\Str::toList(Util\Arr::getValue($conf, 'tables', '')); |
204
|
|
|
$this->databases = Util\Str::toList(Util\Arr::getValue($conf, 'databases', '')); |
205
|
|
|
$this->ignoreTables = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables', '')); |
206
|
|
|
$this->structureOnly = Util\Str::toList(Util\Arr::getValue($conf, 'structureOnly', '')); |
207
|
|
|
} |
208
|
|
|
|
209
|
4 |
|
/** |
210
|
|
|
* Execute the backup. |
211
|
|
|
* |
212
|
4 |
|
* @see \phpbu\App\Backup\Source |
213
|
1 |
|
* @param \phpbu\App\Backup\Target $target |
214
|
1 |
|
* @param \phpbu\App\Result $result |
215
|
1 |
|
* @return \phpbu\App\Backup\Source\Status |
216
|
|
|
* @throws \phpbu\App\Exception |
217
|
|
|
*/ |
218
|
4 |
|
public function backup(Target $target, Result $result) : Status |
219
|
|
|
{ |
220
|
4 |
|
// create the writable dump directory for tables files |
221
|
|
|
if ($this->filePerTable && !is_dir($this->getDumpTarget($target))) { |
222
|
4 |
|
$old = umask(0); |
223
|
1 |
|
mkdir($this->getDumpTarget($target), 0777, true); |
224
|
|
|
umask($old); |
225
|
|
|
} |
226
|
3 |
|
|
227
|
|
|
$mysqldump = $this->execute($target); |
228
|
|
|
|
229
|
|
|
$result->debug($this->getExecutable($target)->getCommandPrintable()); |
230
|
|
|
|
231
|
|
|
if (!$mysqldump->isSuccessful()) { |
232
|
|
|
throw new Exception('mysqldump failed:' . $mysqldump->getStdErr()); |
233
|
|
|
} |
234
|
|
|
|
235
|
12 |
|
return $this->createStatus($target); |
236
|
|
|
} |
237
|
12 |
|
|
238
|
12 |
|
/** |
239
|
12 |
|
* Create the Executable to run the mysqldump command. |
240
|
12 |
|
* |
241
|
12 |
|
* @param \phpbu\App\Backup\Target $target |
242
|
12 |
|
* @return \phpbu\App\Cli\Executable |
243
|
12 |
|
*/ |
244
|
12 |
|
protected function createExecutable(Target $target) : Executable |
245
|
12 |
|
{ |
246
|
12 |
|
$executable = new Executable\Mysqldump($this->pathToMysqldump); |
247
|
12 |
|
$executable->credentials($this->user, $this->password) |
248
|
12 |
|
->useHost($this->host) |
249
|
12 |
|
->usePort($this->port) |
250
|
12 |
|
->useQuickMode($this->quick) |
251
|
12 |
|
->lockTables($this->lockTables) |
252
|
12 |
|
->dumpBlobsHexadecimal($this->hexBlob) |
253
|
12 |
|
->addGTIDStatement($this->gtidPurged) |
254
|
|
|
->useCompression($this->compress) |
255
|
12 |
|
->useExtendedInsert($this->extendedInsert) |
256
|
2 |
|
->dumpTables($this->tables) |
257
|
|
|
->singleTransaction($this->singleTransaction) |
258
|
12 |
|
->dumpDatabases($this->databases) |
259
|
|
|
->ignoreTables($this->ignoreTables) |
260
|
|
|
->produceFilePerTable($this->filePerTable) |
261
|
|
|
->dumpNoData($this->noData) |
262
|
|
|
->dumpStructureOnly($this->structureOnly) |
263
|
|
|
->dumpTo($this->getDumpTarget($target)); |
264
|
|
|
// if compression is active and commands can be piped |
265
|
|
|
if ($this->isHandlingCompression($target)) { |
266
|
|
|
$executable->compressOutput($target->getCompression()); |
267
|
4 |
|
} |
268
|
|
|
return $executable; |
269
|
|
|
} |
270
|
4 |
|
|
271
|
1 |
|
/** |
272
|
|
|
* Create backup status. |
273
|
|
|
* |
274
|
|
|
* @param \phpbu\App\Backup\Target |
275
|
|
|
* @return \phpbu\App\Backup\Source\Status |
276
|
3 |
|
*/ |
277
|
1 |
|
protected function createStatus(Target $target) : Status |
278
|
|
|
{ |
279
|
|
|
// file_per_table creates a directory with all the files |
280
|
|
|
if ($this->filePerTable) { |
281
|
2 |
|
return Status::create()->uncompressedDirectory($this->getDumpTarget($target)); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
// if compression is active and commands can be piped |
285
|
|
|
// compression is handled via pipe |
286
|
|
|
if ($this->isHandlingCompression($target)) { |
287
|
|
|
return Status::create(); |
288
|
|
|
} |
289
|
|
|
|
290
|
12 |
|
// default create uncompressed dump file |
291
|
|
|
return Status::create()->uncompressedFile($this->getDumpTarget($target)); |
292
|
12 |
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Cann compression be handled via pipe operator. |
296
|
|
|
* |
297
|
|
|
* @param \phpbu\App\Backup\Target $target |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
|
|
private function isHandlingCompression(Target $target) : bool |
301
|
12 |
|
{ |
302
|
|
|
return $target->shouldBeCompressed() && Util\Cli::canPipe() && $target->getCompression()->isPipeable(); |
303
|
12 |
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Return dump target path. |
307
|
|
|
* |
308
|
|
|
* @param \phpbu\App\Backup\Target $target |
309
|
|
|
* @return string |
310
|
|
|
*/ |
311
|
|
|
private function getDumpTarget(Target $target) : string |
312
|
|
|
{ |
313
|
|
|
return $target->getPathnamePlain() . ($this->filePerTable ? '.dump' : ''); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|