|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Runner; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Crypter; |
|
5
|
|
|
use phpbu\App\Backup\Decompressor; |
|
6
|
|
|
use phpbu\App\Backup\Restore\Plan; |
|
7
|
|
|
use phpbu\App\Backup\Source; |
|
8
|
|
|
use phpbu\App\Backup\Target; |
|
9
|
|
|
use phpbu\App\Configuration; |
|
10
|
|
|
use phpbu\App\Result; |
|
11
|
|
|
use SebastianFeldmann\Cli\Util; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Restore Runner |
|
15
|
|
|
* |
|
16
|
|
|
* @package phpbu |
|
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 https://phpbu.de/ |
|
21
|
|
|
* @since Class available since Release 6.0.0 |
|
22
|
|
|
* @internal |
|
23
|
|
|
*/ |
|
24
|
|
|
class Restore extends Process |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Restore all backups |
|
28
|
|
|
* |
|
29
|
|
|
* @param \phpbu\App\Configuration $configuration |
|
30
|
|
|
* @return \phpbu\App\Result |
|
31
|
|
|
* @throws \Exception |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function run(Configuration $configuration) : Result |
|
34
|
|
|
{ |
|
35
|
3 |
|
foreach ($configuration->getBackups() as $backup) { |
|
36
|
3 |
|
if ($configuration->isBackupActive($backup->getName())) { |
|
37
|
3 |
|
$this->printPlan($this->createRestorePlan($backup)); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
return $this->result; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Collects all restore commands in a restore plan |
|
46
|
|
|
* |
|
47
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
|
48
|
|
|
* @return \phpbu\App\Backup\Restore\Plan |
|
49
|
|
|
* @throws \phpbu\App\Exception |
|
50
|
|
|
*/ |
|
51
|
3 |
|
private function createRestorePlan(Configuration\Backup $backup): Plan |
|
52
|
|
|
{ |
|
53
|
3 |
|
$plan = new Plan(); |
|
54
|
3 |
|
$target = $this->factory->createTarget($backup->getTarget()); |
|
55
|
3 |
|
$this->decryptBackup($target, $backup, $plan); |
|
56
|
3 |
|
$this->restoreBackup($target, $backup, $plan); |
|
57
|
|
|
|
|
58
|
3 |
|
return $plan; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Output the restore plan |
|
63
|
|
|
* |
|
64
|
|
|
* @param \phpbu\App\Backup\Restore\Plan $plan |
|
65
|
|
|
*/ |
|
66
|
3 |
|
private function printPlan(Plan $plan) |
|
67
|
|
|
{ |
|
68
|
3 |
|
$this->printDecryptionCommands($plan); |
|
69
|
3 |
|
$this->printRestoreCommands($plan); |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
74
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
|
75
|
|
|
* @param \phpbu\App\Backup\Restore\Plan $plan |
|
76
|
|
|
* @throws \phpbu\App\Exception |
|
77
|
|
|
*/ |
|
78
|
3 |
|
private function decryptBackup(Target $target, Configuration\Backup $backup, Plan $plan) |
|
79
|
|
|
{ |
|
80
|
3 |
|
if (!$backup->hasCrypt()) { |
|
81
|
1 |
|
return; |
|
82
|
|
|
} |
|
83
|
2 |
|
$cryptConf = $backup->getCrypt(); |
|
84
|
2 |
|
$crypt = $this->factory->createCrypter($cryptConf->type, $cryptConf->options); |
|
85
|
|
|
|
|
86
|
|
|
// check if decryption is supported |
|
87
|
2 |
|
if (!$crypt instanceof Crypter\Restorable) { |
|
88
|
1 |
|
$plan->markCryptAsUnsupported(); |
|
89
|
1 |
|
return; |
|
90
|
|
|
} |
|
91
|
1 |
|
$crypt->restore($target, $plan); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
96
|
|
|
* @param \phpbu\App\Configuration\Backup $backup |
|
97
|
|
|
* @param \phpbu\App\Backup\Restore\Plan $plan |
|
98
|
|
|
* @throws \phpbu\App\Exception |
|
99
|
|
|
*/ |
|
100
|
3 |
|
private function restoreBackup(Target $target, Configuration\Backup $backup, Plan $plan) |
|
101
|
|
|
{ |
|
102
|
3 |
|
$sourceConf = $backup->getSource(); |
|
103
|
3 |
|
$source = $this->factory->createSource($sourceConf->type, $sourceConf->options); |
|
104
|
|
|
// make sure restore is supported |
|
105
|
3 |
|
if (!$source instanceof Source\Restorable) { |
|
106
|
|
|
$plan->markSourceAsUnsupported(); |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
// pass plan to source to collect restore commands |
|
110
|
3 |
|
$status = $source->restore($target, $plan); |
|
111
|
|
|
|
|
112
|
|
|
// make sure we decompress the backup |
|
113
|
3 |
|
if ($target->shouldBeCompressed()) { |
|
114
|
|
|
$decompressor = new Decompressor\File(); |
|
115
|
|
|
$command = $decompressor->decompress($target); |
|
116
|
|
|
$plan->addDecompressionCommand($command); |
|
117
|
|
|
$target->disableCompression(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// if source created a directory we have to un-tar the decompressed backup |
|
121
|
3 |
|
if ($status->isDirectory()) { |
|
122
|
|
|
$decompressor = new Decompressor\Directory(); |
|
123
|
|
|
$command = $decompressor->decompress($target); |
|
124
|
|
|
|
|
125
|
|
|
$target->removeFileSuffix('tar'); |
|
126
|
|
|
$plan->addDecompressionCommand($command); |
|
127
|
|
|
} |
|
128
|
3 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Output the decryption commands |
|
132
|
|
|
* |
|
133
|
|
|
* @param \phpbu\App\Backup\Restore\Plan $plan |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
3 |
|
private function printDecryptionCommands(Plan $plan): void |
|
137
|
|
|
{ |
|
138
|
3 |
|
if (!$plan->isCryptSupported()) { |
|
139
|
1 |
|
echo Util::formatWithColor('fg-red', "WARNING: Your configured crypt does not support restore for now.\n"); |
|
140
|
1 |
|
return; |
|
141
|
|
|
} |
|
142
|
2 |
|
$commands = $plan->getDecryptionCommands(); |
|
143
|
|
|
|
|
144
|
2 |
|
if (empty($commands)) { |
|
145
|
1 |
|
return; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
echo Util::formatWithColor('fg-yellow', "# Decrypt your backup\n"); |
|
149
|
1 |
|
$this->printCommands($commands); |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Output the restore commands |
|
154
|
|
|
* |
|
155
|
|
|
* @param \phpbu\App\Backup\Restore\Plan $plan |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
3 |
|
private function printRestoreCommands(Plan $plan): void |
|
159
|
|
|
{ |
|
160
|
3 |
|
if (!$plan->isSourceSupported()) { |
|
161
|
|
|
echo Util::formatWithColor('fg-red', "WARNING: Your configured source does not support restore for now.\n"); |
|
162
|
|
|
return; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
3 |
|
$this->printExtractionCommands($plan->getDecompressionCommands()); |
|
166
|
|
|
|
|
167
|
3 |
|
echo Util::formatWithColor('fg-yellow', "# Restore your data [BE CAREFUL]\n"); |
|
168
|
3 |
|
$this->printCommands($plan->getRestoreCommands()); |
|
169
|
3 |
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Output extraction commands |
|
173
|
|
|
* |
|
174
|
|
|
* @param array $commands |
|
175
|
|
|
* @return void |
|
176
|
|
|
*/ |
|
177
|
3 |
|
private function printExtractionCommands(array $commands): void |
|
178
|
|
|
{ |
|
179
|
3 |
|
if (!empty($commands)) { |
|
180
|
|
|
echo Util::formatWithColor('fg-yellow', "# Extract your backup \n"); |
|
181
|
|
|
$this->printCommands($commands); |
|
182
|
|
|
} |
|
183
|
3 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Print restore plan commands |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $commands |
|
189
|
|
|
* @return void |
|
190
|
|
|
*/ |
|
191
|
3 |
|
private function printCommands(array $commands): void |
|
192
|
|
|
{ |
|
193
|
3 |
|
foreach ($commands as $cmd) { |
|
194
|
1 |
|
if (!empty($cmd['comment'])) { |
|
195
|
|
|
echo Util::formatWithColor('fg-yellow', '# ' . $cmd['comment'] . PHP_EOL); |
|
196
|
|
|
} |
|
197
|
1 |
|
echo $cmd['command'] . PHP_EOL; |
|
198
|
|
|
} |
|
199
|
3 |
|
echo PHP_EOL; |
|
200
|
3 |
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|