From fe8ea3f1c90cfce205924b5a3d1ea6e9f7079b66 Mon Sep 17 00:00:00 2001
From: florian030 <florian@schaal-24.de>
Date: Fri, 18 May 2018 08:57:35 +0200
Subject: [PATCH] daily rotate for ispconfig-logs (ispconfig.log, cron.log,
 auth.log) up to 10 days

---
 .../lib/classes/cron.d/200-logfiles.inc.php   | 37 ++++++++++---------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/server/lib/classes/cron.d/200-logfiles.inc.php b/server/lib/classes/cron.d/200-logfiles.inc.php
index 7f3378744f..f05e4e20e2 100644
--- a/server/lib/classes/cron.d/200-logfiles.inc.php
+++ b/server/lib/classes/cron.d/200-logfiles.inc.php
@@ -175,25 +175,26 @@ class cronjob_logfiles extends cronjob {
 		// Rotate the ispconfig.log file
 		//######################################################################################################
 
-		// rotate the ispconfig.log when it exceeds a size of 10 MB
-		$logfile = $conf['ispconfig_log_dir'].'/ispconfig.log';
-		if(is_file($logfile) && filesize($logfile) > 10000000) {
-			exec("gzip -c $logfile > $logfile.1.gz");
-			exec("cat /dev/null > $logfile");
-		}
-
-		// rotate the cron.log when it exceeds a size of 10 MB
-		$logfile = $conf['ispconfig_log_dir'].'/cron.log';
-		if(is_file($logfile) && filesize($logfile) > 10000000) {
-			exec("gzip -c $logfile > $logfile.1.gz");
-			exec("cat /dev/null > $logfile");
-		}
+		$num = 10;
 
-		// rotate the auth.log when it exceeds a size of 10 MB
-		$logfile = $conf['ispconfig_log_dir'].'/auth.log';
-		if(is_file($logfile) && filesize($logfile) > 10000000) {
-			exec("gzip -c $logfile > $logfile.1.gz");
-			exec("cat /dev/null > $logfile");
+		$ispconfig_logfiles = array('ispconfig.log', 'cron.log', 'auth.log');
+		foreach($ispconfig_logfiles as $ispconfig_logfile) {
+			$ispconfig_logfile = escapeshellcmd($conf['ispconfig_log_dir'].'/'.$ispconfig_logfile);
+			// rename older files (move up by one)
+			while($num >= 1 && is_file($ispconfig_logfile . '.' . $num . '.gz')) {
+				rename($ispconfig_logfile . '.' . $num . '.gz', $ispconfig_logfile . '.' . ($num + 1) . '.gz');
+				$num--;
+			}
+			// compress current logfile
+			if(is_file($ispconfig_logfile)) {
+				exec("gzip -c $ispconfig_logfile > $ispconfig_logfile.1.gz");
+				exec("cat /dev/null > $ispconfig_logfile");
+			}
+			// remove older logs
+			while(is_file($ispconfig_logfile . '.' . $num . '.gz')) {
+				@unlink($ispconfig_logfile . '.' . $num . '.gz');
+				$num++;
+			}
 		}
 
 		//######################################################################################################
-- 
GitLab