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
|
|
|
* Setup. |
109
|
|
|
* |
110
|
|
|
* @see \phpbu\App\Backup\Source |
111
|
|
|
* @param array $conf |
112
|
|
|
* @throws \phpbu\App\Exception |
113
|
|
|
*/ |
114
|
17 |
|
public function setup(array $conf = []) |
115
|
|
|
{ |
116
|
17 |
|
$this->setupPath($conf); |
117
|
16 |
|
$this->pathToTar = Util\Arr::getValue($conf, 'pathToTar', ''); |
118
|
16 |
|
$this->excludes = Util\Str::toList(Util\Arr::getValue($conf, 'exclude', '')); |
119
|
16 |
|
$this->compressProgram = Util\Arr::getValue($conf, 'compressProgram', ''); |
120
|
16 |
|
$this->throttle = Util\Arr::getValue($conf, 'throttle', ''); |
121
|
16 |
|
$this->forceLocal = Util\Str::toBoolean(Util\Arr::getValue($conf, 'forceLocal', ''), false); |
122
|
16 |
|
$this->ignoreFailedRead = Util\Str::toBoolean(Util\Arr::getValue($conf, 'ignoreFailedRead', ''), false); |
123
|
16 |
|
$this->removeSourceDir = Util\Str::toBoolean(Util\Arr::getValue($conf, 'removeSourceDir', ''), false); |
124
|
16 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Setup the path to the directory that should be compressed. |
128
|
|
|
* |
129
|
|
|
* @param array $conf |
130
|
|
|
* @throws \phpbu\App\Exception |
131
|
|
|
*/ |
132
|
17 |
|
protected function setupPath(array $conf) |
133
|
|
|
{ |
134
|
17 |
|
$path = Util\Arr::getValue($conf, 'path', ''); |
135
|
17 |
|
if (empty($path)) { |
136
|
1 |
|
throw new Exception('path option is mandatory'); |
137
|
|
|
} |
138
|
16 |
|
$this->path = Util\Path::toAbsolutePath($path, Configuration::getWorkingDirectory()); |
139
|
16 |
|
if (!file_exists($this->path)) { |
140
|
|
|
throw new Exception('could not find directory to compress'); |
141
|
|
|
} |
142
|
16 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Execute the backup. |
146
|
|
|
* |
147
|
|
|
* @see \phpbu\App\Backup\Source |
148
|
|
|
* @param \phpbu\App\Backup\Target $target |
149
|
|
|
* @param \phpbu\App\Result $result |
150
|
|
|
* @return \phpbu\App\Backup\Source\Status |
151
|
|
|
* @throws \phpbu\App\Exception |
152
|
|
|
*/ |
153
|
7 |
View Code Duplication |
public function backup(Target $target, Result $result) : Status |
154
|
|
|
{ |
155
|
|
|
// make sure source path is a directory |
156
|
7 |
|
$this->validatePath(); |
157
|
|
|
// set uncompressed default MIME type |
158
|
6 |
|
$target->setMimeType('application/x-tar'); |
159
|
6 |
|
$tar = $this->execute($target); |
160
|
|
|
|
161
|
5 |
|
$result->debug($tar->getCmdPrintable()); |
162
|
|
|
|
163
|
5 |
|
if (!$tar->isSuccessful()) { |
164
|
2 |
|
throw new Exception('tar failed: ' . $tar->getStdErr()); |
165
|
|
|
} |
166
|
|
|
|
167
|
3 |
|
return $this->createStatus($target); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Setup the Executable to run the 'tar' command. |
172
|
|
|
* |
173
|
|
|
* @param \phpbu\App\Backup\Target |
174
|
|
|
* @return \phpbu\App\Cli\Executable |
175
|
|
|
*/ |
176
|
15 |
|
protected function createExecutable(Target $target) : Executable |
177
|
|
|
{ |
178
|
15 |
|
$this->pathToArchive = $target->getPathnamePlain(); |
179
|
|
|
|
180
|
|
|
// check if archive should be compressed and tar supports requested compression |
181
|
15 |
|
if ($target->shouldBeCompressed() |
182
|
15 |
|
&& Executable\Tar::isCompressionValid($target->getCompression()->getCommand())) { |
183
|
5 |
|
$this->pathToArchive = $target->getPathname(); |
184
|
5 |
|
$this->compression = $target->getCompression()->getCommand(); |
185
|
|
|
} |
186
|
|
|
|
187
|
15 |
|
$executable = new Executable\Tar($this->pathToTar); |
188
|
15 |
|
$executable->archiveDirectory($this->path) |
189
|
15 |
|
->useCompression($this->compression) |
190
|
15 |
|
->useCompressProgram($this->compressProgram) |
191
|
15 |
|
->forceLocal($this->forceLocal) |
192
|
15 |
|
->ignoreFailedRead($this->ignoreFailedRead) |
193
|
15 |
|
->removeSourceDirectory($this->removeSourceDir) |
194
|
15 |
|
->throttle($this->throttle) |
195
|
15 |
|
->archiveTo($this->pathToArchive); |
196
|
|
|
// add paths to exclude |
197
|
15 |
|
foreach ($this->excludes as $path) { |
198
|
1 |
|
$executable->addExclude($path); |
199
|
|
|
} |
200
|
|
|
|
201
|
15 |
|
return $executable; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Check the source to compress. |
206
|
|
|
* |
207
|
|
|
* @throws \phpbu\App\Exception |
208
|
|
|
*/ |
209
|
7 |
|
private function validatePath() |
210
|
|
|
{ |
211
|
7 |
|
if (!is_dir($this->path)) { |
212
|
1 |
|
throw new Exception('path to compress has to be a directory'); |
213
|
|
|
} |
214
|
6 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Create backup status. |
218
|
|
|
* |
219
|
|
|
* @param \phpbu\App\Backup\Target |
220
|
|
|
* @return \phpbu\App\Backup\Source\Status |
221
|
|
|
*/ |
222
|
3 |
|
protected function createStatus(Target $target) : Status |
223
|
|
|
{ |
224
|
3 |
|
$status = Status::create(); |
225
|
|
|
// if tar doesn't handle the compression mark status uncompressed |
226
|
|
|
// so the app can take care of compression |
227
|
3 |
|
if (!$this->executable->handlesCompression()) { |
228
|
1 |
|
$status->uncompressedFile($target->getPathnamePlain()); |
229
|
|
|
} |
230
|
3 |
|
return $status; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|