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
|
|
|
* Pgdump 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 Pgdump extends Abstraction implements Executable |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Host to connect to |
24
|
|
|
* --host=<hostname> |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $host; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Port to connect to |
32
|
|
|
* --port=<portnumber> |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $port; |
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
|
|
|
* Database to dump |
56
|
|
|
* db-name |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $databaseToDump; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* List of schmeas to dump |
64
|
|
|
* --schema=<schema> |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $schemasToDump = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Exclude Schemas |
72
|
|
|
* --exclude-schema=<schema> |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
private $schemasToExclude = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Tables to dump. |
80
|
|
|
* --table=<table> |
81
|
|
|
* |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
private $tablesToDump = []; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* List of tables to exclude |
88
|
|
|
* --exclude-table=<table> |
89
|
|
|
* |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
private $tablesToExclude = []; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Don't dump the structure |
96
|
|
|
* --data-only |
97
|
|
|
* |
98
|
|
|
* @var boolean |
99
|
|
|
*/ |
100
|
|
|
private $dataOnly; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Dump only schema definitions. |
104
|
|
|
* --schema-only |
105
|
|
|
* |
106
|
|
|
* @var boolean |
107
|
|
|
*/ |
108
|
|
|
private $schemaOnly; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Do not dump data for any tables matching the table pattern. |
112
|
|
|
* --exclude-table-data |
113
|
|
|
* |
114
|
|
|
* @var array |
115
|
|
|
*/ |
116
|
|
|
private $excludeTableData = []; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add drop statements to the dump. |
120
|
|
|
* --clean |
121
|
|
|
* |
122
|
|
|
* @var boolean |
123
|
|
|
*/ |
124
|
|
|
private $clean = false; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Encoding of the dump file |
128
|
|
|
* --encoding |
129
|
|
|
* |
130
|
|
|
* @var boolean |
131
|
|
|
*/ |
132
|
|
|
private $encoding; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* postgreSQLdump format definition |
136
|
|
|
* --format [plain|custom|directory|tar] |
137
|
|
|
* |
138
|
|
|
* @var string |
139
|
|
|
*/ |
140
|
|
|
private $format; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Allow any user to restore the dump |
144
|
|
|
* --no-owner |
145
|
|
|
* |
146
|
|
|
* @var bool |
147
|
|
|
*/ |
148
|
|
|
private $noOwner = false; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Prevent dumping of access privileges. |
152
|
|
|
* --no-acl |
153
|
|
|
* |
154
|
|
|
* @var boolean |
155
|
|
|
*/ |
156
|
|
|
private $noPrivileges; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Do not output commands to select tablespaces. |
160
|
|
|
* --no-tablespaces |
161
|
|
|
* |
162
|
|
|
* @var boolean |
163
|
|
|
*/ |
164
|
|
|
private $noTablespaces; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* File to dump to |
168
|
|
|
* --file |
169
|
|
|
* |
170
|
|
|
* @var string |
171
|
|
|
*/ |
172
|
|
|
private $file; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* List of available output formats |
176
|
|
|
* |
177
|
|
|
* @var array |
178
|
|
|
*/ |
179
|
|
|
private $availableFormats = [ |
180
|
|
|
'p' => true, |
181
|
|
|
'plain' => true, |
182
|
|
|
'c' => true, |
183
|
|
|
'custom' => true, |
184
|
|
|
'd' => true, |
185
|
|
|
'directory' => true, |
186
|
|
|
't' => true, |
187
|
|
|
'tar' => true, |
188
|
|
|
]; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Constructor. |
192
|
|
|
* |
193
|
|
|
* @param string $path |
194
|
|
|
*/ |
195
|
|
|
public function __construct($path = null) |
196
|
|
|
{ |
197
|
|
|
$this->cmd = 'pg_dump'; |
198
|
|
|
parent::__construct($path); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set the postgreSQL credentials. |
203
|
|
|
* |
204
|
|
|
* @param string $user |
205
|
|
|
* @param string $password |
206
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
207
|
|
|
*/ |
208
|
|
|
public function credentials($user = null, $password = null) |
209
|
|
|
{ |
210
|
|
|
$this->user = $user; |
211
|
|
|
$this->password = $password; |
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Set the postgreSQL hostname. |
217
|
|
|
* |
218
|
|
|
* @param string $host |
219
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
220
|
|
|
*/ |
221
|
|
|
public function useHost($host) |
222
|
|
|
{ |
223
|
|
|
$this->host = $host; |
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set the postgreSQL port. |
229
|
|
|
* |
230
|
|
|
* @param int $port |
231
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
232
|
|
|
*/ |
233
|
|
|
public function usePort($port) |
234
|
|
|
{ |
235
|
|
|
$this->port = $port; |
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Set database to dump. |
241
|
|
|
* |
242
|
|
|
* @param string $database |
243
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
244
|
|
|
*/ |
245
|
|
|
public function dumpDatabase($database) |
246
|
|
|
{ |
247
|
|
|
$this->databaseToDump = $database; |
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Add drop statements to the dump file. |
253
|
|
|
* Works only on format=plain-text. |
254
|
|
|
* |
255
|
|
|
* @param boolean $bool |
256
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
257
|
|
|
*/ |
258
|
|
|
public function addDropStatements($bool) |
259
|
|
|
{ |
260
|
|
|
$this->clean = $bool; |
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Add the --no-owner option so no ownership setting commands will be added. |
266
|
|
|
* |
267
|
|
|
* @param boolean $bool |
268
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
269
|
|
|
*/ |
270
|
|
|
public function skipOwnerCommands($bool) |
271
|
|
|
{ |
272
|
|
|
$this->noOwner = $bool; |
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Set schemas to dump. |
278
|
|
|
* |
279
|
|
|
* @param array $schemas |
280
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
281
|
|
|
*/ |
282
|
|
|
public function dumpSchemas(array $schemas) |
283
|
|
|
{ |
284
|
|
|
$this->schemasToDump = $schemas; |
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Set schemas to exclude. |
290
|
|
|
* |
291
|
|
|
* @param array $schemas |
292
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
293
|
|
|
*/ |
294
|
|
|
public function excludeSchemas(array $schemas) |
295
|
|
|
{ |
296
|
|
|
$this->schemasToExclude = $schemas; |
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set tables to dump. |
302
|
|
|
* |
303
|
|
|
* @param array $tables |
304
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
305
|
|
|
*/ |
306
|
|
|
public function dumpTables(array $tables) |
307
|
|
|
{ |
308
|
|
|
$this->tablesToDump = $tables; |
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set tables to ignore. |
314
|
|
|
* |
315
|
|
|
* @param array $tables |
316
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
317
|
|
|
*/ |
318
|
|
|
public function excludeTables(array $tables) |
319
|
|
|
{ |
320
|
|
|
$this->tablesToExclude = $tables; |
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Set tables where no data is dumped. |
326
|
|
|
* |
327
|
|
|
* @param array $tables |
328
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
329
|
|
|
*/ |
330
|
|
|
public function excludeTableData(array $tables) |
331
|
|
|
{ |
332
|
|
|
$this->excludeTableData = $tables; |
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Dump only the schema information. |
338
|
|
|
* |
339
|
|
|
* @param boolean $bool |
340
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
341
|
|
|
* @throws \phpbu\App\Exception |
342
|
|
|
*/ |
343
|
|
|
public function dumpSchemaOnly($bool) |
344
|
|
|
{ |
345
|
|
|
if ($this->dataOnly) { |
346
|
|
|
throw new Exception('can\'t use schema-only when data-only is used already'); |
347
|
|
|
} |
348
|
|
|
$this->schemaOnly = $bool; |
349
|
|
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Dump no schema information. |
354
|
|
|
* |
355
|
|
|
* @param boolean $bool |
356
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
357
|
|
|
* @throws \phpbu\App\Exception |
358
|
|
|
*/ |
359
|
|
|
public function dumpDataOnly($bool) |
360
|
|
|
{ |
361
|
|
|
if ($this->schemaOnly) { |
362
|
|
|
throw new Exception('can\'t use data-only when schema-only is used already'); |
363
|
|
|
} |
364
|
|
|
$this->dataOnly = $bool; |
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Set the dump target path. |
370
|
|
|
* |
371
|
|
|
* @param string $path |
372
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
373
|
|
|
*/ |
374
|
|
|
public function dumpTo($path) |
375
|
|
|
{ |
376
|
|
|
$this->file = $path; |
377
|
|
|
return $this; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Set the dump format. |
382
|
|
|
* |
383
|
|
|
* @param string $format |
384
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
385
|
|
|
* @throws \phpbu\App\Exception |
386
|
|
|
*/ |
387
|
|
|
public function dumpFormat($format) |
388
|
|
|
{ |
389
|
|
|
if (!isset($this->availableFormats[$format])) { |
390
|
|
|
throw new Exception('invalid format'); |
391
|
|
|
} |
392
|
|
|
$this->format = $format; |
393
|
|
|
return $this; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Do not dump commands setting ownership. |
398
|
|
|
* --no-owner |
399
|
|
|
* |
400
|
|
|
* @param $bool |
401
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
402
|
|
|
*/ |
403
|
|
|
public function dumpNoOwner($bool) |
404
|
|
|
{ |
405
|
|
|
$this->noOwner = $bool; |
406
|
|
|
return $this; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Do not output commands to select tablespaces. |
411
|
|
|
* --no-tablespaces |
412
|
|
|
* |
413
|
|
|
* @param boolean $bool |
414
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
415
|
|
|
*/ |
416
|
|
|
public function dumpNoTablespaces($bool) |
417
|
|
|
{ |
418
|
|
|
$this->noTablespaces = $bool; |
419
|
|
|
return $this; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Prevent dumping of access privileges. |
424
|
|
|
* --no-acl |
425
|
|
|
* |
426
|
|
|
* @param boolean $bool |
427
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
428
|
|
|
*/ |
429
|
|
|
public function dumpNoPrivileges($bool) |
430
|
|
|
{ |
431
|
|
|
$this->noPrivileges = $bool; |
432
|
|
|
return $this; |
433
|
|
|
} |
434
|
|
|
/** |
435
|
|
|
* Set the output file encoding. |
436
|
|
|
* |
437
|
|
|
* @param string $encoding |
438
|
|
|
* @return \phpbu\App\Cli\Executable\Pgdump |
439
|
|
|
*/ |
440
|
|
|
public function encode($encoding) |
441
|
|
|
{ |
442
|
|
|
$this->encoding = $encoding; |
|
|
|
|
443
|
|
|
return $this; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Process generator. |
448
|
|
|
* |
449
|
|
|
* @return \phpbu\App\Cli\Process |
450
|
|
|
*/ |
451
|
|
|
protected function createProcess() |
452
|
|
|
{ |
453
|
|
|
$process = new Process(); |
454
|
|
|
$password = $this->password ? 'PGPASSWORD=' . escapeshellarg($this->password) . ' ' : ''; |
455
|
|
|
$cmd = new Cmd($password . $this->binary); |
456
|
|
|
$process->addCommand($cmd); |
457
|
|
|
|
458
|
|
|
// always disable password prompt |
459
|
|
|
$cmd->addOption('-w'); |
460
|
|
|
|
461
|
|
|
$cmd->addOptionIfNotEmpty('--username', $this->user); |
462
|
|
|
$cmd->addOptionIfNotEmpty('--host', $this->host); |
463
|
|
|
$cmd->addOptionIfNotEmpty('--port', $this->port); |
464
|
|
|
$cmd->addOptionIfNotEmpty('--dbname', $this->databaseToDump); |
465
|
|
|
$cmd->addOptionIfNotEmpty('--schema-only', $this->schemaOnly, false); |
466
|
|
|
$cmd->addOptionIfNotEmpty('--data-only', $this->dataOnly, false); |
467
|
|
|
$cmd->addOptionIfNotEmpty('--clean', $this->clean, false); |
468
|
|
|
$cmd->addOptionIfNotEmpty('--no-owner', $this->noOwner, false); |
469
|
|
|
$cmd->addOptionIfNotEmpty('--encoding', $this->encoding); |
470
|
|
|
$cmd->addOptionIfNotEmpty('--no-tablespaces', $this->noTablespaces, false); |
471
|
|
|
$cmd->addOptionIfNotEmpty('--no-acl', $this->noPrivileges, false); |
472
|
|
|
|
473
|
|
|
foreach ($this->schemasToDump as $schema) { |
474
|
|
|
$cmd->addOption('--schema', $schema); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
foreach ($this->schemasToExclude as $table) { |
478
|
|
|
$cmd->addOption('--exclude-schema', $table); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
foreach ($this->tablesToDump as $table) { |
482
|
|
|
$cmd->addOption('--table', $table); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
foreach ($this->tablesToExclude as $table) { |
486
|
|
|
$cmd->addOption('--exclude-table', $table); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
foreach ($this->excludeTableData as $table) { |
490
|
|
|
$cmd->addOption('--exclude-table-data', $table); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
$cmd->addOptionIfNotEmpty('--file', $this->file); |
494
|
|
|
$cmd->addOptionIfNotEmpty('--format', $this->format); |
495
|
|
|
return $process; |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.