|
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
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Mysqldump Executable class. |
|
10
|
|
|
* |
|
11
|
|
|
* @package phpbu |
|
12
|
|
|
* @subpackage Backup |
|
13
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
14
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
15
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
16
|
|
|
* @link http://phpbu.de/ |
|
17
|
|
|
* @since Class available since Release 1.0.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
class Mysqldump extends Abstraction implements Executable |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Host to connect to |
|
23
|
|
|
* --host <hostname> |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $host; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* User to connect with |
|
31
|
|
|
* --user <username> |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $user; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Password to authenticate with |
|
39
|
|
|
* --password <password> |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $password; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* List of tables to backup |
|
47
|
|
|
* --tables array of strings |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
private $tablesToDump = array(); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* List of databases to backup |
|
55
|
|
|
* --databases array of strings |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private $databasesToDump = array(); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* List of tables to ignore |
|
63
|
|
|
* |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
private $tablesToIgnore = array(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* List of tables where only the table structure is stored |
|
70
|
|
|
* |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
private $structureOnly = array(); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Use mysqldump quick mode |
|
77
|
|
|
* -q |
|
78
|
|
|
* |
|
79
|
|
|
* @var bool |
|
80
|
|
|
*/ |
|
81
|
|
|
private $quick = false; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* |
|
85
|
|
|
* Lock tables option |
|
86
|
|
|
* --lock-tables |
|
87
|
|
|
* |
|
88
|
|
|
* @var bool |
|
89
|
|
|
*/ |
|
90
|
|
|
private $lockTables; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Use mysqldump with compression |
|
94
|
|
|
* -C |
|
95
|
|
|
* |
|
96
|
|
|
* @var bool |
|
97
|
|
|
*/ |
|
98
|
|
|
private $compress = false; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Dump only table structures |
|
102
|
|
|
* --no-data |
|
103
|
|
|
* |
|
104
|
|
|
* @var boolean |
|
105
|
|
|
*/ |
|
106
|
|
|
private $noData = false; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Use mysqldump extended insert mode |
|
110
|
|
|
* -e, --extended-insert |
|
111
|
|
|
* |
|
112
|
|
|
* @var boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
private $extendedInsert = false; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Dump blob fields as hex. |
|
118
|
|
|
* --hex-blob |
|
119
|
|
|
* |
|
120
|
|
|
* @var boolean |
|
121
|
|
|
*/ |
|
122
|
|
|
private $hexBlob = false; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Path to dump file |
|
126
|
|
|
* |
|
127
|
14 |
|
* @var string |
|
128
|
|
|
*/ |
|
129
|
14 |
|
private $dumpPathname; |
|
130
|
14 |
|
|
|
131
|
14 |
|
/** |
|
132
|
|
|
* Constructor. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $path |
|
135
|
|
|
*/ |
|
136
|
|
|
public function __construct($path = null) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->cmd = 'mysqldump'; |
|
139
|
|
|
parent::__construct($path); |
|
140
|
3 |
|
} |
|
141
|
|
|
|
|
142
|
3 |
|
/** |
|
143
|
3 |
|
* Set the mysql credentials. |
|
144
|
3 |
|
* |
|
145
|
|
|
* @param string $user |
|
146
|
|
|
* @param string $password |
|
147
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
148
|
|
|
*/ |
|
149
|
|
|
public function credentials($user = null, $password = null) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->user = $user; |
|
152
|
|
|
$this->password = $password; |
|
153
|
3 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
3 |
|
|
|
156
|
3 |
|
/** |
|
157
|
|
|
* Set the mysql hostname. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $host |
|
160
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
161
|
|
|
*/ |
|
162
|
|
|
public function useHost($host) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->host = $host; |
|
165
|
3 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
3 |
|
|
|
168
|
3 |
|
/** |
|
169
|
|
|
* Use '-q' quick mode. |
|
170
|
|
|
* |
|
171
|
|
|
* @param boolean $bool |
|
172
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
173
|
|
|
*/ |
|
174
|
|
|
public function useQuickMode($bool) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->quick = $bool; |
|
177
|
3 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
3 |
|
|
|
180
|
3 |
|
/** |
|
181
|
|
|
* Use '--lock-tables' option. |
|
182
|
|
|
* |
|
183
|
|
|
* @param boolean $bool |
|
184
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
185
|
|
|
*/ |
|
186
|
|
|
public function lockTables($bool) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->lockTables = $bool; |
|
189
|
4 |
|
return $this; |
|
190
|
|
|
} |
|
191
|
4 |
|
|
|
192
|
4 |
|
/** |
|
193
|
|
|
* Use '-C' compress mode. |
|
194
|
|
|
* |
|
195
|
|
|
* @param boolean $bool |
|
196
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
197
|
|
|
*/ |
|
198
|
|
|
public function useCompression($bool) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->compress = $bool; |
|
201
|
4 |
|
return $this; |
|
202
|
|
|
} |
|
203
|
4 |
|
|
|
204
|
4 |
|
/** |
|
205
|
|
|
* Use '-e' extended insert mode. |
|
206
|
|
|
* |
|
207
|
|
|
* @param boolean $bool |
|
208
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
209
|
|
|
*/ |
|
210
|
|
|
public function useExtendedInsert($bool) |
|
211
|
|
|
{ |
|
212
|
|
|
$this->extendedInsert = $bool; |
|
213
|
4 |
|
return $this; |
|
214
|
|
|
} |
|
215
|
4 |
|
|
|
216
|
4 |
|
/** |
|
217
|
|
|
* Use '--hex-blob' to encode binary fields. |
|
218
|
|
|
* |
|
219
|
|
|
* @param boolean $bool |
|
220
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
221
|
|
|
*/ |
|
222
|
|
|
public function dumpBlobsHexadecimal($bool) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->hexBlob = $bool; |
|
225
|
4 |
|
return $this; |
|
226
|
|
|
} |
|
227
|
4 |
|
|
|
228
|
4 |
|
/** |
|
229
|
|
|
* Set tables to dump. |
|
230
|
|
|
* |
|
231
|
|
|
* @param array $tables |
|
232
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
233
|
|
|
*/ |
|
234
|
|
|
public function dumpTables(array $tables) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->tablesToDump = $tables; |
|
237
|
4 |
|
return $this; |
|
238
|
|
|
} |
|
239
|
4 |
|
|
|
240
|
4 |
|
/** |
|
241
|
|
|
* Set databases to dump. |
|
242
|
|
|
* |
|
243
|
|
|
* @param array $databases |
|
244
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
245
|
|
|
*/ |
|
246
|
|
|
public function dumpDatabases(array $databases) |
|
247
|
|
|
{ |
|
248
|
|
|
$this->databasesToDump = $databases; |
|
249
|
4 |
|
return $this; |
|
250
|
|
|
} |
|
251
|
4 |
|
|
|
252
|
4 |
|
/** |
|
253
|
|
|
* Set tables to ignore. |
|
254
|
|
|
* |
|
255
|
|
|
* @param array $tables |
|
256
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
257
|
|
|
*/ |
|
258
|
|
|
public function ignoreTables(array $tables) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->tablesToIgnore = $tables; |
|
261
|
4 |
|
return $this; |
|
262
|
|
|
} |
|
263
|
4 |
|
|
|
264
|
4 |
|
/** |
|
265
|
|
|
* Set tables where only table structure should be dumped. |
|
266
|
|
|
* |
|
267
|
|
|
* @param array $tables |
|
268
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
269
|
|
|
*/ |
|
270
|
|
|
public function dumpStructureOnly(array $tables) |
|
271
|
|
|
{ |
|
272
|
|
|
$this->structureOnly = $tables; |
|
273
|
3 |
|
return $this; |
|
274
|
|
|
} |
|
275
|
3 |
|
|
|
276
|
3 |
|
/** |
|
277
|
|
|
* Dump no table data at all. |
|
278
|
|
|
* |
|
279
|
|
|
* @param boolean $bool |
|
280
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
281
|
|
|
*/ |
|
282
|
11 |
|
public function dumpNoData($bool) |
|
283
|
|
|
{ |
|
284
|
11 |
|
$this->noData = $bool; |
|
285
|
11 |
|
return $this; |
|
286
|
11 |
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
11 |
|
* Set the dump target path. |
|
290
|
11 |
|
* |
|
291
|
|
|
* @param string $path |
|
292
|
11 |
|
* @return \phpbu\App\Cli\Executable\Mysqldump |
|
293
|
11 |
|
*/ |
|
294
|
11 |
|
public function dumpTo($path) |
|
295
|
11 |
|
{ |
|
296
|
11 |
|
$this->dumpPathname = $path; |
|
297
|
11 |
|
return $this; |
|
298
|
11 |
|
} |
|
299
|
11 |
|
|
|
300
|
|
|
/** |
|
301
|
11 |
|
* Process generator |
|
302
|
1 |
|
*/ |
|
303
|
1 |
|
protected function createProcess() |
|
304
|
10 |
|
{ |
|
305
|
1 |
|
$process = new Process(); |
|
306
|
1 |
|
$cmd = new Cmd($this->binary); |
|
307
|
9 |
|
$process->addCommand($cmd); |
|
308
|
|
|
|
|
309
|
|
|
// no std error unless it is activated |
|
310
|
|
|
if (!$this->showStdErr) { |
|
311
|
11 |
|
$cmd->silence(); |
|
312
|
1 |
|
// i kill you |
|
313
|
1 |
|
} |
|
314
|
1 |
|
$cmd->addOptionIfNotEmpty('--user', $this->user); |
|
315
|
1 |
|
$cmd->addOptionIfNotEmpty('--password', $this->password); |
|
316
|
11 |
|
$cmd->addOptionIfNotEmpty('--host', $this->host); |
|
317
|
1 |
|
$cmd->addOptionIfNotEmpty('--lock-tables', $this->lockTables, false); |
|
318
|
1 |
|
$cmd->addOptionIfNotEmpty('-q', $this->quick, false); |
|
319
|
10 |
|
$cmd->addOptionIfNotEmpty('-C', $this->compress, false); |
|
320
|
1 |
|
$cmd->addOptionIfNotEmpty('-e', $this->extendedInsert, false); |
|
321
|
1 |
|
$cmd->addOptionIfNotEmpty('--hex-blob', $this->hexBlob, false); |
|
322
|
1 |
|
|
|
323
|
1 |
|
if (count($this->tablesToDump)) { |
|
324
|
1 |
|
$cmd->addOption('--tables', $this->tablesToDump); |
|
325
|
1 |
|
} else { |
|
326
|
1 |
|
if (count($this->databasesToDump)) { |
|
327
|
1 |
|
$cmd->addOption('--databases', $this->databasesToDump); |
|
328
|
1 |
|
} else { |
|
329
|
1 |
|
$cmd->addOption('--all-databases'); |
|
330
|
|
|
} |
|
331
|
11 |
|
} |
|
332
|
11 |
|
|
|
333
|
|
|
if (count($this->tablesToIgnore)) { |
|
334
|
|
|
foreach ($this->tablesToIgnore as $table) { |
|
335
|
|
|
$cmd->addOption('--ignore-table', $table); |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
if ($this->noData) { |
|
339
|
|
|
$cmd->addOption('--no-data'); |
|
340
|
|
|
} else { |
|
341
|
|
|
if (count($this->structureOnly)) { |
|
342
|
|
|
$cmd2 = clone($cmd); |
|
343
|
|
|
foreach ($this->structureOnly as $table) { |
|
344
|
|
|
$cmd2->addOption('--ignore-table', $table); |
|
345
|
|
|
} |
|
346
|
|
|
$cmd2->addOption('--skip-add-drop-table'); |
|
347
|
|
|
$cmd2->addOption('--no-create-db'); |
|
348
|
|
|
$cmd2->addOption('--no-create-info'); |
|
349
|
|
|
$cmd->addOption('--no-data'); |
|
350
|
|
|
$process->addCommand($cmd2); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
$process->redirectOutputTo($this->dumpPathname); |
|
354
|
|
|
return $process; |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|