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