Skip to content

Commit 699049b

Browse files
authored
Merge pull request #9855 from nextcloud/feature/noid/cleanup-old-updater-backup-dirs
Background job to clean up old backups of the updater
2 parents b6457d9 + dc4c158 commit 699049b

5 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@
462462
'OC\\Contacts\\ContactsMenu\\Manager' => $baseDir . '/lib/private/Contacts/ContactsMenu/Manager.php',
463463
'OC\\Contacts\\ContactsMenu\\Providers\\EMailProvider' => $baseDir . '/lib/private/Contacts/ContactsMenu/Providers/EMailProvider.php',
464464
'OC\\Core\\Application' => $baseDir . '/core/Application.php',
465+
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => $baseDir . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
465466
'OC\\Core\\Command\\App\\CheckCode' => $baseDir . '/core/Command/App/CheckCode.php',
466467
'OC\\Core\\Command\\App\\Disable' => $baseDir . '/core/Command/App/Disable.php',
467468
'OC\\Core\\Command\\App\\Enable' => $baseDir . '/core/Command/App/Enable.php',
@@ -838,6 +839,7 @@
838839
'OC\\Remote\\User' => $baseDir . '/lib/private/Remote/User.php',
839840
'OC\\Repair' => $baseDir . '/lib/private/Repair.php',
840841
'OC\\RepairException' => $baseDir . '/lib/private/RepairException.php',
842+
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => $baseDir . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
841843
'OC\\Repair\\CleanTags' => $baseDir . '/lib/private/Repair/CleanTags.php',
842844
'OC\\Repair\\ClearFrontendCaches' => $baseDir . '/lib/private/Repair/ClearFrontendCaches.php',
843845
'OC\\Repair\\Collation' => $baseDir . '/lib/private/Repair/Collation.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
492492
'OC\\Contacts\\ContactsMenu\\Manager' => __DIR__ . '/../../..' . '/lib/private/Contacts/ContactsMenu/Manager.php',
493493
'OC\\Contacts\\ContactsMenu\\Providers\\EMailProvider' => __DIR__ . '/../../..' . '/lib/private/Contacts/ContactsMenu/Providers/EMailProvider.php',
494494
'OC\\Core\\Application' => __DIR__ . '/../../..' . '/core/Application.php',
495+
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
495496
'OC\\Core\\Command\\App\\CheckCode' => __DIR__ . '/../../..' . '/core/Command/App/CheckCode.php',
496497
'OC\\Core\\Command\\App\\Disable' => __DIR__ . '/../../..' . '/core/Command/App/Disable.php',
497498
'OC\\Core\\Command\\App\\Enable' => __DIR__ . '/../../..' . '/core/Command/App/Enable.php',
@@ -868,6 +869,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
868869
'OC\\Remote\\User' => __DIR__ . '/../../..' . '/lib/private/Remote/User.php',
869870
'OC\\Repair' => __DIR__ . '/../../..' . '/lib/private/Repair.php',
870871
'OC\\RepairException' => __DIR__ . '/../../..' . '/lib/private/RepairException.php',
872+
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
871873
'OC\\Repair\\CleanTags' => __DIR__ . '/../../..' . '/lib/private/Repair/CleanTags.php',
872874
'OC\\Repair\\ClearFrontendCaches' => __DIR__ . '/../../..' . '/lib/private/Repair/ClearFrontendCaches.php',
873875
'OC\\Repair\\Collation' => __DIR__ . '/../../..' . '/lib/private/Repair/Collation.php',

lib/private/Repair.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
namespace OC;
3232

33+
use OC\Repair\AddCleanupUpdaterBackupsJob;
3334
use OC\Repair\CleanTags;
3435
use OC\Repair\ClearFrontendCaches;
3536
use OC\Repair\Collation;
@@ -135,6 +136,7 @@ public static function getRepairSteps() {
135136
new AddLogRotateJob(\OC::$server->getJobList()),
136137
new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)),
137138
new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
139+
new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
138140
];
139141
}
140142

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 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+
24+
namespace OC\Repair;
25+
26+
use OC\Core\BackgroundJobs\BackgroundCleanupUpdaterBackupsJob;
27+
use OCP\BackgroundJob\IJobList;
28+
use OCP\Migration\IOutput;
29+
use OCP\Migration\IRepairStep;
30+
31+
class AddCleanupUpdaterBackupsJob implements IRepairStep {
32+
33+
/** @var IJobList */
34+
protected $jobList;
35+
36+
public function __construct(IJobList $jobList) {
37+
$this->jobList = $jobList;
38+
}
39+
40+
public function getName() {
41+
return 'Queue a one-time job to cleanup old backups of the updater';
42+
}
43+
44+
public function run(IOutput $output) {
45+
$this->jobList->add(BackgroundCleanupUpdaterBackupsJob::class);
46+
}
47+
}
48+

0 commit comments

Comments
 (0)