Skip to content

Commit 66fa9bd

Browse files
author
Florian Schaal
committed
FS#3897 - backup - delete unused file (interval = none), fixed mail-backup delete, moved mail-function for failed mounts/umounts
1 parent 3fb090c commit 66fa9bd

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

server/lib/classes/cron.d/500-backup.inc.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,16 @@ public function onRunJob() {
192192
$web_user = $rec['system_user'];
193193
$web_backup_dir = realpath($backup_dir.'/web'.$web_id);
194194
if(is_dir($web_backup_dir)) {
195-
exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
196-
$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ?";
197-
$app->db->query($sql, $conf['server_id'], $web_id);
198-
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id);
195+
$dir_handle = opendir($web_backup_dir.'/');
196+
while ($file = readdir($dir_handle)) {
197+
if(!is_dir($file)) {
198+
unlink ("$web_backup_dir/"."$file");
199+
}
200+
}
199201
}
202+
$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ?";
203+
$app->db->query($sql, $conf['server_id'], $web_id);
204+
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id);
200205
}
201206
}
202207
}
@@ -353,15 +358,7 @@ public function onRunJob() {
353358
}
354359
//* end run_backups
355360
if( $server_config['backup_dir_is_mount'] == 'y' ) $app->system->umount_backup_dir($backup_dir);
356-
} else {
357-
//* send email to admin that backup directory could not be mounted
358-
$global_config = $app->getconf->get_global_config('mail');
359-
if($global_config['admin_mail'] != ''){
360-
$subject = 'Backup directory '.$backup_dir.' could not be mounted';
361-
$message = "Backup directory ".$backup_dir." could not be mounted.\n\nThe command\n\n".$server_config['backup_dir_mount_cmd']."\n\nfailed.";
362-
mail($global_config['admin_mail'], $subject, $message);
363-
}
364-
}
361+
}
365362
}
366363

367364
// delete files from backup download dir (/var/www/example.com/backup)

server/lib/classes/cron.d/500-backup_mail.inc.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function onRunJob() {
6666
if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $run_backups = false;
6767

6868
$records = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE server_id = ? AND maildir != ''", intval($conf['server_id']));
69-
7069
if(is_array($records) && $run_backups) {
7170
if(!is_dir($backup_dir)) {
7271
mkdir(escapeshellcmd($backup_dir), $backup_dir_permissions, true);
@@ -76,12 +75,13 @@ public function onRunJob() {
7675

7776
foreach($records as $rec) {
7877
//* Do the mailbox backup
78+
$email = $rec['email'];
79+
$temp = explode("@",$email);
80+
$domain = $temp[1];
81+
unset($temp);
82+
$domain_rec=$app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = ?", $domain);
83+
7984
if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
80-
$email = $rec['email'];
81-
$temp = explode("@",$email);
82-
$domain = $temp[1];
83-
unset($temp);;
84-
$domain_rec=$app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = ?", $domain);
8585

8686
$backupusername = 'root';
8787
$backupgroup = 'root';
@@ -194,23 +194,29 @@ public function onRunJob() {
194194
unset($dir_handle);
195195
}
196196
/* Remove inactive backups */
197-
if($rec['backup_interval'] == 'none') {
198-
/* remove backups from db */
199-
$sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND mailuser_id = ?";
200-
$app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id']);
201-
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id']);
197+
if($rec['backup_interval'] == 'none' || $rec['backup_interval'] == '') {
198+
202199
/* remove archives */
203-
$mail_backup_dir = $backup_dir.'/mail'.$rec['domain_id'];
200+
$mail_backup_dir = realpath($backup_dir.'/mail'.$domain_rec['domain_id']);
204201
$mail_backup_file = 'mail'.$rec['mailuser_id'].'_*';
205202
if(is_dir($mail_backup_dir)) {
206-
foreach (glob($mail_backup_dir.'/'.$mail_backup_file) as $filename) {
207-
unlink($filename);
203+
$dir_handle = opendir($mail_backup_dir.'/');
204+
while ($file = readdir($dir_handle)) {
205+
if(!is_dir($file)) {
206+
unlink ("$mail_backup_dir/"."$file");
207+
}
208208
}
209209
}
210+
/* remove backups from db */
211+
$sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND mailuser_id = ?";
212+
$app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id']);
213+
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id']);
214+
210215
}
211216
}
212217
if( $server_config['backup_dir_is_mount'] == 'y' ) $app->system->umount_backup_dir($backup_dir);
213-
} //* end run_backups
218+
//* end run_backups
219+
}
214220
}
215221

216222
parent::onRunJob();

server/lib/classes/system.inc.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,8 @@ function is_mounted($mountpoint){
18021802
}
18031803

18041804
function mount_backup_dir($backup_dir, $mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_mount.sh'){
1805+
global $app, $conf;
1806+
18051807
$mounted = true;
18061808
if ( is_file($mount_cmd) &&
18071809
is_executable($mount_cmd) &&
@@ -1813,11 +1815,22 @@ function mount_backup_dir($backup_dir, $mount_cmd = '/usr/local/ispconfig/server
18131815
if (!$this->is_mounted($backup_dir)) $mounted = false;
18141816
}
18151817
} else $mounted = false;
1818+
if (!$mounted) {
1819+
//* send email to admin that backup directory could not be mounted
1820+
$global_config = $app->getconf->get_global_config('mail');
1821+
if($global_config['admin_mail'] != ''){
1822+
$subject = 'Backup directory '.$backup_dir.' could not be mounted';
1823+
$message = "Backup directory ".$backup_dir." could not be mounted.\n\nThe command\n\n".$mount_cmd."\n\nfailed.";
1824+
mail($global_config['admin_mail'], $subject, $message);
1825+
}
1826+
}
18161827

18171828
return $mounted;
18181829
}
18191830

18201831
function umount_backup_dir($backup_dir, $mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_umount.sh'){
1832+
global $app, $conf;
1833+
18211834
if ( is_file($mount_cmd) &&
18221835
is_executable($mount_cmd) &&
18231836
fileowner($mount_cmd) === 0
@@ -1828,7 +1841,19 @@ function umount_backup_dir($backup_dir, $mount_cmd = '/usr/local/ispconfig/serve
18281841
}
18291842
}
18301843

1831-
return $this->is_mounted($backup_dir) == 0 ? true : false;
1844+
$unmounted = $this->is_mounted($backup_dir) == 0 ? true : false;
1845+
if(!$unmounted) {
1846+
//* send email to admin that backup directory could not be unmounted
1847+
$global_config = $app->getconf->get_global_config('mail');
1848+
if($global_config['admin_mail'] != ''){
1849+
$subject = 'Backup directory '.$backup_dir.' could not be unmounted';
1850+
$message = "Backup directory ".$backup_dir." could not be unmounted.\n\nThe command\n\n".$mount_cmd."\n\nfailed.";
1851+
mail($global_config['admin_mail'], $subject, $message);
1852+
}
1853+
}
1854+
1855+
return $unmounted;
1856+
18321857
}
18331858

18341859
function getinitcommand($servicename, $action, $init_script_directory = ''){

0 commit comments

Comments
 (0)