From 92e61db752c95ccbb59a03d0df8b78fbaae1a77d Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Mon, 30 Oct 2017 12:40:11 +0200 Subject: [PATCH] Cleanup: remove old backup files after upgrade --- application/Espo/Jobs/Cleanup.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/application/Espo/Jobs/Cleanup.php b/application/Espo/Jobs/Cleanup.php index f183a25e2e..1a96ff027d 100644 --- a/application/Espo/Jobs/Cleanup.php +++ b/application/Espo/Jobs/Cleanup.php @@ -49,6 +49,8 @@ class Cleanup extends \Espo\Core\Jobs\Base protected $cleanupRemindersPeriod = '15 days'; + protected $cleanupBackupPeriod = '2 month'; + public function run() { $this->cleanupJobs(); @@ -59,6 +61,7 @@ class Cleanup extends \Espo\Core\Jobs\Base $this->cleanupNotifications(); $this->cleanupActionHistory(); $this->cleanupAuthToken(); + $this->cleanupUpgradeBackups(); } protected function cleanupJobs() @@ -310,5 +313,25 @@ class Cleanup extends \Espo\Core\Jobs\Base $this->getEntityManager()->getRepository('Notification')->deleteFromDb($id); } } + + protected function cleanupUpgradeBackups() + { + $path = 'data/.backup/upgrades'; + $datetime = new \DateTime('-' . $this->cleanupBackupPeriod); + + if (file_exists($path)) { + $fileManager = $this->getContainer()->get('fileManager'); + $fileList = $fileManager->getFileList($path, false, '', false); + + foreach ($fileList as $dirName) { + $dirPath = $path . '/' . $dirName; + + $info = new \SplFileInfo($dirPath); + if ($datetime->getTimestamp() > $info->getMTime()) { + $fileManager->removeInDir($dirPath, true); + } + } + } + } }