浏览代码

added keepBackupsCountCron
added function getBackupsToDelete

CauseFX 3 年之前
父节点
当前提交
742b371f8f
共有 3 个文件被更改,包括 23 次插入0 次删除
  1. 3 0
      api/classes/organizr.class.php
  2. 1 0
      api/config/default.php
  3. 19 0
      api/functions/backup-functions.php

+ 3 - 0
api/classes/organizr.class.php

@@ -2429,6 +2429,9 @@ class Organizr
 				$this->settingsOption('cron', 'autoUpdateCronSchedule'),
 				$this->settingsOption('enable', 'autoBackupCronEnabled', ['label' => 'Auto-Backup Organizr']),
 				$this->settingsOption('cron', 'autoBackupCronSchedule'),
+				$this->settingsOption('number', 'keepBackupsCountCron', ['label' => '# Backups Keep', 'help' => 'Number of backups to keep', 'attr' => 'min="1"']),
+				$this->settingsOption('blank'),
+
 			],
 			'Login' => [
 				$this->settingsOption('password', 'registrationPassword', ['label' => 'Registration Password', 'help' => 'Sets the password for the Registration form on the login screen']),

+ 1 - 0
api/config/default.php

@@ -661,6 +661,7 @@ return [
 	'autoUpdateCronSchedule' => '@weekly',
 	'autoBackupCronEnabled' => false,
 	'autoBackupCronSchedule' => '@weekly',
+	'keepBackupsCountCron' => '20',
 	'useRandomMediaImage' => false,
 	'sendLogsToSlack' => false,
 	'slackLogLevel' => 'WARNING',

+ 19 - 0
api/functions/backup-functions.php

@@ -93,6 +93,25 @@ trait BackupFunctions
 		return true;
 	}
 
+	public function getBackupsToDelete()
+	{
+		$backups = $this->getBackups();
+		if ($backups) {
+			$list = array_reverse($backups['files']);
+			$killCount = count($list) - $this->config['keepBackupsCountCron'];
+			if ($killCount >= 1) {
+				foreach ($list as $count => $backup) {
+					$count++;
+					if ($count <= $killCount) {
+						$this->log('Cron')->notice('Deleting organizr backup file as it is over limit', ['file' => $backup['name']]);
+						$this->deleteBackup($backup['name']);
+					}
+				}
+			}
+		}
+		return true;
+	}
+
 	public function getBackups()
 	{
 		$path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;