|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Runner; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Check; |
|
5
|
|
|
use phpbu\App\Backup\Cleaner; |
|
6
|
|
|
use phpbu\App\Backup\Collector; |
|
7
|
|
|
use phpbu\App\Backup\Compressor; |
|
8
|
|
|
use phpbu\App\Backup\Crypter; |
|
9
|
|
|
use phpbu\App\Backup\Source; |
|
10
|
|
|
use phpbu\App\Backup\Sync; |
|
11
|
|
|
use phpbu\App\Backup\Target; |
|
12
|
|
|
use phpbu\App\Configuration; |
|
13
|
|
|
use phpbu\App\Result; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Simulate Runner |
|
17
|
|
|
* |
|
18
|
|
|
* @package phpbu |
|
19
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
20
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
21
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
22
|
|
|
* @link https://phpbu.de/ |
|
23
|
|
|
* @since Class available since Release 5.1.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class Simulate extends Compression |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Execute backups. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \phpbu\App\Configuration $configuration |
|
31
|
|
|
* @return \phpbu\App\Result |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
*/ |
|
34
|
5 |
|
public function run(Configuration $configuration) : Result |
|
35
|
|
|
{ |
|
36
|
5 |
|
$this->configuration = $configuration; |
|
37
|
5 |
|
$this->result->phpbuStart($configuration); |
|
38
|
|
|
|
|
39
|
|
|
// create backups |
|
40
|
|
|
/** @var \phpbu\App\Configuration\Backup $backup */ |
|
41
|
5 |
|
foreach ($configuration->getBackups() as $backup) { |
|
42
|
|
|
// make sure the backup should be executed and is not excluded via the --limit option |
|
43
|
5 |
View Code Duplication |
if (!$configuration->isBackupActive($backup->getName())) { |
|
44
|
1 |
|
$this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL); |
|
45
|
1 |
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
// setup target and collector |
|
48
|
5 |
|
$target = $this->factory->createTarget($backup->getTarget()); |
|
49
|
5 |
|
$collector = new Collector($target); |
|
50
|
|
|
|
|
51
|
5 |
|
$this->simulateSource($backup, $target); |
|
52
|
5 |
|
$this->simulateChecks($backup, $target, $collector); |
|
53
|
5 |
|
$this->simulateCrypt($backup, $target); |
|
54
|
5 |
|
$this->simulateSyncs($backup, $target); |
|
55
|
5 |
|
$this->simulateCleanup($backup, $target, $collector); |
|
56
|
|
|
} |
|
57
|
5 |
|
$this->result->phpbuEnd(); |
|
58
|
|
|
|
|
59
|
5 |
|
return $this->result; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Simulate the backup. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \phpbu\App\Configuration\Backup $conf |
|
66
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
67
|
|
|
* @throws \Exception |
|
68
|
|
|
*/ |
|
69
|
5 |
View Code Duplication |
protected function simulateSource(Configuration\Backup $conf, Target $target) |
|
70
|
|
|
{ |
|
71
|
5 |
|
$this->result->backupStart($conf); |
|
72
|
5 |
|
$source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options); |
|
73
|
|
|
|
|
74
|
5 |
|
if ($source instanceof Source\Simulator) { |
|
75
|
5 |
|
$status = $source->simulate($target, $this->result); |
|
76
|
5 |
|
$this->compress($status, $target, $this->result); |
|
77
|
|
|
} |
|
78
|
5 |
|
$this->result->backupEnd($conf); |
|
79
|
5 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Simulate checks. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
|
85
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
86
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
5 |
View Code Duplication |
protected function simulateChecks(Configuration\Backup $backup, Target $target, Collector $collector) |
|
90
|
|
|
{ |
|
91
|
5 |
|
foreach ($backup->getChecks() as $config) { |
|
92
|
5 |
|
$this->result->checkStart($config); |
|
93
|
5 |
|
$check = $this->factory->createCheck($config->type); |
|
94
|
5 |
|
if ($check instanceof Check\Simulator) { |
|
95
|
5 |
|
$check->simulate($target, $config->value, $collector, $this->result); |
|
96
|
|
|
} |
|
97
|
5 |
|
$this->result->checkEnd($config); |
|
98
|
|
|
} |
|
99
|
5 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Simulate encryption. |
|
103
|
|
|
* |
|
104
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
|
105
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
106
|
|
|
* @throws \phpbu\App\Exception |
|
107
|
|
|
*/ |
|
108
|
5 |
View Code Duplication |
protected function simulateCrypt(Configuration\Backup $backup, Target $target) |
|
109
|
|
|
{ |
|
110
|
5 |
|
if ($backup->hasCrypt()) { |
|
111
|
5 |
|
$crypt = $backup->getCrypt(); |
|
112
|
5 |
|
$this->result->cryptStart($crypt); |
|
113
|
5 |
|
$crypter = $this->factory->createCrypter($crypt->type, $crypt->options); |
|
114
|
5 |
|
if ($crypter instanceof Crypter\Simulator) { |
|
115
|
5 |
|
$crypter->simulate($target, $this->result); |
|
116
|
|
|
} |
|
117
|
5 |
|
$target->setCrypter($crypter); |
|
118
|
5 |
|
$this->result->cryptEnd($crypt); |
|
119
|
|
|
} |
|
120
|
5 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Simulate all syncs. |
|
124
|
|
|
* |
|
125
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
|
126
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
127
|
|
|
* @throws \Exception |
|
128
|
|
|
*/ |
|
129
|
5 |
|
protected function simulateSyncs(Configuration\Backup $backup, Target $target) |
|
130
|
|
|
{ |
|
131
|
|
|
/* @var \phpbu\App\Configuration\Backup\Sync $sync */ |
|
132
|
5 |
|
foreach ($backup->getSyncs() as $sync) { |
|
133
|
5 |
|
$sync = $this->factory->createSync($sync->type, $sync->options); |
|
134
|
5 |
|
if ($sync instanceof Sync\Simulator) { |
|
135
|
5 |
|
$sync->simulate($target, $this->result); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
5 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Simulate the cleanup. |
|
142
|
|
|
* |
|
143
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
|
144
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
145
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
|
146
|
|
|
* @throws \phpbu\App\Exception |
|
147
|
|
|
*/ |
|
148
|
5 |
View Code Duplication |
protected function simulateCleanup(Configuration\Backup $backup, Target $target, Collector $collector) |
|
149
|
|
|
{ |
|
150
|
|
|
/* @var \phpbu\App\Configuration\Backup\Cleanup $cleanup */ |
|
151
|
5 |
|
if ($backup->hasCleanup()) { |
|
152
|
5 |
|
$cleanup = $backup->getCleanup(); |
|
153
|
5 |
|
$cleaner = $this->factory->createCleaner($cleanup->type, $cleanup->options); |
|
154
|
5 |
|
$this->result->cleanupStart($cleanup); |
|
155
|
5 |
|
if ($cleaner instanceof Cleaner\Simulator) { |
|
156
|
5 |
|
$cleaner->simulate($target, $collector, $this->result); |
|
157
|
|
|
} |
|
158
|
5 |
|
$this->result->cleanupEnd($cleanup); |
|
159
|
|
|
} |
|
160
|
5 |
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Execute the compressor. |
|
164
|
|
|
* Returns the path to the created archive file. |
|
165
|
|
|
* |
|
166
|
|
|
* @param \phpbu\App\Backup\Compressor\Executable $compressor |
|
167
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
168
|
|
|
* @param \phpbu\App\Result $result |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
3 |
|
protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string |
|
172
|
|
|
{ |
|
173
|
3 |
|
$result->debug($compressor->getExecutable($target)->getCommand()); |
|
174
|
3 |
|
return $compressor->getArchiveFile($target); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|