1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup; |
5
|
|
|
use phpbu\App\Backup\Collector; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Runner actually executes all backup jobs. |
10
|
|
|
* |
11
|
|
|
* @package phpbu |
12
|
|
|
* @subpackage App |
13
|
|
|
* @author Sebastian Feldmann <[email protected]> |
14
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
15
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
16
|
|
|
* @link https://phpbu.de/ |
17
|
|
|
* @since Class available since Release 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
class Runner |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* phpbu Factory |
23
|
|
|
* |
24
|
|
|
* @var \phpbu\App\Factory |
25
|
|
|
*/ |
26
|
|
|
protected $factory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Application result |
30
|
|
|
* |
31
|
|
|
* @var \phpbu\App\Result |
32
|
|
|
*/ |
33
|
|
|
protected $result; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Backup failed |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $failure; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* App Configuration |
44
|
|
|
* |
45
|
|
|
* @var \phpbu\App\Configuration |
46
|
|
|
*/ |
47
|
|
|
protected $configuration; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor |
51
|
|
|
* |
52
|
|
|
* @param \phpbu\App\Factory $factory |
53
|
|
|
*/ |
54
|
8 |
|
public function __construct(Factory $factory) |
55
|
|
|
{ |
56
|
8 |
|
$this->factory = $factory; |
57
|
8 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Factory getter |
61
|
|
|
* |
62
|
|
|
* @return \phpbu\App\Factory |
63
|
|
|
*/ |
64
|
2 |
|
public function getFactory() |
65
|
|
|
{ |
66
|
2 |
|
return $this->factory; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Run phpbu |
71
|
|
|
* |
72
|
|
|
* @param \phpbu\App\Configuration $configuration |
73
|
|
|
* @param \phpbu\App\Factory |
74
|
|
|
* @return \phpbu\App\Result |
75
|
|
|
* @throws \phpbu\App\Exception |
76
|
8 |
|
*/ |
77
|
|
|
public function run(Configuration $configuration) |
78
|
8 |
|
{ |
79
|
8 |
|
$stop = false; |
80
|
8 |
|
$this->result = new Result(); |
81
|
|
|
$this->configuration = $configuration; |
82
|
8 |
|
|
83
|
8 |
|
$this->setupLoggers($configuration); |
84
|
8 |
|
$this->result->phpbuStart($configuration); |
85
|
|
|
|
86
|
|
|
// create backups |
87
|
|
|
/** @var \phpbu\App\Configuration\Backup $backup */ |
88
|
8 |
|
foreach ($configuration->getBackups() as $backup) { |
89
|
8 |
|
if ($stop) { |
90
|
1 |
|
break; |
91
|
|
|
} |
92
|
|
|
// make sure the backup should be executed and is not excluded via the --limit option |
93
|
8 |
|
if (!$configuration->isBackupActive($backup->getName())) { |
94
|
1 |
|
$this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL); |
95
|
1 |
|
continue; |
96
|
|
|
} |
97
|
|
|
// setup target and collector, reset failure state |
98
|
8 |
|
$target = $this->createTarget($backup->getTarget()); |
99
|
8 |
|
$collector = new Collector($target); |
100
|
8 |
|
$this->failure = false; |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
/* ___ ___ _______ ____ _____ |
104
|
|
|
* / _ )/ _ |/ ___/ //_/ / / / _ \ |
105
|
|
|
* / _ / __ / /__/ ,< / /_/ / ___/ |
106
|
|
|
* /____/_/ |_\___/_/|_|\____/_/ |
107
|
|
|
*/ |
108
|
8 |
|
$this->executeSource($backup, $target); |
109
|
|
|
|
110
|
|
|
/* _______ _____________ ______ |
111
|
|
|
* / ___/ // / __/ ___/ //_/ __/ |
112
|
|
|
* / /__/ _ / _// /__/ ,< _\ \ |
113
|
|
|
* \___/_//_/___/\___/_/|_/___/ |
114
|
|
|
*/ |
115
|
7 |
|
$this->executeChecks($backup, $target, $collector); |
116
|
|
|
|
117
|
|
|
/* __________ _____ ______ |
118
|
|
|
* / ___/ _ \ \/ / _ \/_ __/ |
119
|
|
|
* / /__/ , _/\ / ___/ / / |
120
|
|
|
* \___/_/|_| /_/_/ /_/ |
121
|
|
|
*/ |
122
|
7 |
|
$this->executeCrypt($backup, $target); |
123
|
|
|
|
124
|
|
|
/* ______ ___ ___________ |
125
|
|
|
* / __/\ \/ / |/ / ___/ __/ |
126
|
|
|
* _\ \ \ / / /___\ \ |
127
|
|
|
* /___/ /_/_/|_/\___/___/ |
128
|
|
|
*/ |
129
|
7 |
|
$this->executeSyncs($backup, $target); |
130
|
|
|
|
131
|
|
|
/* _______ _______ _ ____ _____ |
132
|
|
|
* / ___/ / / __/ _ | / |/ / / / / _ \ |
133
|
|
|
* / /__/ /__/ _// __ |/ / /_/ / ___/ |
134
|
|
|
* \___/____/___/_/ |_/_/|_/\____/_/ |
135
|
|
|
*/ |
136
|
7 |
|
$this->executeCleanup($backup, $target, $collector); |
137
|
|
|
|
138
|
1 |
|
} catch (\Exception $e) { |
139
|
1 |
|
$this->result->debug('exception: ' . $e->getMessage()); |
140
|
1 |
|
$this->result->addError($e); |
141
|
1 |
|
$this->result->backupFailed($backup); |
142
|
1 |
|
if ($backup->stopOnFailure()) { |
143
|
8 |
|
$stop = true; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
8 |
|
$this->result->phpbuEnd(); |
148
|
|
|
|
149
|
8 |
|
return $this->result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create and register all configured loggers. |
154
|
|
|
* |
155
|
|
|
* @param \phpbu\App\Configuration $configuration |
156
|
|
|
* @throws \phpbu\App\Exception |
157
|
|
|
*/ |
158
|
8 |
|
protected function setupLoggers(Configuration $configuration) |
159
|
|
|
{ |
160
|
8 |
|
foreach ($configuration->getLoggers() as $log) { |
161
|
8 |
|
// this is a already fully setup Listener so just add it |
162
|
8 |
|
if ($log instanceof Listener) { |
163
|
|
|
$logger = $log; |
164
|
|
|
} else { |
165
|
|
|
// this is a configuration blueprint for a logger, so create and add it |
166
|
|
|
/** @var \phpbu\App\Configuration\Logger $log */ |
167
|
|
|
/** @var \phpbu\App\Listener $logger */ |
168
|
|
|
$logger = $this->factory->createLogger($log->type, $log->options); |
169
|
8 |
|
} |
170
|
|
|
$this->result->addListener($logger); |
171
|
8 |
|
} |
172
|
|
|
} |
173
|
8 |
|
|
174
|
8 |
|
/** |
175
|
|
|
* Create a target. |
176
|
|
|
* |
177
|
|
|
* @param \phpbu\App\Configuration\Backup\Target $conf |
178
|
|
|
* @return \phpbu\App\Backup\Target |
179
|
8 |
|
* @throws \phpbu\App\Exception |
180
|
|
|
*/ |
181
|
8 |
|
protected function createTarget(Configuration\Backup\Target $conf) |
182
|
|
|
{ |
183
|
8 |
|
$target = new Target($conf->dirname, $conf->filename); |
184
|
|
|
$target->setupPath(); |
185
|
|
|
// add possible compressor |
186
|
|
|
if (!empty($conf->compression)) { |
187
|
|
|
$compression = Target\Compression\Factory::create($conf->compression); |
188
|
|
|
$target->setCompression($compression); |
189
|
|
|
} |
190
|
|
|
return $target; |
191
|
|
|
} |
192
|
8 |
|
|
193
|
|
|
/** |
194
|
8 |
|
* Execute the backup. |
195
|
8 |
|
* |
196
|
|
|
* @param \phpbu\App\Configuration\Backup $conf |
197
|
8 |
|
* @param \phpbu\App\Backup\Target $target |
198
|
8 |
|
* @throws \Exception |
199
|
8 |
|
*/ |
200
|
|
|
protected function executeSource(Configuration\Backup $conf, Target $target) |
201
|
8 |
|
{ |
202
|
|
|
$this->result->backupStart($conf); |
203
|
|
|
/* @var \phpbu\App\Runner\Source $runner */ |
204
|
|
|
$source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options); |
205
|
|
|
$runner = $this->factory->createRunner('source', $this->configuration->isSimulation()); |
206
|
|
|
$runner->run($source, $target, $this->result); |
207
|
|
|
$this->result->backupEnd($conf); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
8 |
|
* Execute checks. |
212
|
|
|
* |
213
|
8 |
|
* @param \phpbu\App\Configuration\Backup $backup |
214
|
|
|
* @param \phpbu\App\Backup\Target $target |
215
|
8 |
|
* @param \phpbu\App\Backup\Collector $collector |
216
|
8 |
|
* @throws \Exception |
217
|
8 |
|
*/ |
218
|
7 |
|
protected function executeChecks(Configuration\Backup $backup, Target $target, Collector $collector) |
219
|
7 |
|
{ |
220
|
|
|
/* @var \phpbu\App\Runner\Check $runner */ |
221
|
|
|
$runner = $this->factory->createRunner('check', $this->configuration->isSimulation()); |
222
|
|
|
foreach ($backup->getChecks() as $config) { |
223
|
|
|
try { |
224
|
|
|
$this->result->checkStart($config); |
225
|
|
|
$check = $this->factory->createCheck($config->type); |
226
|
|
|
if ($runner->run($check, $target, $config->value, $collector, $this->result)) { |
227
|
|
|
$this->result->checkEnd($config); |
228
|
|
|
} else { |
229
|
7 |
|
$this->failure = true; |
230
|
|
|
$this->result->checkFailed($config); |
231
|
|
|
} |
232
|
7 |
|
} catch (Exception $e) { |
233
|
7 |
|
$this->failure = true; |
234
|
|
|
$this->result->addError($e); |
235
|
7 |
|
$this->result->checkFailed($config); |
236
|
7 |
|
} |
237
|
7 |
|
} |
238
|
5 |
|
} |
239
|
|
|
|
240
|
1 |
|
/** |
241
|
6 |
|
* Execute encryption. |
242
|
|
|
* |
243
|
1 |
|
* @param \phpbu\App\Configuration\Backup $backup |
244
|
1 |
|
* @param \phpbu\App\Backup\Target $target |
245
|
1 |
|
* @throws \phpbu\App\Exception |
246
|
7 |
|
*/ |
247
|
|
|
protected function executeCrypt(Configuration\Backup $backup, Target $target) |
248
|
|
|
{ |
249
|
7 |
|
if ($backup->hasCrypt()) { |
250
|
|
|
$crypt = $backup->getCrypt(); |
251
|
|
|
try { |
252
|
|
|
$this->result->cryptStart($crypt); |
253
|
|
|
if ($this->failure && $crypt->skipOnFailure) { |
254
|
|
|
$this->result->cryptSkipped($crypt); |
255
|
|
|
} else { |
256
|
|
|
/* @var \phpbu\App\Runner\Crypter $runner */ |
257
|
7 |
|
$runner = $this->factory->createRunner('crypter', $this->configuration->isSimulation()); |
258
|
|
|
$runner->run($this->factory->createCrypter($crypt->type, $crypt->options), $target, $this->result); |
259
|
7 |
|
} |
260
|
7 |
|
} catch (Backup\Crypter\Exception $e) { |
261
|
|
|
$this->failure = true; |
262
|
7 |
|
$this->result->addError($e); |
263
|
7 |
|
$this->result->cryptFailed($crypt); |
264
|
2 |
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
5 |
|
|
268
|
7 |
|
/** |
269
|
|
|
* Execute all syncs. |
270
|
1 |
|
* |
271
|
1 |
|
* @param \phpbu\App\Configuration\Backup $backup |
272
|
1 |
|
* @param \phpbu\App\Backup\Target $target |
273
|
1 |
|
* @throws \Exception |
274
|
|
|
*/ |
275
|
|
|
protected function executeSyncs(Configuration\Backup $backup, Target $target) |
276
|
7 |
|
{ |
277
|
|
|
/* @var \phpbu\App\Runner\Crypter $runner */ |
278
|
|
|
/* @var \phpbu\App\Configuration\Backup\Sync $sync */ |
279
|
|
|
$runner = $this->factory->createRunner('sync', $this->configuration->isSimulation()); |
280
|
|
|
foreach ($backup->getSyncs() as $sync) { |
281
|
|
|
try { |
282
|
|
|
$this->result->syncStart($sync); |
283
|
|
|
if ($this->failure && $sync->skipOnFailure) { |
284
|
|
|
$this->result->syncSkipped($sync); |
285
|
7 |
|
} else { |
286
|
|
|
$runner->run($this->factory->createSync($sync->type, $sync->options), $target, $this->result); |
287
|
|
|
$this->result->syncEnd($sync); |
288
|
|
|
} |
289
|
7 |
|
} catch (Backup\Sync\Exception $e) { |
290
|
7 |
|
$this->failure = true; |
291
|
|
|
$this->result->addError($e); |
292
|
7 |
|
$this->result->syncFailed($sync); |
293
|
7 |
|
} |
294
|
3 |
|
} |
295
|
|
|
} |
296
|
4 |
|
|
297
|
6 |
|
/** |
298
|
|
|
* Execute the cleanup. |
299
|
1 |
|
* |
300
|
1 |
|
* @param \phpbu\App\Configuration\Backup $backup |
301
|
1 |
|
* @param \phpbu\App\Backup\Target $target |
302
|
7 |
|
* @param \phpbu\App\Backup\Collector $collector |
303
|
|
|
* @throws \phpbu\App\Exception |
304
|
|
|
*/ |
305
|
7 |
|
protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector $collector) |
306
|
|
|
{ |
307
|
|
|
/* @var \phpbu\App\Runner\Cleaner $runner */ |
308
|
|
|
/* @var \phpbu\App\Configuration\Backup\Cleanup $cleanup */ |
309
|
|
|
if ($backup->hasCleanup()) { |
310
|
|
|
$cleanup = $backup->getCleanup(); |
311
|
|
|
try { |
312
|
|
|
$runner = $this->factory->createRunner('cleaner', $this->configuration->isSimulation()); |
313
|
|
|
$this->result->cleanupStart($cleanup); |
314
|
|
|
if ($this->failure && $cleanup->skipOnFailure) { |
315
|
7 |
|
$this->result->cleanupSkipped($cleanup); |
316
|
|
|
} else { |
317
|
|
|
$cleaner = $this->factory->createCleaner($cleanup->type, $cleanup->options); |
318
|
|
|
$runner->run($cleaner, $target, $collector, $this->result); |
319
|
7 |
|
$this->result->cleanupEnd($cleanup); |
320
|
7 |
|
} |
321
|
|
|
} catch (Backup\Cleaner\Exception $e) { |
322
|
7 |
|
$this->failure = true; |
323
|
7 |
|
$this->result->addError($e); |
324
|
7 |
|
$this->result->cleanupFailed($cleanup); |
325
|
4 |
|
} |
326
|
|
|
} |
327
|
3 |
|
} |
328
|
|
|
} |
329
|
|
|
|