From afee192d2a77afa1c11d25156d0a44df94e19aed Mon Sep 17 00:00:00 2001
From: A. Täffner <darkalex@firesplash.de>
Date: Sat, 23 Jan 2016 07:30:37 -0500
Subject: [PATCH] fixed the cronjob and noticed a bug in ISPC but not able to report as FlySpray is offline

---
 server/lib/classes/cron.d/500-backup.inc.php |   48 +++++++++++++++++++++++++++++++++---------------
 1 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/server/lib/classes/cron.d/500-backup.inc.php b/server/lib/classes/cron.d/500-backup.inc.php
index 87321d5..fc0619e 100644
--- a/server/lib/classes/cron.d/500-backup.inc.php
+++ b/server/lib/classes/cron.d/500-backup.inc.php
@@ -52,7 +52,7 @@
 
 		$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
 		$global_config = $app->getconf->get_global_config('sites');
-		$backup_dir = $server_config['backup_dir'];
+		$backup_dir = trim($server_config['backup_dir']);
 		$backup_mode = $server_config['backup_mode'];
 		if($backup_mode == '') $backup_mode = 'userzip';
 
@@ -117,12 +117,12 @@
 							if($backup_mode == 'userzip') {
 								//* Create a .zip backup as web user and include also files owned by apache / nginx user
 								$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.zip';
-								exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b /tmp --exclude=backup\*'.$backup_excludes.' --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
-								if($retval == 0 || $retval == 12) exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -user '.escapeshellarg($http_server_user).' -print 2> /dev/null | zip -b /tmp --exclude=backup\*'.$backup_excludes.' --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
+								exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b /tmp --exclude=./backup\*'.$backup_excludes.' --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
+								if($retval == 0 || $retval == 12) exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -user '.escapeshellarg($http_server_user).' -print 2> /dev/null | zip -b /tmp --exclude=./backup\*'.$backup_excludes.' --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
 							} else {
 								//* Create a tar.gz backup as root user
 								$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.tar.gz';
-								exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=backup\*'.$backup_excludes.' --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
+								exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=./backup\*'.$backup_excludes.' --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
 							}
 							if($retval == 0 || ($backup_mode != 'userzip' && $retval == 1) || ($backup_mode == 'userzip' && $retval == 12)) { // tar can return 1, zip can return 12(due to harmless warings) and still create valid backups  
 								if(is_file($web_backup_dir.'/'.$web_backup_file)){
@@ -192,11 +192,16 @@
 							$web_user = $rec['system_user'];
 							$web_backup_dir = realpath($backup_dir.'/web'.$web_id);
 							if(is_dir($web_backup_dir)) {
-								exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
-								$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ?";
-								$app->db->query($sql, $conf['server_id'], $web_id);
-								if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id);
+								$dir_handle = opendir($web_backup_dir.'/');
+								while ($file = readdir($dir_handle)) {
+									if(!is_dir($file)) {
+										unlink ("$web_backup_dir/"."$file");
+									}
+								}
 							}
+							$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ?";
+							$app->db->query($sql, $conf['server_id'], $web_id);
+							if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id);
 						}
 					}
 				}
@@ -353,13 +358,26 @@
 				}
 				//* end run_backups
 				if( $server_config['backup_dir_is_mount'] == 'y' ) $app->system->umount_backup_dir($backup_dir);
-			} else {
-				//* send email to admin that backup directory could not be mounted
-				$global_config = $app->getconf->get_global_config('mail');
-				if($global_config['admin_mail'] != ''){
-					$subject = 'Backup directory '.$backup_dir.' could not be mounted';
-					$message = "Backup directory ".$backup_dir." could not be mounted.\n\nThe command\n\n".$server_config['backup_dir_mount_cmd']."\n\nfailed.";
-					mail($global_config['admin_mail'], $subject, $message);
+			} 
+		}
+		
+		// delete files from backup download dir (/var/www/example.com/backup)
+		unset($records, $entry, $files);
+		$sql = "SELECT * FROM web_domain WHERE server_id = ? AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias') AND active = 'y'";
+		$records = $app->db->queryAllRecords($sql, $conf['server_id']);
+		if(is_array($records)) {
+			foreach($records as $rec) {
+				$backup_download_dir = $rec['document_root'].'/backup';
+				if(is_dir($backup_download_dir)){
+					$dir_handle = dir($backup_download_dir);
+					$files = array();
+					while (false !== ($entry = $dir_handle->read())) {
+						if($entry != '.' && $entry != '..' && is_file($backup_download_dir.'/'.$entry)) {
+							// delete files older than 3 days
+							if(time() - filemtime($backup_download_dir.'/'.$entry) >= 60*60*24*3) @unlink($backup_download_dir.'/'.$entry);
+						}
+					}
+					$dir_handle->close();
 				}
 			}
 		}

--
Gitblit v1.9.1