BackgroundCleanupUpdaterBackupsJob.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 OCP\ILogger;
  29. class BackgroundCleanupUpdaterBackupsJob extends QueuedJob {
  30. /** @var IConfig */
  31. protected $config;
  32. /** @var ILogger */
  33. protected $log;
  34. public function __construct(IConfig $config, ILogger $log) {
  35. $this->config = $config;
  36. $this->log = $log;
  37. }
  38. /**
  39. * This job cleans up all backups except the latest 3 from the updaters backup directory
  40. *
  41. */
  42. public function run($arguments) {
  43. $dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data');
  44. $instanceId = $this->config->getSystemValue('instanceid', null);
  45. if (!is_string($instanceId) || empty($instanceId)) {
  46. return;
  47. }
  48. $updaterFolderPath = $dataDir . '/updater-' . $instanceId;
  49. $backupFolderPath = $updaterFolderPath . '/backups';
  50. if (file_exists($backupFolderPath)) {
  51. $this->log->info("$backupFolderPath exists - start to clean it up");
  52. $dirList = [];
  53. $dirs = new \DirectoryIterator($backupFolderPath);
  54. foreach ($dirs as $dir) {
  55. // skip files and dot dirs
  56. if ($dir->isFile() || $dir->isDot()) {
  57. continue;
  58. }
  59. $mtime = $dir->getMTime();
  60. $realPath = $dir->getRealPath();
  61. if ($realPath === false) {
  62. continue;
  63. }
  64. $dirList[$mtime] = $realPath;
  65. }
  66. ksort($dirList);
  67. // drop the newest 3 directories
  68. $dirList = array_slice($dirList, 0, -3);
  69. $this->log->info("List of all directories that will be deleted: " . json_encode($dirList));
  70. foreach ($dirList as $dir) {
  71. $this->log->info("Removing $dir ...");
  72. \OC_Helper::rmdirr($dir);
  73. }
  74. $this->log->info("Cleanup finished");
  75. } else {
  76. $this->log->info("Could not find updater directory $backupFolderPath - cleanup step not needed");
  77. }
  78. }
  79. }