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