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