1 | <?php |
||
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 |
|
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 |
|
60 | |||
61 | /** |
||
62 | * Output the restore plan |
||
63 | * |
||
64 | * @param \phpbu\App\Backup\Restore\Plan $plan |
||
65 | */ |
||
66 | 3 | private function printPlan(Plan $plan) |
|
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) |
|
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 |
|
154 | |||
155 | /** |
||
156 | * Output the restore commands |
||
157 | * |
||
158 | * @param \phpbu\App\Backup\Restore\Plan $plan |
||
159 | * @return void |
||
160 | */ |
||
161 | 3 | private function printRestoreCommands(Plan $plan): void |
|
162 | { |
||
163 | 3 | if (!$plan->isSourceSupported()) { |
|
164 | echo Util::formatWithColor('fg-red', "WARNING: Your configured source does not support restore for now.\n"); |
||
165 | return; |
||
166 | } |
||
167 | |||
168 | 3 | $this->printExtractionCommands($plan->getDecompressionCommands()); |
|
169 | |||
170 | 3 | echo Util::formatWithColor('fg-yellow', "# Restore your data [BE CAREFUL]\n"); |
|
171 | 3 | foreach ($plan->getRestoreCommands() as $cmd) { |
|
172 | 1 | echo $cmd . PHP_EOL; |
|
173 | } |
||
174 | 3 | echo PHP_EOL; |
|
175 | 3 | } |
|
176 | |||
177 | /** |
||
178 | * Output extraction commands |
||
179 | * |
||
180 | * @param array $commands |
||
181 | * @return void |
||
182 | */ |
||
183 | 3 | private function printExtractionCommands(array $commands): void |
|
193 | } |
||
194 |