|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Cli\Executable; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Executable; |
|
5
|
|
|
use phpbu\App\Exception; |
|
6
|
|
|
use SebastianFeldmann\Cli\CommandLine; |
|
7
|
|
|
use SebastianFeldmann\Cli\Command\Executable as Cmd; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Tar 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 Tar extends Abstraction implements Executable |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Path to compress |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $path; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Compression to use |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $compression; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Compress program to use. |
|
38
|
|
|
* --use-compress-program |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $compressProgram; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Path to dump file |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $tarPathname; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* List of excluded path. |
|
53
|
|
|
* --exclude='foo' |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
private $excludes = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Force local file resolution |
|
61
|
|
|
* --force-local |
|
62
|
|
|
* |
|
63
|
|
|
* @var bool |
|
64
|
|
|
*/ |
|
65
|
|
|
private $local = false; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Ignore failed reads |
|
69
|
|
|
* --ignore-failed-read |
|
70
|
|
|
* |
|
71
|
|
|
* @var bool |
|
72
|
|
|
*/ |
|
73
|
|
|
private $ignoreFailedRead; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Should the source directory be removed. |
|
77
|
|
|
* |
|
78
|
|
|
* @var boolean |
|
79
|
|
|
*/ |
|
80
|
|
|
private $removeSourceDir = false; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Limit data throughput |
|
84
|
|
|
* | pv -L ${limit} |
|
85
|
|
|
* |
|
86
|
|
|
* @var string |
|
87
|
|
|
*/ |
|
88
|
|
|
private $pvLimit = ''; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* List of available compressors |
|
92
|
|
|
* |
|
93
|
|
|
* @var array |
|
94
|
|
|
*/ |
|
95
|
|
|
private static $availableCompressions = [ |
|
96
|
|
|
'bzip2' => 'j', |
|
97
|
|
|
'gzip' => 'z', |
|
98
|
|
|
'xz' => 'J' |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Instead of archiving symbolic links, archive the files they link to |
|
103
|
|
|
* |
|
104
|
|
|
* @var bool |
|
105
|
|
|
*/ |
|
106
|
34 |
|
private $dereference = false; |
|
107
|
|
|
|
|
108
|
34 |
|
/** |
|
109
|
34 |
|
* Constructor. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $path |
|
112
|
|
|
*/ |
|
113
|
|
|
public function __construct(string $path = '') |
|
114
|
|
|
{ |
|
115
|
|
|
$this->setup('tar', $path); |
|
116
|
|
|
} |
|
117
|
13 |
|
|
|
118
|
|
|
/** |
|
119
|
13 |
|
* Return 'tar' compressor option e.g. 'j' for bzip2. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $compressor |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function getCompressionOption(string $compressor) : string |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->isCompressionValid($compressor) ? self::$availableCompressions[$compressor] : ''; |
|
127
|
|
|
} |
|
128
|
24 |
|
|
|
129
|
|
|
/** |
|
130
|
24 |
|
* Compress tar. |
|
131
|
13 |
|
* |
|
132
|
|
|
* @param string $compression |
|
133
|
24 |
|
* @return \phpbu\App\Cli\Executable\Tar |
|
134
|
|
|
*/ |
|
135
|
|
|
public function useCompression(string $compression) : Tar |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->isCompressionValid($compression)) { |
|
138
|
|
|
$this->compression = $this->getCompressionOption($compression); |
|
139
|
|
|
} |
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
16 |
|
|
|
143
|
|
|
/** |
|
144
|
16 |
|
* Set compress program. |
|
145
|
16 |
|
* |
|
146
|
|
|
* @param string $program |
|
147
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
148
|
|
|
*/ |
|
149
|
|
|
public function useCompressProgram(string $program) : Tar |
|
150
|
|
|
{ |
|
151
|
|
|
$this->compressProgram = $program; |
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
2 |
|
|
|
155
|
|
|
/** |
|
156
|
2 |
|
* Add an path to exclude. |
|
157
|
2 |
|
* |
|
158
|
|
|
* @param string $path |
|
159
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
160
|
|
|
*/ |
|
161
|
|
|
public function addExclude(string $path) : Tar |
|
162
|
|
|
{ |
|
163
|
|
|
$this->excludes[] = $path; |
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
16 |
|
|
|
167
|
|
|
/** |
|
168
|
16 |
|
* Force local file resolution. |
|
169
|
16 |
|
* |
|
170
|
|
|
* @param bool $bool |
|
171
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
172
|
|
|
*/ |
|
173
|
|
|
public function forceLocal(bool $bool) : Tar |
|
174
|
|
|
{ |
|
175
|
|
|
$this->local = $bool; |
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
178
|
16 |
|
|
|
179
|
|
|
/** |
|
180
|
16 |
|
* Ignore failed reads setter. |
|
181
|
16 |
|
* |
|
182
|
|
|
* @param bool $bool |
|
183
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
184
|
|
|
*/ |
|
185
|
|
|
public function ignoreFailedRead(bool $bool) : Tar |
|
186
|
|
|
{ |
|
187
|
|
|
$this->ignoreFailedRead = $bool; |
|
188
|
|
|
return $this; |
|
189
|
|
|
} |
|
190
|
17 |
|
|
|
191
|
|
|
/** |
|
192
|
17 |
|
* Limit the data throughput. |
|
193
|
17 |
|
* |
|
194
|
|
|
* @param string $limit |
|
195
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
196
|
|
|
*/ |
|
197
|
|
|
public function throttle(string $limit) : Tar |
|
198
|
|
|
{ |
|
199
|
|
|
$this->pvLimit = $limit; |
|
200
|
|
|
return $this; |
|
201
|
4 |
|
} |
|
202
|
|
|
|
|
203
|
4 |
|
/** |
|
204
|
|
|
* Does the tar handle the compression. |
|
205
|
|
|
* |
|
206
|
|
|
* @return bool |
|
207
|
|
|
*/ |
|
208
|
|
|
public function handlesCompression() : bool |
|
209
|
|
|
{ |
|
210
|
|
|
return !empty($this->compression); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
32 |
|
/** |
|
214
|
|
|
* Set folder to compress. |
|
215
|
32 |
|
* |
|
216
|
31 |
|
* @param string $path |
|
217
|
31 |
|
* @return \phpbu\App\Cli\Executable\Tar |
|
218
|
|
|
* @throws \phpbu\App\Exception |
|
219
|
|
|
*/ |
|
220
|
|
|
public function archiveDirectory(string $path) : Tar |
|
221
|
|
|
{ |
|
222
|
|
|
$this->validateDirectory($path); |
|
223
|
|
|
$this->path = $path; |
|
224
|
|
|
return $this; |
|
225
|
|
|
} |
|
226
|
30 |
|
|
|
227
|
|
|
/** |
|
228
|
30 |
|
* Set target filename. |
|
229
|
30 |
|
* |
|
230
|
|
|
* @param string $path |
|
231
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
232
|
|
|
*/ |
|
233
|
|
|
public function archiveTo(string $path) : Tar |
|
234
|
|
|
{ |
|
235
|
|
|
$this->tarPathname = $path; |
|
236
|
|
|
return $this; |
|
237
|
|
|
} |
|
238
|
21 |
|
|
|
239
|
|
|
/** |
|
240
|
21 |
|
* Delete the source directory. |
|
241
|
21 |
|
* |
|
242
|
|
|
* @param boolean $bool |
|
243
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
244
|
|
|
*/ |
|
245
|
|
|
public function removeSourceDirectory(bool $bool) : Tar |
|
246
|
|
|
{ |
|
247
|
|
|
$this->removeSourceDir = $bool; |
|
248
|
|
|
return $this; |
|
249
|
|
|
} |
|
250
|
32 |
|
|
|
251
|
|
|
/** |
|
252
|
32 |
|
* Instead of archiving symbolic links, archive the files they link |
|
253
|
|
|
* |
|
254
|
30 |
|
* @param bool $bool |
|
255
|
30 |
|
* @return \phpbu\App\Cli\Executable\Tar |
|
256
|
30 |
|
*/ |
|
257
|
30 |
|
public function dereference(bool $bool) : Tar |
|
258
|
|
|
{ |
|
259
|
30 |
|
$this->dereference = $bool; |
|
260
|
30 |
|
return $this; |
|
261
|
|
|
} |
|
262
|
30 |
|
|
|
263
|
30 |
|
/** |
|
264
|
30 |
|
* Tar CommandLine generator. |
|
265
|
|
|
* |
|
266
|
30 |
|
* @return \SebastianFeldmann\Cli\CommandLine |
|
267
|
3 |
|
* @throws \phpbu\App\Exception |
|
268
|
3 |
|
*/ |
|
269
|
3 |
|
protected function createCommandLine() : CommandLine |
|
270
|
3 |
|
{ |
|
271
|
|
|
$this->validateSetup(); |
|
272
|
27 |
|
|
|
273
|
|
|
$process = new CommandLine(); |
|
274
|
|
|
$tar = new Cmd($this->binary); |
|
275
|
30 |
|
$create = $this->isThrottled() ? 'c' : 'cf'; |
|
276
|
30 |
|
$process->addCommand($tar); |
|
277
|
|
|
|
|
278
|
|
|
$this->setExcludeOptions($tar); |
|
279
|
30 |
|
$this->handleWarnings($tar); |
|
280
|
|
|
|
|
281
|
30 |
|
$tar->addOptionIfNotEmpty('-h', $this->dereference, false); |
|
282
|
|
|
$tar->addOptionIfNotEmpty('--force-local', $this->local, false); |
|
283
|
|
|
$tar->addOptionIfNotEmpty('--use-compress-program', $this->compressProgram); |
|
284
|
|
|
$tar->addOption('-' . (empty($this->compressProgram) ? $this->compression : '') . $create); |
|
285
|
|
|
|
|
286
|
|
|
if ($this->isThrottled()) { |
|
287
|
|
|
$pv = new Cmd('pv'); |
|
288
|
|
|
$pv->addOption('-qL', $this->pvLimit, ' '); |
|
289
|
30 |
|
$process->pipeOutputTo($pv); |
|
290
|
|
|
$process->redirectOutputTo($this->tarPathname); |
|
291
|
30 |
|
} else { |
|
292
|
2 |
|
$tar->addArgument($this->tarPathname); |
|
293
|
|
|
} |
|
294
|
30 |
|
|
|
295
|
|
|
$tar->addOption('-C', dirname($this->path), ' '); |
|
296
|
|
|
$tar->addArgument(basename($this->path)); |
|
297
|
|
|
|
|
298
|
|
|
// delete the source data if requested |
|
299
|
|
|
$this->addRemoveCommand($process); |
|
300
|
|
|
|
|
301
|
|
|
return $process; |
|
302
|
30 |
|
} |
|
303
|
|
|
|
|
304
|
30 |
|
/** |
|
305
|
3 |
|
* Adds necessary exclude options to tat command. |
|
306
|
3 |
|
* |
|
307
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $tar |
|
308
|
30 |
|
*/ |
|
309
|
|
|
protected function setExcludeOptions(Cmd $tar) |
|
310
|
|
|
{ |
|
311
|
|
|
foreach ($this->excludes as $path) { |
|
312
|
|
|
$tar->addOption('--exclude', $path); |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
30 |
|
|
|
316
|
|
|
/** |
|
317
|
30 |
|
* Configure warning handling. |
|
318
|
7 |
|
* With the 'ignoreFailedRead' option set, exit code '1' is also accepted since it only indicates a warning. |
|
319
|
|
|
* |
|
320
|
30 |
|
* @param \SebastianFeldmann\Cli\Command\Executable $tar |
|
321
|
|
|
*/ |
|
322
|
|
|
protected function handleWarnings(Cmd $tar) |
|
323
|
|
|
{ |
|
324
|
|
|
if ($this->ignoreFailedRead) { |
|
325
|
|
|
$tar->addOption('--ignore-failed-read'); |
|
326
|
|
|
$this->acceptableExitCodes = [0, 1]; |
|
327
|
7 |
|
} |
|
328
|
|
|
} |
|
329
|
7 |
|
|
|
330
|
7 |
|
/** |
|
331
|
7 |
|
* Add a remove command if requested. |
|
332
|
|
|
* |
|
333
|
|
|
* @param \SebastianFeldmann\Cli\CommandLine $process |
|
334
|
|
|
*/ |
|
335
|
|
|
protected function addRemoveCommand(CommandLine $process) |
|
336
|
|
|
{ |
|
337
|
|
|
if ($this->removeSourceDir) { |
|
338
|
|
|
$process->addCommand($this->getRmCommand()); |
|
339
|
|
|
} |
|
340
|
32 |
|
} |
|
341
|
|
|
|
|
342
|
32 |
|
/** |
|
343
|
1 |
|
* Return 'rm' command. |
|
344
|
|
|
* |
|
345
|
31 |
|
* @return \SebastianFeldmann\Cli\Command\Executable |
|
346
|
|
|
*/ |
|
347
|
|
|
protected function getRmCommand() : Cmd |
|
348
|
|
|
{ |
|
349
|
|
|
$rm = new Cmd('rm'); |
|
350
|
|
|
$rm->addOption('-rf', $this->path, ' '); |
|
351
|
|
|
return $rm; |
|
352
|
32 |
|
} |
|
353
|
|
|
|
|
354
|
32 |
|
/** |
|
355
|
1 |
|
* Check directory to compress. |
|
356
|
|
|
* |
|
357
|
31 |
|
* @param string $path |
|
358
|
1 |
|
* @throws \phpbu\App\Exception |
|
359
|
|
|
*/ |
|
360
|
30 |
|
private function validateDirectory(string $path) |
|
361
|
|
|
{ |
|
362
|
|
|
if ($path === '.') { |
|
363
|
|
|
throw new Exception('unable to tar current working directory'); |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
26 |
|
* Check if source and target values are set. |
|
369
|
|
|
* |
|
370
|
26 |
|
* @throws \phpbu\App\Exception |
|
371
|
|
|
*/ |
|
372
|
|
|
private function validateSetup() |
|
373
|
|
|
{ |
|
374
|
|
|
if (empty($this->path)) { |
|
375
|
|
|
throw new Exception('no directory to compress'); |
|
376
|
|
|
} |
|
377
|
|
|
if (empty($this->tarPathname)) { |
|
378
|
30 |
|
throw new Exception('no target filename set'); |
|
379
|
|
|
} |
|
380
|
30 |
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Return true if a given compression is valid false otherwise. |
|
384
|
|
|
* |
|
385
|
|
|
* @param string $compression |
|
386
|
|
|
* @return bool |
|
387
|
|
|
*/ |
|
388
|
|
|
public static function isCompressionValid(string $compression) : bool |
|
389
|
|
|
{ |
|
390
|
|
|
return isset(self::$availableCompressions[$compression]); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* Should output be throttled through pv. |
|
395
|
|
|
* |
|
396
|
|
|
* @return bool |
|
397
|
|
|
*/ |
|
398
|
|
|
public function isThrottled() : bool |
|
399
|
|
|
{ |
|
400
|
|
|
return !empty($this->pvLimit); |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
|