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