|
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
|
|
|
* Ignore failed reads |
|
61
|
|
|
* --ignore-failed-read |
|
62
|
|
|
* |
|
63
|
|
|
* @var bool |
|
64
|
|
|
*/ |
|
65
|
13 |
|
private $ignoreFailedRead; |
|
66
|
|
|
|
|
67
|
13 |
|
/** |
|
68
|
13 |
|
* Should the source directory be removed. |
|
69
|
13 |
|
* |
|
70
|
|
|
* @var boolean |
|
71
|
|
|
*/ |
|
72
|
|
|
private $removeSourceDir = false; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* List of available compressors |
|
76
|
|
|
* |
|
77
|
6 |
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
6 |
|
private static $availableCompressions = [ |
|
80
|
|
|
'bzip2' => 'j', |
|
81
|
|
|
'gzip' => 'z', |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Constructor. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $path |
|
88
|
8 |
|
*/ |
|
89
|
|
|
public function __construct(string $path = '') |
|
90
|
8 |
|
{ |
|
91
|
6 |
|
$this->setup('tar', $path); |
|
92
|
6 |
|
} |
|
93
|
8 |
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Return 'tar' compressor option e.g. 'j' for bzip2. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $compressor |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getCompressionOption(string $compressor) : string |
|
101
|
1 |
|
{ |
|
102
|
|
|
return $this->isCompressionValid($compressor) ? self::$availableCompressions[$compressor] : ''; |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Compress tar. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $compression |
|
109
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
110
|
|
|
*/ |
|
111
|
|
|
public function useCompression(string $compression) : Tar |
|
112
|
|
|
{ |
|
113
|
11 |
|
if ($this->isCompressionValid($compression)) { |
|
114
|
|
|
$this->compression = $this->getCompressionOption($compression); |
|
115
|
11 |
|
} |
|
116
|
1 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
10 |
|
|
|
119
|
10 |
|
/** |
|
120
|
|
|
* Set compress program. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $program |
|
123
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
124
|
|
|
*/ |
|
125
|
|
|
public function useCompressProgram(string $program) : Tar |
|
126
|
|
|
{ |
|
127
|
|
|
$this->compressProgram = $program; |
|
128
|
9 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
9 |
|
|
|
131
|
9 |
|
/** |
|
132
|
|
|
* Add an path to exclude. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $path |
|
135
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
136
|
|
|
*/ |
|
137
|
|
|
public function addExclude(string $path) : Tar |
|
138
|
|
|
{ |
|
139
|
|
|
$this->excludes[] = $path; |
|
140
|
2 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
2 |
|
|
|
143
|
2 |
|
/** |
|
144
|
|
|
* Ignore failed reads setter. |
|
145
|
|
|
* |
|
146
|
|
|
* @param bool $bool |
|
147
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
148
|
|
|
*/ |
|
149
|
11 |
|
public function ignoreFailedRead(bool $bool) : Tar |
|
150
|
|
|
{ |
|
151
|
|
|
$this->ignoreFailedRead = $bool; |
|
152
|
11 |
|
return $this; |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
10 |
|
/** |
|
156
|
1 |
|
* Does the tar handle the compression. |
|
157
|
|
|
* |
|
158
|
|
|
* @return bool |
|
159
|
9 |
|
*/ |
|
160
|
9 |
|
public function handlesCompression() : bool |
|
161
|
|
|
{ |
|
162
|
|
|
return !empty($this->compression); |
|
163
|
9 |
|
} |
|
164
|
8 |
|
|
|
165
|
|
|
/** |
|
166
|
8 |
|
* Set folder to compress. |
|
167
|
|
|
* |
|
168
|
9 |
|
* @param string $path |
|
169
|
9 |
|
* @return \phpbu\App\Cli\Executable\Tar |
|
170
|
9 |
|
* @throws \phpbu\App\Exception |
|
171
|
9 |
|
*/ |
|
172
|
|
|
public function archiveDirectory(string $path) : Tar |
|
173
|
9 |
|
{ |
|
174
|
|
|
$this->validateDirectory($path); |
|
175
|
|
|
$this->path = $path; |
|
176
|
9 |
|
return $this; |
|
177
|
2 |
|
} |
|
178
|
2 |
|
|
|
179
|
|
|
/** |
|
180
|
9 |
|
* Set target filename. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $path |
|
183
|
|
|
* @return \phpbu\App\Cli\Executable\Tar |
|
184
|
|
|
*/ |
|
185
|
|
|
public function archiveTo(string $path) : Tar |
|
186
|
|
|
{ |
|
187
|
|
|
$this->tarPathname = $path; |
|
188
|
2 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
2 |
|
|
|
191
|
2 |
|
/** |
|
192
|
2 |
|
* Delete the source directory. |
|
193
|
2 |
|
* |
|
194
|
2 |
|
* @param boolean $bool |
|
195
|
2 |
|
* @return \phpbu\App\Cli\Executable\Tar |
|
196
|
|
|
*/ |
|
197
|
|
|
public function removeSourceDirectory(bool $bool) : Tar |
|
198
|
|
|
{ |
|
199
|
|
|
$this->removeSourceDir = $bool; |
|
200
|
|
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
9 |
|
* Tar CommandLine generator. |
|
205
|
|
|
* |
|
206
|
9 |
|
* @return \SebastianFeldmann\Cli\CommandLine |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function createCommandLine() : CommandLine |
|
209
|
|
|
{ |
|
210
|
|
|
$this->validateSetup(); |
|
211
|
|
|
|
|
212
|
|
|
$process = new CommandLine(); |
|
213
|
|
|
$tar = new Cmd($this->binary); |
|
214
|
|
|
|
|
215
|
|
|
$this->setExcludeOptions($tar); |
|
216
|
|
|
|
|
217
|
|
|
$tar->addOptionIfNotEmpty('--ignore-failed-read', $this->ignoreFailedRead, false); |
|
218
|
|
|
$tar->addOptionIfNotEmpty('--use-compress-program', $this->compressProgram); |
|
219
|
|
|
$tar->addOption('-' . (empty($this->compressProgram) ? $this->compression : '') . 'cf'); |
|
220
|
|
|
$tar->addArgument($this->tarPathname); |
|
221
|
|
|
$tar->addOption('-C', dirname($this->path), ' '); |
|
222
|
|
|
$tar->addArgument(basename($this->path)); |
|
223
|
|
|
|
|
224
|
|
|
$process->addCommand($tar); |
|
225
|
|
|
|
|
226
|
|
|
// delete the source data if requested |
|
227
|
|
|
$this->addRemoveCommand($process); |
|
228
|
|
|
|
|
229
|
|
|
return $process; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Adds necessary exclude options to tat command. |
|
234
|
|
|
* |
|
235
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $tar |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function setExcludeOptions(Cmd $tar) |
|
238
|
|
|
{ |
|
239
|
|
|
foreach ($this->excludes as $path) { |
|
240
|
|
|
$tar->addOption('--exclude', $path); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Add a remove command if requested. |
|
246
|
|
|
* |
|
247
|
|
|
* @param \SebastianFeldmann\Cli\CommandLine $process |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function addRemoveCommand(CommandLine $process) |
|
250
|
|
|
{ |
|
251
|
|
|
if ($this->removeSourceDir) { |
|
252
|
|
|
$process->addCommand($this->getRmCommand()); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Return 'rm' command. |
|
258
|
|
|
* |
|
259
|
|
|
* @return \SebastianFeldmann\Cli\Command\Executable |
|
260
|
|
|
*/ |
|
261
|
|
|
protected function getRmCommand() : Cmd |
|
262
|
|
|
{ |
|
263
|
|
|
$rm = new Cmd('rm'); |
|
264
|
|
|
$rm->addOption('-rf', $this->path, ' '); |
|
265
|
|
|
return $rm; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Check directory to compress. |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $path |
|
272
|
|
|
* @throws \phpbu\App\Exception |
|
273
|
|
|
*/ |
|
274
|
|
|
private function validateDirectory(string $path) |
|
275
|
|
|
{ |
|
276
|
|
|
if ($path === '.') { |
|
277
|
|
|
throw new Exception('unable to tar current working directory'); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Check if source and target values are set. |
|
283
|
|
|
* |
|
284
|
|
|
* @throws \phpbu\App\Exception |
|
285
|
|
|
*/ |
|
286
|
|
|
private function validateSetup() |
|
287
|
|
|
{ |
|
288
|
|
|
if (empty($this->path)) { |
|
289
|
|
|
throw new Exception('no directory to compress'); |
|
290
|
|
|
} |
|
291
|
|
|
if (empty($this->tarPathname)) { |
|
292
|
|
|
throw new Exception('no target filename set'); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Return true if a given compression is valid false otherwise. |
|
298
|
|
|
* |
|
299
|
|
|
* @param string $compression |
|
300
|
|
|
* @return bool |
|
301
|
|
|
*/ |
|
302
|
|
|
public static function isCompressionValid(string $compression) : bool |
|
303
|
|
|
{ |
|
304
|
|
|
return isset(self::$availableCompressions[$compression]); |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|