BackgroundCleanupUpdaterBackupsJob.php 2.6 KB

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