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