This repository was archived by the owner on Jul 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupCommand.php
More file actions
198 lines (173 loc) · 7.85 KB
/
Copy pathBackupCommand.php
File metadata and controls
198 lines (173 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php declare(strict_types=1);
namespace Cli\Commands;
use Cli\Services\AppLocator;
use Cli\Services\EnvironmentLoader;
use Cli\Services\MySqlRunner;
use Cli\Services\Paths;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use ZipArchive;
final class BackupCommand extends Command
{
protected function configure(): void
{
$this->setName('backup');
$this->setDescription('Backup a BookStack installation to a single compressed ZIP file.');
$this->addArgument('backup-path', InputArgument::OPTIONAL, 'Outfile file or directory to store the resulting backup ZIP file.', '');
$this->addOption('no-database', null, InputOption::VALUE_NONE, "Skip adding a database dump to the backup");
$this->addOption('no-uploads', null, InputOption::VALUE_NONE, "Skip adding uploaded files to the backup");
$this->addOption('no-themes', null, InputOption::VALUE_NONE, "Skip adding the themes folder to the backup");
$this->addOption('app-directory', null, InputOption::VALUE_OPTIONAL, 'BookStack install directory to backup', '');
}
/**
* @throws CommandError
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$appDir = AppLocator::require($input->getOption('app-directory'));
$output->writeln("<info>Checking system requirements...</info>");
$this->ensureRequiredExtensionInstalled();
$handleDatabase = !$input->getOption('no-database');
$handleUploads = !$input->getOption('no-uploads');
$handleThemes = !$input->getOption('no-themes');
$suggestedOutPath = $input->getArgument('backup-path');
$zipOutFile = $this->buildZipFilePath($suggestedOutPath, $appDir);
// Create a new ZIP file
$zipTempFile = tempnam(sys_get_temp_dir(), 'bsbackup');
$dumpTempFile = '';
$zip = new ZipArchive();
$zip->open($zipTempFile, ZipArchive::OVERWRITE);
// Add default files (.env config file and this CLI if existing)
$zip->addFile(Paths::join($appDir, '.env'), '.env');
$cliPath = Paths::join($appDir, 'bookstack-system-cli');
if (file_exists($cliPath)) {
$zip->addFile($cliPath, 'bookstack-system-cli');
}
if ($handleDatabase) {
$output->writeln("<info>Dumping the database via mysqldump...</info>");
$dumpTempFile = $this->createDatabaseDump($appDir, $output);
$output->writeln("<info>Adding database dump to backup archive...</info>");
$zip->addFile($dumpTempFile, 'db.sql');
}
if ($handleUploads) {
$output->writeln("<info>Adding BookStack upload folders to backup archive...</info>");
$this->addUploadFoldersToZip($zip, $appDir);
}
if ($handleThemes) {
$output->writeln("<info>Adding BookStack theme folders to backup archive...</info>");
$this->addFolderToZipRecursive($zip, Paths::join($appDir, 'themes'), 'themes');
}
$output->writeln("<info>Saving backup archive...</info>");
// Close off our zip and move it to the required location
$zip->close();
// Delete our temporary DB dump file if exists. Must be done after zip close.
if ($dumpTempFile) {
unlink($dumpTempFile);
}
// Move the zip into the target location
rename($zipTempFile, $zipOutFile);
// Announce end
$output->writeln("<success>Backup finished.</success>");
$output->writeln("Output ZIP saved to: {$zipOutFile}");
return Command::SUCCESS;
}
/**
* Ensure the required PHP extensions are installed for this command.
* @throws CommandError
*/
protected function ensureRequiredExtensionInstalled(): void
{
if (!extension_loaded('zip')) {
throw new CommandError('The "zip" PHP extension is required to run this command');
}
}
/**
* Build a full zip path from the given suggestion, which may be empty,
* a path to a folder, or a path to a file in relative or absolute form.
* Targets the <app>/backups directory by default if existing, otherwise <app>.
* @throws CommandError
*/
protected function buildZipFilePath(string $suggestedOutPath, string $appDir): string
{
$zipDir = Paths::join($appDir, 'storage', 'backups');
$zipName = "bookstack-backup-" . date('Y-m-d-His') . '.zip';
if ($suggestedOutPath) {
$suggestedOutPath = Paths::resolve($suggestedOutPath);
if (is_dir($suggestedOutPath)) {
$zipDir = realpath($suggestedOutPath);
} else if (is_dir(dirname($suggestedOutPath))) {
$zipDir = realpath(dirname($suggestedOutPath));
$zipName = basename($suggestedOutPath);
} else {
throw new CommandError("Could not resolve provided [{$suggestedOutPath}] path to an existing folder.");
}
} else {
if (!is_dir($zipDir)) {
$zipDir = $appDir;
}
}
$fullPath = Paths::join($zipDir, $zipName);
if (file_exists($fullPath)) {
throw new CommandError("Target ZIP output location at [{$fullPath}] already exists.");
} else if (!is_dir($zipDir)) {
throw new CommandError("Target ZIP output directory [{$fullPath}] could not be found.");
} else if (!is_writable($zipDir)) {
throw new CommandError("Target ZIP output directory [{$fullPath}] is not writable.");
}
return $fullPath;
}
/**
* Add app-relative upload folders to the provided zip archive.
* Will recursively go through all directories to add all files.
*/
protected function addUploadFoldersToZip(ZipArchive $zip, string $appDir): void
{
$this->addFolderToZipRecursive($zip, Paths::join($appDir, 'public', 'uploads'), 'public/uploads');
$this->addFolderToZipRecursive($zip, Paths::join($appDir, 'storage', 'uploads'), 'storage/uploads');
}
/**
* Recursively add all contents of the given dirPath to the provided zip file
* with a zip location of the targetZipPath.
*/
protected function addFolderToZipRecursive(ZipArchive $zip, string $dirPath, string $targetZipPath): void
{
$dirIter = new RecursiveDirectoryIterator(
$dirPath,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::FOLLOW_SYMLINKS
);
$fileIter = new \RecursiveIteratorIterator($dirIter);
/** @var SplFileInfo $file */
foreach ($fileIter as $file) {
if (!$file->isDir()) {
$zip->addFile($file->getRealPath(), $targetZipPath . '/' . $fileIter->getSubPathname());
}
}
}
/**
* Create a database dump and return the path to the dumped SQL output.
* @throws CommandError
*/
protected function createDatabaseDump(string $appDir, OutputInterface $output): string
{
$envOptions = EnvironmentLoader::loadMergedWithCurrentEnv($appDir);
$mysql = MySqlRunner::fromEnvOptions($envOptions);
$mysql->ensureOptionsSet();
$dumpTempFile = tempnam(sys_get_temp_dir(), 'bsdbdump');
try {
$warnings = $mysql->runDumpToFile($dumpTempFile);
if ($warnings) {
$output->writeln("<warn>Received warnings during mysqldump:\n{$warnings}</warn>");
}
} catch (\Exception $exception) {
unlink($dumpTempFile);
throw new CommandError($exception->getMessage());
}
return $dumpTempFile;
}
}