|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup\Source; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Target; |
|
5
|
|
|
use phpbu\App\Cli\Executable; |
|
6
|
|
|
use phpbu\App\Configuration; |
|
7
|
|
|
use phpbu\App\Exception; |
|
8
|
|
|
use phpbu\App\Result; |
|
9
|
|
|
use phpbu\App\Util; |
|
10
|
|
|
use Symfony\Component\Finder\Tests\Iterator\FilecontentFilterIteratorTest; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Tar source class. |
|
14
|
|
|
* |
|
15
|
|
|
* @package phpbu |
|
16
|
|
|
* @subpackage Backup |
|
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
20
|
|
|
* @link http://phpbu.de/ |
|
21
|
|
|
* @since Class available since Release 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class Tar extends SimulatorExecutable implements Simulator |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Tar Executable |
|
27
|
|
|
* |
|
28
|
|
|
* @var \phpbu\App\Cli\Executable\Tar |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $executable; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Path to executable. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $pathToTar; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Path to backup |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $path; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* List of paths to exclude |
|
48
|
|
|
* --exclude |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $excludes; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Special compression program |
|
56
|
|
|
* --use-compress-program |
|
57
|
|
|
* |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
private $compressProgram; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Force local file resolution |
|
64
|
|
|
* |
|
65
|
|
|
* --force-local |
|
66
|
|
|
* |
|
67
|
|
|
* @var bool |
|
68
|
|
|
*/ |
|
69
|
|
|
private $forceLocal; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Tar should ignore failed reads |
|
73
|
|
|
* --ignore-failed-read |
|
74
|
|
|
* |
|
75
|
|
|
* @var bool |
|
76
|
|
|
*/ |
|
77
|
|
|
private $ignoreFailedRead; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Remove the packed data |
|
81
|
|
|
* |
|
82
|
|
|
* @var bool |
|
83
|
|
|
*/ |
|
84
|
|
|
private $removeSourceDir; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Compression to use. |
|
88
|
|
|
* |
|
89
|
|
|
* @var string |
|
90
|
|
|
*/ |
|
91
|
|
|
private $compression = ''; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Throttle cpu usage. |
|
95
|
|
|
* |
|
96
|
|
|
* @var string |
|
97
|
|
|
*/ |
|
98
|
|
|
private $throttle = ''; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Path where to store the archive. |
|
102
|
|
|
* |
|
103
|
|
|
* @var string |
|
104
|
|
|
*/ |
|
105
|
|
|
private $pathToArchive; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Instead of archiving symbolic links, archive the files they link to |
|
109
|
|
|
* |
|
110
|
|
|
* @var bool |
|
111
|
|
|
*/ |
|
112
|
|
|
private $dereference; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Setup. |
|
116
|
|
|
* |
|
117
|
|
|
* @see \phpbu\App\Backup\Source |
|
118
|
|
|
* @param array $conf |
|
119
|
|
|
* @throws \phpbu\App\Exception |
|
120
|
|
|
*/ |
|
121
|
18 |
|
public function setup(array $conf = []) |
|
122
|
|
|
{ |
|
123
|
18 |
|
$this->setupPath($conf); |
|
124
|
17 |
|
$this->pathToTar = Util\Arr::getValue($conf, 'pathToTar', ''); |
|
125
|
17 |
|
$this->excludes = Util\Str::toList(Util\Arr::getValue($conf, 'exclude', '')); |
|
126
|
17 |
|
$this->compressProgram = Util\Arr::getValue($conf, 'compressProgram', ''); |
|
127
|
17 |
|
$this->throttle = Util\Arr::getValue($conf, 'throttle', ''); |
|
128
|
17 |
|
$this->forceLocal = Util\Str::toBoolean(Util\Arr::getValue($conf, 'forceLocal', ''), false); |
|
129
|
17 |
|
$this->ignoreFailedRead = Util\Str::toBoolean(Util\Arr::getValue($conf, 'ignoreFailedRead', ''), false); |
|
130
|
17 |
|
$this->removeSourceDir = Util\Str::toBoolean(Util\Arr::getValue($conf, 'removeSourceDir', ''), false); |
|
131
|
17 |
|
$this->dereference = Util\Str::toBoolean(Util\Arr::getValue($conf, 'dereference', ''), false); |
|
132
|
17 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Setup the path to the directory that should be compressed. |
|
136
|
|
|
* |
|
137
|
|
|
* @param array $conf |
|
138
|
|
|
* @throws \phpbu\App\Exception |
|
139
|
|
|
*/ |
|
140
|
18 |
|
protected function setupPath(array $conf) |
|
141
|
|
|
{ |
|
142
|
18 |
|
$path = Util\Arr::getValue($conf, 'path', ''); |
|
143
|
18 |
|
if (empty($path)) { |
|
144
|
1 |
|
throw new Exception('path option is mandatory'); |
|
145
|
|
|
} |
|
146
|
17 |
|
$this->path = Util\Path::toAbsolutePath($path, Configuration::getWorkingDirectory()); |
|
147
|
17 |
|
if (!file_exists($this->path)) { |
|
148
|
|
|
throw new Exception('could not find directory to compress'); |
|
149
|
|
|
} |
|
150
|
17 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Execute the backup. |
|
154
|
|
|
* |
|
155
|
|
|
* @see \phpbu\App\Backup\Source |
|
156
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
157
|
|
|
* @param \phpbu\App\Result $result |
|
158
|
|
|
* @return \phpbu\App\Backup\Source\Status |
|
159
|
|
|
* @throws \phpbu\App\Exception |
|
160
|
|
|
*/ |
|
161
|
7 |
View Code Duplication |
public function backup(Target $target, Result $result) : Status |
|
162
|
|
|
{ |
|
163
|
|
|
// make sure source path is a directory |
|
164
|
7 |
|
$this->validatePath(); |
|
165
|
|
|
// set uncompressed default MIME type |
|
166
|
6 |
|
$target->setMimeType('application/x-tar'); |
|
167
|
6 |
|
$tar = $this->execute($target); |
|
168
|
|
|
|
|
169
|
5 |
|
$result->debug($tar->getCmdPrintable()); |
|
170
|
|
|
|
|
171
|
5 |
|
if (!$tar->isSuccessful()) { |
|
172
|
2 |
|
throw new Exception('tar failed: ' . $tar->getStdErr()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
3 |
|
return $this->createStatus($target); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Setup the Executable to run the 'tar' command. |
|
180
|
|
|
* |
|
181
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
182
|
|
|
* @return \phpbu\App\Cli\Executable |
|
183
|
|
|
*/ |
|
184
|
16 |
|
protected function createExecutable(Target $target) : Executable |
|
185
|
|
|
{ |
|
186
|
16 |
|
$this->pathToArchive = $target->getPathnamePlain(); |
|
187
|
|
|
|
|
188
|
|
|
// check if archive should be compressed and tar supports requested compression |
|
189
|
16 |
|
if ($target->shouldBeCompressed() |
|
190
|
16 |
|
&& Executable\Tar::isCompressionValid($target->getCompression()->getCommand())) { |
|
191
|
5 |
|
$this->pathToArchive = $target->getPathname(); |
|
192
|
5 |
|
$this->compression = $target->getCompression()->getCommand(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
16 |
|
$executable = new Executable\Tar($this->pathToTar); |
|
196
|
16 |
|
$executable->archiveDirectory($this->path) |
|
197
|
16 |
|
->useCompression($this->compression) |
|
198
|
16 |
|
->useCompressProgram($this->compressProgram) |
|
199
|
16 |
|
->forceLocal($this->forceLocal) |
|
200
|
16 |
|
->ignoreFailedRead($this->ignoreFailedRead) |
|
201
|
16 |
|
->removeSourceDirectory($this->removeSourceDir) |
|
202
|
16 |
|
->throttle($this->throttle) |
|
203
|
16 |
|
->archiveTo($this->pathToArchive) |
|
204
|
16 |
|
->dereference($this->dereference); |
|
205
|
|
|
// add paths to exclude |
|
206
|
16 |
|
foreach ($this->excludes as $path) { |
|
207
|
1 |
|
$executable->addExclude($path); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
16 |
|
return $executable; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Check the source to compress. |
|
215
|
|
|
* |
|
216
|
|
|
* @throws \phpbu\App\Exception |
|
217
|
|
|
*/ |
|
218
|
7 |
|
private function validatePath() |
|
219
|
|
|
{ |
|
220
|
7 |
|
if (!is_dir($this->path)) { |
|
221
|
1 |
|
throw new Exception('path to compress has to be a directory'); |
|
222
|
|
|
} |
|
223
|
6 |
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Create backup status. |
|
227
|
|
|
* |
|
228
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
229
|
|
|
* @return \phpbu\App\Backup\Source\Status |
|
230
|
|
|
*/ |
|
231
|
3 |
|
protected function createStatus(Target $target) : Status |
|
232
|
|
|
{ |
|
233
|
3 |
|
$status = Status::create(); |
|
234
|
|
|
// if tar doesn't handle the compression mark status uncompressed |
|
235
|
|
|
// so the app can take care of compression |
|
236
|
3 |
|
if (!$this->executable->handlesCompression()) { |
|
237
|
1 |
|
$status->uncompressedFile($target->getPathnamePlain()); |
|
238
|
|
|
} |
|
239
|
3 |
|
return $status; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|