1 <?php declare(strict_types=1);
3 namespace Cli\Commands;
5 use Cli\Services\AppLocator;
6 use Cli\Services\ArtisanRunner;
7 use Cli\Services\BackupZip;
8 use Cli\Services\EnvironmentLoader;
9 use Cli\Services\InteractiveConsole;
10 use Cli\Services\MySqlRunner;
11 use Cli\Services\ProgramRunner;
12 use Cli\Services\RequirementsValidator;
14 use RecursiveDirectoryIterator;
15 use RecursiveIteratorIterator;
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;
22 class RestoreCommand extends Command
24 protected function configure(): void
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', '');
33 * @throws CommandError
36 protected function execute(InputInterface $input, OutputInterface $output): int
38 $interactions = new InteractiveConsole($this->getHelper('question'), $input, $output);
40 $output->writeln("<info>Warning!</info>");
41 $output->writeln("<info>- A restore operation will overwrite and remove files & content from an existing instance.</info>");
42 $output->writeln("<info>- Any existing tables within the configured database will be dropped.</info>");
43 $output->writeln("<info>- You should only restore into an instance of the same or newer BookStack version.</info>");
44 $output->writeln("<info>- This command won't handle, restore or address any server configuration.</info>");
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();
51 $zipPath = realpath($input->getArgument('backup-zip'));
52 $zip = new BackupZip($zipPath);
53 $contents = $zip->getContentsOverview();
55 $output->writeln("\n<info>Contents found in the backup ZIP:</info>");
57 foreach ($contents as $info) {
58 $output->writeln(($info['exists'] ? '✔ ' : '❌ ') . $info['desc']);
59 if ($info['exists']) {
65 throw new CommandError("Provided ZIP backup [{$zipPath}] does not have any expected restorable content.");
68 $output->writeln("<info>The checked elements will be restored into [{$appDir}].</info>");
69 $output->writeln("<info>Existing content will be overwritten.</info>");
71 if (!$interactions->confirm("Do you want to continue?")) {
72 $output->writeln("<info>Stopping restore operation.</info>");
73 return Command::SUCCESS;
76 $output->writeln("<info>Extracting ZIP into temporary directory...</info>");
77 $extractDir = $appDir . DIRECTORY_SEPARATOR . 'restore-temp-' . time();
78 if (!mkdir($extractDir)) {
79 throw new CommandError("Could not create temporary extraction directory at [{$extractDir}].");
81 $zip->extractInto($extractDir);
84 if ($contents['env']['exists']) {
85 $output->writeln("<info>Restoring and merging .env file...</info>");
86 $envChanges = $this->restoreEnv($extractDir, $appDir, $output, $interactions);
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);
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);
102 $output->writeln("<info>Running database migrations...</info>");
103 $artisan->run(['migrate', '--force']);
106 if ($envChanges && $envChanges['old_url'] !== $envChanges['new_url']) {
107 $output->writeln("<info>App URL change made, Updating database with URL change...</info>");
109 'bookstack:update-url',
110 $envChanges['old_url'], $envChanges['new_url'],
114 $output->writeln("<info>Clearing app caches...</info>");
115 $artisan->run(['cache:clear']);
116 $artisan->run(['config:clear']);
117 $artisan->run(['view:clear']);
119 $output->writeln("<info>Cleaning up extract directory...</info>");
120 $this->deleteDirectoryAndContents($extractDir);
122 $output->writeln("<info>\nRestore operation complete!</info>");
124 return Command::SUCCESS;
127 protected function restoreEnv(string $extractDir, string $appDir, OutputInterface $output, InteractiveConsole $interactions): array
129 $oldEnv = EnvironmentLoader::load($extractDir);
130 $currentEnv = EnvironmentLoader::load($appDir);
131 $envContents = file_get_contents($extractDir . DIRECTORY_SEPARATOR . '.env');
132 $appEnvPath = $appDir . DIRECTORY_SEPARATOR . '.env';
134 $mysqlCurrent = MySqlRunner::fromEnvOptions($currentEnv);
135 $mysqlOld = MySqlRunner::fromEnvOptions($oldEnv);
136 if (!$mysqlOld->testConnection()) {
137 $currentWorking = $mysqlCurrent->testConnection();
138 if (!$currentWorking) {
139 throw new CommandError("Could not find a working database configuration");
142 // Copy across new env details to old env
143 $currentEnvContents = file_get_contents($appEnvPath);
144 $currentEnvDbLines = array_values(array_filter(explode("\n", $currentEnvContents), function (string $line) {
145 return str_starts_with($line, 'DB_');
147 $oldEnvLines = array_values(array_filter(explode("\n", $currentEnvContents), function (string $line) {
148 return !str_starts_with($line, 'DB_');
150 $envContents = implode("\n", [
151 '# Database credentials merged from existing .env file',
152 ...$currentEnvDbLines,
155 copy($appEnvPath, $appEnvPath . '.backup');
158 $oldUrl = $oldEnv['APP_URL'] ?? '';
159 $newUrl = $currentEnv['APP_URL'] ?? '';
161 'old_url' => $oldUrl,
162 'new_url' => $oldUrl,
165 if ($oldUrl !== $newUrl) {
166 $output->writeln("Found different APP_URL values:");
167 $changedUrl = $interactions->choice('Which would you like to use?', array_filter([$oldUrl, $newUrl]));
168 $envContents = preg_replace('/^APP_URL=.*?$/', 'APP_URL="' . $changedUrl . '"', $envContents);
169 $returnData['new_url'] = $changedUrl;
172 file_put_contents($appDir . DIRECTORY_SEPARATOR . '.env', $envContents);
177 protected function restoreFolder(string $folderSubPath, string $appDir, string $extractDir): void
179 $fullAppFolderPath = $appDir . DIRECTORY_SEPARATOR . $folderSubPath;
180 $this->deleteDirectoryAndContents($fullAppFolderPath);
181 rename($extractDir . DIRECTORY_SEPARATOR . $folderSubPath, $fullAppFolderPath);
184 protected function deleteDirectoryAndContents(string $dir): void
186 $files = new RecursiveIteratorIterator(
187 new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
188 RecursiveIteratorIterator::CHILD_FIRST
191 foreach ($files as $fileinfo) {
192 $path = $fileinfo->getRealPath();
193 $fileinfo->isDir() ? rmdir($path) : unlink($path);
199 protected function restoreDatabase(string $appDir, string $extractDir): void
201 $dbDump = $extractDir . DIRECTORY_SEPARATOR . 'db.sql';
202 $currentEnv = EnvironmentLoader::load($appDir);
203 $mysql = MySqlRunner::fromEnvOptions($currentEnv);
205 // Drop existing tables
206 $dropSqlTempFile = tempnam(sys_get_temp_dir(), 'bs-cli-restore');
207 file_put_contents($dropSqlTempFile, $mysql->dropTablesSql());
208 $mysql->importSqlFile($dropSqlTempFile);
211 $mysql->importSqlFile($dbDump);