From adba7b7871b0a6edea436fb91e3dd119eefd13f7 Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A." Date: Wed, 19 Jun 2024 15:21:56 +0300 Subject: [PATCH] Made the prune command more picky about removing files within the backup directory. --- src/API/System/Backup.php | 4 ++-- src/Commands/System/PruneCommand.php | 32 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/API/System/Backup.php b/src/API/System/Backup.php index 3ded4b08..72a30da9 100644 --- a/src/API/System/Backup.php +++ b/src/API/System/Backup.php @@ -26,11 +26,11 @@ final class Backup $list = []; foreach (glob($path . '/*.json') as $file) { - $isTemp = 1 === preg_match('/\w+\.\d+\.json/i', basename($file)); + $isAuto = 1 === preg_match('/\w+\.\d{8}\.json/i', basename($file)); $builder = [ 'filename' => basename($file), - 'type' => $isTemp ? 'temporary' : 'permanent', + 'type' => $isAuto ? 'Automatic' : 'Manual', 'size' => filesize($file), 'created_at' => filectime($file), 'modified_at' => filemtime($file), diff --git a/src/Commands/System/PruneCommand.php b/src/Commands/System/PruneCommand.php index 1fab1928..ee274614 100644 --- a/src/Commands/System/PruneCommand.php +++ b/src/Commands/System/PruneCommand.php @@ -77,33 +77,39 @@ final class PruneCommand extends Command $directories = [ [ + 'name' => 'logs_remover', 'path' => Config::get('tmpDir') . '/logs', 'base' => Config::get('tmpDir'), 'filter' => '*.log', 'time' => strtotime('-7 DAYS', $time) ], [ + 'name' => 'webhooks_remover', 'path' => Config::get('tmpDir') . '/webhooks', 'base' => Config::get('tmpDir'), 'filter' => '*.json', 'time' => strtotime('-3 DAYS', $time) ], [ + 'name' => 'profiler_remover', 'path' => Config::get('tmpDir') . '/profiler', 'base' => Config::get('tmpDir'), 'filter' => '*.json', 'time' => strtotime('-3 DAYS', $time) ], [ + 'name' => 'debug_remover', 'path' => Config::get('tmpDir') . '/debug', 'base' => Config::get('tmpDir'), 'filter' => '*.json', 'time' => strtotime('-3 DAYS', $time) ], [ + 'name' => 'backup_remover', 'path' => Config::get('path') . '/backup', 'base' => Config::get('path'), 'filter' => '*.*.json', + 'validate' => fn(SplFileInfo $f): bool => 1 === @preg_match('/\w+\.\d{8}\.json/i', $f->getBasename()), 'time' => strtotime('-9 DAYS', $time) ], ]; @@ -111,10 +117,12 @@ final class PruneCommand extends Command $inDryRunMode = $input->getOption('dry-run'); foreach ($directories as $item) { + $name = ag($item, 'name'); $path = ag($item, 'path'); if (null === ($expiresAt = ag($item, 'time'))) { - $this->logger->warning('Error No expected time to live was found for [{path}].', [ + $this->logger->warning("No expected time to live was found for '{name}' - '{path}'.", [ + 'name' => $name, 'path' => $path ]); continue; @@ -122,34 +130,48 @@ final class PruneCommand extends Command if (null === $path || !is_dir($path)) { if (true === (bool)ag($item, 'report', true)) { - $this->logger->warning('Path [{path}] not found or inaccessible.', [ + $this->logger->warning("{name}: Path '{path}' not found or is inaccessible.", [ + 'name' => $name, 'path' => $path ]); } continue; } + $validate = ag($item, 'validate', null); + foreach (glob(ag($item, 'path') . '/' . ag($item, 'filter')) as $file) { $file = new SplFileInfo($file); $fileName = $file->getBasename(); if ('.' === $fileName || '..' === $fileName || true === $file->isDir() || false === $file->isFile()) { - $this->logger->debug('Path [{path}] is not considered valid file.', [ + $this->logger->debug("{name}: Path '{path}' is not considered valid file.", [ + 'name' => $name, 'path' => $file->getRealPath(), ]); continue; } + if (null !== $validate && false === $validate($file)) { + $this->logger->debug("{name}: File '{file}' did not pass validation checks.", [ + 'name' => $name, + 'file' => after($file->getRealPath(), ag($item, 'base') . '/'), + ]); + continue; + } + if ($file->getMTime() > $expiresAt) { - $this->logger->debug('File [{file}] Not yet expired. {ttl} left seconds.', [ + $this->logger->debug("{name}: File '{file}' Not yet expired. '{ttl}' seconds left.", [ + 'name' => $name, 'file' => after($file->getRealPath(), ag($item, 'base') . '/'), 'ttl' => number_format($file->getMTime() - $expiresAt), ]); continue; } - $this->logger->notice('Removing [{file}].', [ + $this->logger->notice("{name}: Removing '{file}'. expired TTL.", [ + 'name' => $name, 'file' => after($file->getRealPath(), ag($item, 'base') . '/') ]);