Made the prune command more picky about removing files within the backup directory.

This commit is contained in:
Abdulmhsen B. A. A.
2024-06-19 15:21:56 +03:00
parent 509d62abb0
commit adba7b7871
2 changed files with 29 additions and 7 deletions

View File

@@ -26,11 +26,11 @@ final class Backup
$list = []; $list = [];
foreach (glob($path . '/*.json') as $file) { 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 = [ $builder = [
'filename' => basename($file), 'filename' => basename($file),
'type' => $isTemp ? 'temporary' : 'permanent', 'type' => $isAuto ? 'Automatic' : 'Manual',
'size' => filesize($file), 'size' => filesize($file),
'created_at' => filectime($file), 'created_at' => filectime($file),
'modified_at' => filemtime($file), 'modified_at' => filemtime($file),

View File

@@ -77,33 +77,39 @@ final class PruneCommand extends Command
$directories = [ $directories = [
[ [
'name' => 'logs_remover',
'path' => Config::get('tmpDir') . '/logs', 'path' => Config::get('tmpDir') . '/logs',
'base' => Config::get('tmpDir'), 'base' => Config::get('tmpDir'),
'filter' => '*.log', 'filter' => '*.log',
'time' => strtotime('-7 DAYS', $time) 'time' => strtotime('-7 DAYS', $time)
], ],
[ [
'name' => 'webhooks_remover',
'path' => Config::get('tmpDir') . '/webhooks', 'path' => Config::get('tmpDir') . '/webhooks',
'base' => Config::get('tmpDir'), 'base' => Config::get('tmpDir'),
'filter' => '*.json', 'filter' => '*.json',
'time' => strtotime('-3 DAYS', $time) 'time' => strtotime('-3 DAYS', $time)
], ],
[ [
'name' => 'profiler_remover',
'path' => Config::get('tmpDir') . '/profiler', 'path' => Config::get('tmpDir') . '/profiler',
'base' => Config::get('tmpDir'), 'base' => Config::get('tmpDir'),
'filter' => '*.json', 'filter' => '*.json',
'time' => strtotime('-3 DAYS', $time) 'time' => strtotime('-3 DAYS', $time)
], ],
[ [
'name' => 'debug_remover',
'path' => Config::get('tmpDir') . '/debug', 'path' => Config::get('tmpDir') . '/debug',
'base' => Config::get('tmpDir'), 'base' => Config::get('tmpDir'),
'filter' => '*.json', 'filter' => '*.json',
'time' => strtotime('-3 DAYS', $time) 'time' => strtotime('-3 DAYS', $time)
], ],
[ [
'name' => 'backup_remover',
'path' => Config::get('path') . '/backup', 'path' => Config::get('path') . '/backup',
'base' => Config::get('path'), 'base' => Config::get('path'),
'filter' => '*.*.json', 'filter' => '*.*.json',
'validate' => fn(SplFileInfo $f): bool => 1 === @preg_match('/\w+\.\d{8}\.json/i', $f->getBasename()),
'time' => strtotime('-9 DAYS', $time) 'time' => strtotime('-9 DAYS', $time)
], ],
]; ];
@@ -111,10 +117,12 @@ final class PruneCommand extends Command
$inDryRunMode = $input->getOption('dry-run'); $inDryRunMode = $input->getOption('dry-run');
foreach ($directories as $item) { foreach ($directories as $item) {
$name = ag($item, 'name');
$path = ag($item, 'path'); $path = ag($item, 'path');
if (null === ($expiresAt = ag($item, 'time'))) { 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 'path' => $path
]); ]);
continue; continue;
@@ -122,34 +130,48 @@ final class PruneCommand extends Command
if (null === $path || !is_dir($path)) { if (null === $path || !is_dir($path)) {
if (true === (bool)ag($item, 'report', true)) { 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 'path' => $path
]); ]);
} }
continue; continue;
} }
$validate = ag($item, 'validate', null);
foreach (glob(ag($item, 'path') . '/' . ag($item, 'filter')) as $file) { foreach (glob(ag($item, 'path') . '/' . ag($item, 'filter')) as $file) {
$file = new SplFileInfo($file); $file = new SplFileInfo($file);
$fileName = $file->getBasename(); $fileName = $file->getBasename();
if ('.' === $fileName || '..' === $fileName || true === $file->isDir() || false === $file->isFile()) { 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(), 'path' => $file->getRealPath(),
]); ]);
continue; 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) { 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') . '/'), 'file' => after($file->getRealPath(), ag($item, 'base') . '/'),
'ttl' => number_format($file->getMTime() - $expiresAt), 'ttl' => number_format($file->getMTime() - $expiresAt),
]); ]);
continue; continue;
} }
$this->logger->notice('Removing [{file}].', [ $this->logger->notice("{name}: Removing '{file}'. expired TTL.", [
'name' => $name,
'file' => after($file->getRealPath(), ag($item, 'base') . '/') 'file' => after($file->getRealPath(), ag($item, 'base') . '/')
]); ]);