BackgroundCleanupUpdaterBackupsJob.php 2.6 KB

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