2018-06-13 17:43:29 +02:00
|
|
|
<?php
|
2021-04-26 12:44:37 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-06-13 17:43:29 +02:00
|
|
|
/**
|
2024-05-10 15:09:14 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-06-13 17:43:29 +02:00
|
|
|
*/
|
|
|
|
namespace OC\Core\BackgroundJobs;
|
|
|
|
|
2022-06-28 12:55:26 +02:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\BackgroundJob\QueuedJob;
|
2018-06-13 17:43:29 +02:00
|
|
|
use OCP\IConfig;
|
2022-04-12 17:55:01 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-06-13 17:43:29 +02:00
|
|
|
|
|
|
|
class BackgroundCleanupUpdaterBackupsJob extends QueuedJob {
|
2023-06-23 23:03:56 +03:30
|
|
|
public function __construct(
|
|
|
|
protected IConfig $config,
|
|
|
|
protected LoggerInterface $log,
|
|
|
|
ITimeFactory $time,
|
|
|
|
) {
|
2022-06-28 12:55:26 +02:00
|
|
|
parent::__construct($time);
|
2018-06-13 17:43:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This job cleans up all backups except the latest 3 from the updaters backup directory
|
2023-07-06 12:39:41 +03:30
|
|
|
*
|
|
|
|
* @param array $argument
|
2018-06-13 17:43:29 +02:00
|
|
|
*/
|
2023-07-06 12:39:41 +03:30
|
|
|
public function run($argument): void {
|
2022-10-01 17:01:28 +02:00
|
|
|
$updateDir = $this->config->getSystemValue('updatedirectory', null) ?? $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data');
|
2018-06-13 17:43:29 +02:00
|
|
|
$instanceId = $this->config->getSystemValue('instanceid', null);
|
|
|
|
|
|
|
|
if (!is_string($instanceId) || empty($instanceId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-01 17:01:28 +02:00
|
|
|
$updaterFolderPath = $updateDir . '/updater-' . $instanceId;
|
2018-06-13 17:43:29 +02:00
|
|
|
$backupFolderPath = $updaterFolderPath . '/backups';
|
|
|
|
if (file_exists($backupFolderPath)) {
|
|
|
|
$this->log->info("$backupFolderPath exists - start to clean it up");
|
|
|
|
|
|
|
|
$dirList = [];
|
|
|
|
$dirs = new \DirectoryIterator($backupFolderPath);
|
|
|
|
foreach ($dirs as $dir) {
|
|
|
|
// skip files and dot dirs
|
|
|
|
if ($dir->isFile() || $dir->isDot()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mtime = $dir->getMTime();
|
|
|
|
$realPath = $dir->getRealPath();
|
|
|
|
|
|
|
|
if ($realPath === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dirList[$mtime] = $realPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($dirList);
|
|
|
|
// drop the newest 3 directories
|
|
|
|
$dirList = array_slice($dirList, 0, -3);
|
|
|
|
$this->log->info('List of all directories that will be deleted: ' . json_encode($dirList));
|
|
|
|
|
|
|
|
foreach ($dirList as $dir) {
|
|
|
|
$this->log->info("Removing $dir ...");
|
|
|
|
\OC_Helper::rmdirr($dir);
|
|
|
|
}
|
|
|
|
$this->log->info('Cleanup finished');
|
|
|
|
} else {
|
|
|
|
$this->log->info("Could not find updater directory $backupFolderPath - cleanup step not needed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|