]> BookStack Code Mirror - system-cli/blob - src/Commands/RestoreCommand.php
2fa3c853c071931691c303b19ad53485f7f936d7
[system-cli] / src / Commands / RestoreCommand.php
1 <?php declare(strict_types=1);
2
3 namespace Cli\Commands;
4
5 use Cli\Services\AppLocator;
6 use Cli\Services\ArtisanRunner;
7 use Cli\Services\BackupZip;
8 use Cli\Services\Directories;
9 use Cli\Services\EnvironmentLoader;
10 use Cli\Services\InteractiveConsole;
11 use Cli\Services\MySqlRunner;
12 use Cli\Services\Paths;
13 use Cli\Services\ProgramRunner;
14 use Cli\Services\RequirementsValidator;
15 use Exception;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
21
22 class RestoreCommand extends Command
23 {
24     protected function configure(): void
25     {
26         $this->setName('restore');
27         $this->addArgument('backup-zip', InputArgument::REQUIRED, 'Path to the ZIP file containing your backup.');
28         $this->setDescription('Restore data and files from a backup ZIP file.');
29         $this->addOption('app-directory', null, InputOption::VALUE_OPTIONAL, 'BookStack install directory to restore into', '');
30     }
31
32     /**
33      * @throws CommandError
34      * @throws Exception
35      */
36     protected function execute(InputInterface $input, OutputInterface $output): int
37     {
38         $interactions = new InteractiveConsole($this->getHelper('question'), $input, $output);
39
40         $output->writeln("<warn>Warning!</warn>");
41         $output->writeln("<warn>- A restore operation will overwrite and remove files & content from an existing instance.</warn>");
42         $output->writeln("<warn>- Any existing tables within the configured database will be dropped.</warn>");
43         $output->writeln("<warn>- You should only restore into an instance of the same or newer BookStack version.</warn>");
44         $output->writeln("<warn>- This command won't handle, restore or address any server configuration.</warn>");
45
46         $appDir = AppLocator::require($input->getOption('app-directory'));
47         $output->writeln("<info>Checking system requirements...</info>");
48         RequirementsValidator::validate();
49         (new ProgramRunner('mysql', '/usr/bin/mysql'))->ensureFound();
50
51         $zipPath = realpath($input->getArgument('backup-zip'));
52         $zip = new BackupZip($zipPath);
53         $contents = $zip->getContentsOverview();
54
55         $output->writeln("\n<info>Contents found in the backup ZIP:</info>");
56         $hasContent = false;
57         foreach ($contents as $info) {
58             $output->writeln(($info['exists'] ? '✔ ' : '❌ ') . $info['desc']);
59             if ($info['exists']) {
60                 $hasContent = true;
61             }
62         }
63
64         if (!$hasContent) {
65             throw new CommandError("Provided ZIP backup [{$zipPath}] does not have any expected restorable content.");
66         }
67
68         $output->writeln("<info>The checked elements will be restored into [{$appDir}].</info>");
69         $output->writeln("<warn>Existing content will be overwritten.</warn>");
70
71         if (!$interactions->confirm("Do you want to continue?")) {
72             $output->writeln("<info>Stopping restore operation.</info>");
73             return Command::SUCCESS;
74         }
75
76         $output->writeln("<info>Extracting ZIP into temporary directory...</info>");
77         $extractDir = Paths::join($appDir, 'restore-temp-' . time());
78         if (!mkdir($extractDir)) {
79             throw new CommandError("Could not create temporary extraction directory at [{$extractDir}].");
80         }
81         $zip->extractInto($extractDir);
82
83         $envChanges = [];
84         if ($contents['env']['exists']) {
85             $output->writeln("<info>Restoring and merging .env file...</info>");
86             $envChanges = $this->restoreEnv($extractDir, $appDir, $output, $interactions);
87         }
88
89         $folderLocations = ['themes', 'public/uploads', 'storage/uploads'];
90         foreach ($folderLocations as $folderSubPath) {
91             if ($contents[$folderSubPath]['exists']) {
92                 $output->writeln("<info>Restoring {$folderSubPath} folder...</info>");
93                 $this->restoreFolder($folderSubPath, $appDir, $extractDir);
94             }
95         }
96
97         $artisan = (new ArtisanRunner($appDir));
98         if ($contents['db']['exists']) {
99             $output->writeln("<info>Restoring database from SQL dump...</info>");
100             $this->restoreDatabase($appDir, $extractDir);
101
102             $output->writeln("<info>Running database migrations...</info>");
103             $artisan->run(['migrate', '--force']);
104         }
105
106         if ($envChanges && $envChanges['old_url'] !== $envChanges['new_url']) {
107             $output->writeln("<info>App URL change made, updating database with URL change...</info>");
108             $artisan->run([
109                 'bookstack:update-url', '--force',
110                 $envChanges['old_url'], $envChanges['new_url'],
111             ]);
112         }
113
114         $output->writeln("<info>Clearing app caches...</info>");
115         $artisan->run(['cache:clear']);
116         $artisan->run(['config:clear']);
117         $artisan->run(['view:clear']);
118
119         $output->writeln("<info>Cleaning up extract directory...</info>");
120         Directories::delete($extractDir);
121
122         $output->writeln("<success>\nRestore operation complete!</success>");
123         $output->writeln("<info>You may need to fix file/folder permissions so that the webserver has</info>");
124         $output->writeln("<info>the required read/write access to the necessary directories & files.</info>");
125
126         return Command::SUCCESS;
127     }
128
129     protected function restoreEnv(string $extractDir, string $appDir, OutputInterface $output, InteractiveConsole $interactions): array
130     {
131         $oldEnv = EnvironmentLoader::load($extractDir);
132         $currentEnv = EnvironmentLoader::load($appDir);
133         $envContents = file_get_contents(Paths::join($extractDir, '.env'));
134         $appEnvPath = Paths::real(Paths::join($appDir, '.env'));
135
136         $mysqlCurrent = MySqlRunner::fromEnvOptions($currentEnv);
137         $mysqlOld = MySqlRunner::fromEnvOptions($oldEnv);
138         if (!$mysqlOld->testConnection()) {
139             $currentWorking = $mysqlCurrent->testConnection();
140             if (!$currentWorking) {
141                 throw new CommandError("Could not find a working database configuration");
142             }
143
144             // Copy across new env details to old env
145             $currentEnvContents = file_get_contents($appEnvPath);
146             $currentEnvDbLines = array_values(array_filter(explode("\n", $currentEnvContents), function (string $line) {
147                 return str_starts_with($line, 'DB_');
148             }));
149             $oldEnvLines = array_values(array_filter(explode("\n", $envContents), function (string $line) {
150                 return !str_starts_with($line, 'DB_');
151             }));
152             $envContents = implode("\n", [
153                 '# Database credentials merged from existing .env file',
154                 ...$currentEnvDbLines,
155                 ...$oldEnvLines
156             ]);
157             copy($appEnvPath, $appEnvPath . '.backup');
158         }
159
160         $oldUrl = $oldEnv['APP_URL'] ?? '';
161         $newUrl = $currentEnv['APP_URL'] ?? '';
162         $returnData = [
163             'old_url' => $oldUrl,
164             'new_url' => $oldUrl,
165         ];
166
167         if ($oldUrl !== $newUrl) {
168             $question = 'Found different APP_URL values, which would you like to use?';
169             $changedUrl = $interactions->choice($question, array_filter([$oldUrl, $newUrl]));
170             $envContents = preg_replace('/^APP_URL=.*?$/m', 'APP_URL="' . $changedUrl . '"', $envContents);
171             $returnData['new_url'] = $changedUrl;
172         }
173
174         file_put_contents($appEnvPath, $envContents);
175
176         return $returnData;
177     }
178
179     protected function restoreFolder(string $folderSubPath, string $appDir, string $extractDir): void
180     {
181         $fullAppFolderPath = Paths::real(Paths::join($appDir, $folderSubPath));
182         Directories::delete($fullAppFolderPath);
183         Directories::move(Paths::join($extractDir, $folderSubPath), $fullAppFolderPath);
184     }
185
186     protected function restoreDatabase(string $appDir, string $extractDir): void
187     {
188         $dbDump = Paths::join($extractDir, 'db.sql');
189         $currentEnv = EnvironmentLoader::load($appDir);
190         $mysql = MySqlRunner::fromEnvOptions($currentEnv);
191
192         // Drop existing tables
193         $dropSqlTempFile = tempnam(sys_get_temp_dir(), 'bs-cli-restore');
194         file_put_contents($dropSqlTempFile, $mysql->dropTablesSql());
195         $mysql->importSqlFile($dropSqlTempFile);
196
197         // Import MySQL dump
198         $mysql->importSqlFile($dbDump);
199     }
200 }