1
0

BackgroundCleanupUpdaterBackupsJob.php 2.6 KB

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