Skip to content

Commit 49029cc

Browse files
committed
Implemented the server part for FS#759 - Website Account Backup
1 parent c4085b8 commit 49029cc

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

server/cron_daily.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,72 @@ function setConfigVar( $filename, $varName, $varValue ) {
371371

372372
}
373373

374+
#######################################################################################################
375+
// Create website backups
376+
#######################################################################################################
377+
378+
$server_config = $app->getconf->get_server_config($conf["server_id"], 'server');
379+
$backup_dir = trim($server_config['backup_dir']);
380+
381+
if($backup_dir != '') {
382+
383+
if(!is_dir($backup_dir)) {
384+
exec("mkdir -p ".escapeshellarg($backup_dir));
385+
}
386+
387+
$sql = "SELECT * FROM web_domain WHERE type = 'vhost'";
388+
$records = $app->db->queryAllRecords($sql);
389+
if(is_array($records)) {
390+
foreach($records as $rec) {
391+
392+
// Create a backup
393+
if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'daily' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
394+
395+
$web_path = $rec['document_root'];
396+
$web_user = $rec['system_user'];
397+
$web_group = $rec['system_group'];
398+
$web_id = $rec['domain_id'];
399+
$web_backup_dir = $backup_dir.'/web'.$web_id;
400+
if(!is_dir($web_backup_dir)) mkdir($web_backup_dir);
401+
402+
exec('chown root:root '.$web_backup_dir);
403+
exec('chmod 755 '.$web_backup_dir);
404+
exec("cd ".escapeshellarg($web_path)." && sudo -u ".escapeshellarg($web_user)." find . -group ".escapeshellarg($web_group)." -print | zip -y ".escapeshellarg($web_backup_dir."/web.zip")." -@");
405+
406+
// Rename or remove old backups
407+
$backup_copies = intval($rec['backup_copies']);
408+
409+
if(is_file($web_backup_dir."/web.".$backup_copies.".zip")) unlink($web_backup_dir."/web.".$backup_copies.".zip");
410+
411+
for($n = $backup_copies - 1; $n >= 1; $n--) {
412+
if(is_file($web_backup_dir."/web.".$n.".zip")) {
413+
rename($web_backup_dir."/web.".$n.".zip",$web_backup_dir."/web.".($n+1).".zip");
414+
}
415+
}
416+
417+
if(is_file($web_backup_dir."/web.zip")) rename($web_backup_dir."/web.zip",$web_backup_dir."/web.1.zip");
418+
419+
// Create backupdir symlink
420+
if(is_link($web_path.'/backup')) unlink($web_path.'/backup');
421+
symlink($web_backup_dir,$web_path.'/backup');
422+
423+
}
424+
425+
/* If backup_interval is set to none and we have a
426+
backup directory for the website, then remove the backups */
427+
428+
if($rec['backup_interval'] == 'none') {
429+
$web_id = $rec['domain_id'];
430+
$web_user = $rec['system_user'];
431+
$web_backup_dir = realpath($backup_dir.'/web'.$web_id);
432+
if(is_dir($web_backup_dir)) {
433+
exec("sudo -u ".escapeshellarg($web_user)." rm -f ".escapeshellarg($web_backup_dir.'/*'));
434+
}
435+
}
436+
}
437+
}
438+
}
439+
374440

375441
die("finished.\n");
376442
?>

0 commit comments

Comments
 (0)