|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright 2018 Morris Jobke <[email protected]> |
| 4 | + * |
| 5 | + * @author Morris Jobke <[email protected]> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | +namespace OC\Core\BackgroundJobs; |
| 24 | + |
| 25 | +use OC\BackgroundJob\QueuedJob; |
| 26 | +use OCP\IConfig; |
| 27 | +use OCP\ILogger; |
| 28 | + |
| 29 | +class BackgroundCleanupUpdaterBackupsJob extends QueuedJob { |
| 30 | + |
| 31 | + /** @var IConfig */ |
| 32 | + protected $config; |
| 33 | + /** @var ILogger */ |
| 34 | + protected $log; |
| 35 | + |
| 36 | + public function __construct(IConfig $config, ILogger $log) { |
| 37 | + $this->config = $config; |
| 38 | + $this->log = $log; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * This job cleans up all backups except the latest 3 from the updaters backup directory |
| 43 | + * |
| 44 | + */ |
| 45 | + public function run($arguments) { |
| 46 | + $dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data'); |
| 47 | + $instanceId = $this->config->getSystemValue('instanceid', null); |
| 48 | + |
| 49 | + if(!is_string($instanceId) || empty($instanceId)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $updaterFolderPath = $dataDir . '/updater-' . $instanceId; |
| 54 | + $backupFolderPath = $updaterFolderPath . '/backups'; |
| 55 | + if(file_exists($backupFolderPath)) { |
| 56 | + $this->log->info("$backupFolderPath exists - start to clean it up"); |
| 57 | + |
| 58 | + $dirList = []; |
| 59 | + $dirs = new \DirectoryIterator($backupFolderPath); |
| 60 | + foreach($dirs as $dir) { |
| 61 | + // skip files and dot dirs |
| 62 | + if ($dir->isFile() || $dir->isDot()) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $mtime = $dir->getMTime(); |
| 67 | + $realPath = $dir->getRealPath(); |
| 68 | + |
| 69 | + if ($realPath === false) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + $dirList[$mtime] = $realPath; |
| 74 | + } |
| 75 | + |
| 76 | + ksort($dirList); |
| 77 | + // drop the newest 3 directories |
| 78 | + $dirList = array_slice($dirList, 0, -3); |
| 79 | + $this->log->info("List of all directories that will be deleted: " . json_encode($dirList)); |
| 80 | + |
| 81 | + foreach($dirList as $dir) { |
| 82 | + $this->log->info("Removing $dir ..."); |
| 83 | + \OC_Helper::rmdirr($dir); |
| 84 | + } |
| 85 | + $this->log->info("Cleanup finished"); |
| 86 | + } else { |
| 87 | + $this->log->info("Could not find updater directory $backupFolderPath - cleanup step not needed"); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments