Skip to content

Commit

Permalink
feat!: add compression to backups by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdmlln committed Dec 10, 2024
1 parent 6aa4d3c commit a0fefac
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
39 changes: 27 additions & 12 deletions Classes/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class ExportCommand extends Command
private const INPUT_INCLUDE_CACHE_DATA = 'include-cache-data';
private const INPUT_INCLUDE_DEFAULT_NO_DATA = 'include-default-no-data';
private const INPUT_OMIT_TIMESTAMP = 'omit-timestamp';
private const INPUT_NO_COMPRESSION = 'no-compression';
private const DEFAULT_NO_DATA = [
'be_sessions',
'fe_sessions',
Expand Down Expand Up @@ -52,14 +53,14 @@ protected function configure(): void
self::INPUT_DIR,
'd',
InputOption::VALUE_OPTIONAL,
'',
'Directory where backups are stored',
$this->extensionConfiguration['defaultDir'],
)
->addOption(
self::INPUT_FILE,
'f',
InputOption::VALUE_OPTIONAL,
'',
'Filename of the backup without file extension',
$this->extensionConfiguration['defaultFile'],
)
->addOption(
Expand All @@ -86,6 +87,12 @@ protected function configure(): void
null,
InputOption::VALUE_NONE,
'Omit timestamp in filename'
)
->addOption(
self::INPUT_NO_COMPRESSION,
null,
InputOption::VALUE_NONE,
'Do not compress the output file',
);
}

Expand All @@ -100,11 +107,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$dir = $input->getOption(self::INPUT_DIR);

$noCompression = $input->getOption(self::INPUT_NO_COMPRESSION);

$path = FileNamingUtility::buildPath(
$dir,
$input->getOption(self::INPUT_FILE),
!$input->getOption(self::INPUT_OMIT_TIMESTAMP),
true,
!$noCompression,
);

if (file_exists($path)) {
Expand Down Expand Up @@ -141,18 +151,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->databaseService->mysqldump(['--single-transaction', '--no-create-info', ...$ignoreTableArgs]),
];

foreach ($processes as $process) {
$exitCode = $process->run(function ($type, $buffer) use ($output, $path): void {
if ($type === Process::ERR) {
$output->writeln($buffer);
} else {
file_put_contents($path, $buffer, FILE_APPEND | LOCK_EX);
}
});
$file = $noCompression ? fopen($path, 'a') : gzopen($path, 'a');

if ($exitCode !== 0) {
return Command::FAILURE;
if ($file) {
foreach ($processes as $process) {
$exitCode = $process->run(function ($type, $buffer) use ($output, $file, $noCompression): void {
if ($type === Process::ERR) {
$output->writeln($buffer);
} else {
$noCompression ? fwrite($file, $buffer) : gzwrite($file, $buffer);
}
});

if ($exitCode !== 0) {
return Command::FAILURE;
}
}
$noCompression ? fclose($file) : gzclose($file);
}

return Command::SUCCESS;
Expand Down
20 changes: 14 additions & 6 deletions Classes/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class ImportCommand extends Command
{
private const INPUT_DIR = 'dir';
private const INPUT_FILE = 'file';
private const INPUT_NO_COMPRESSION = 'no-compression';

/**
* @var mixed[]
Expand All @@ -37,15 +38,21 @@ protected function configure(): void
self::INPUT_DIR,
'd',
InputOption::VALUE_OPTIONAL,
'',
'Directory where backups are stored',
$this->extensionConfiguration['defaultDir'],
)
->addOption(
self::INPUT_FILE,
'f',
InputOption::VALUE_OPTIONAL,
'',
'Filename of the backup without file extension',
$this->extensionConfiguration['defaultFile'],
)
->addOption(
self::INPUT_NO_COMPRESSION,
null,
InputOption::VALUE_NONE,
'Do not use compressed file',
);
}

Expand All @@ -60,8 +67,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$dir = $input->getOption(self::INPUT_DIR);
$file = $input->getOption(self::INPUT_FILE);
$noCompression = $input->getOption(self::INPUT_NO_COMPRESSION);

$path = FileNamingUtility::buildPath($dir, $file, false, true);
$path = FileNamingUtility::buildPath($dir, $file, false, true, !$noCompression);

if (!file_exists($path)) {
if (!is_dir($dir)) {
Expand All @@ -70,8 +78,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$files = array_values(array_diff(scandir($dir, SCANDIR_SORT_ASCENDING) ?: [], ['.', '..']));
$matches = array_filter($files, function (string $fileToCheck) use ($file) {
return (bool) preg_match(FileNamingUtility::getRegexPattern($file), $fileToCheck);
$matches = array_filter($files, function (string $fileToCheck) use ($file, $noCompression) {
return (bool) preg_match(FileNamingUtility::getRegexPattern($file, !$noCompression), $fileToCheck);
});
if (!empty($matches)) {
$path = FileNamingUtility::buildPath($dir, array_pop($matches));
Expand All @@ -82,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$stream = fopen($path, 'r');
$stream = $noCompression ? fopen($path, 'r') : gzopen($path, 'r');

$process = $this->databaseService->mysql($stream);

Expand Down
12 changes: 9 additions & 3 deletions Classes/Utility/FileNamingUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@

class FileNamingUtility
{
public static function getRegexPattern(string $file): string
public static function getRegexPattern(string $file, bool $gz = false): string
{
return '/^' . $file . '_\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}.sql$/';
$gzExtension = $gz ? '.gz' : '';
return '/^' . $file . '_\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}.sql' . $gzExtension . '$/';
}

public static function buildPath(
string $dir,
string $file,
?bool $appendDateTime = false,
bool $appendExtension = false
bool $appendExtension = false,
bool $gz = false,
): string {
$path = PathUtility::sanitizeTrailingSeparator($dir) . $file;
if ($appendDateTime) {
$path = $path . '_' . self::getDateTime();
}
if ($appendExtension) {
$path = $path . '.sql';

if ($gz) {
$path = $path . '.gz';
}
}
return $path;
}
Expand Down

0 comments on commit a0fefac

Please sign in to comment.