Newer
Older
//* send email to admin
if($global_config['admin_mail'] != '' && $mail_config['overquota_notify_admin'] == 'y') {
$recipients[] = $global_config['admin_mail'];
}
//* Send email to client
if($mail_config['overquota_notify_client'] == 'y') {
$client_group_id = $rec["sys_groupid"];
$client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
if($client['email'] != '') {
$recipients[] = $client['email'];
}
}
send_notification_email('mail_quota_ok_notification', $placeholders, $recipients);
}
continue;
}
//* Send quota notifications
// could a notification be sent?
$send_notification = false;
if(!$rec['last_quota_notification']) $send_notification = true; // not yet notified
elseif($mail_config['overquota_notify_freq'] > 0 && $rec['notified_before'] >= $mail_config['overquota_notify_freq']) $send_notification = true;
if(($mail_config['overquota_notify_admin'] == 'y' || $mail_config['overquota_notify_client'] == 'y') && $send_notification == true) {
$app->dbmaster->datalogUpdate('mail_user', "last_quota_notification = CURDATE()", 'mailuser_id', $rec['mailuser_id']);
$placeholders = array('{email}' => $rec['email'],
'{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
'{used}' => $rec['used'],
'{name}' => $rec['name'],
'{quota}' => $rec['quota'],
'{ratio}' => $rec['ratio']);
$recipients = array();
//* send email to admin
if($global_config['admin_mail'] != '' && $mail_config['overquota_notify_admin'] == 'y') {
$recipients[] = $global_config['admin_mail'];
}
//* Send email to client
if($mail_config['overquota_notify_client'] == 'y') {
$client_group_id = $rec["sys_groupid"];
$client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
if($client['email'] != '') {
$recipients[] = $client['email'];
}
}
send_notification_email('mail_quota_notification', $placeholders, $recipients);
#######################################################################################################
// deactivate virtual servers (run only on the "master-server")
#######################################################################################################
if ($app->dbmaster == $app->db) {
$current_date = date('Y-m-d');
//* Check which virtual machines have to be deactivated
$sql = "SELECT * FROM openvz_vm WHERE active = 'y' AND active_until_date != '0000-00-00' AND active_until_date < '$current_date'";
$records = $app->db->queryAllRecords($sql);
if(is_array($records)) {
foreach($records as $rec) {
$app->dbmaster->datalogUpdate('openvz_vm', "active = 'n'", 'vm_id', $rec['vm_id']);
$app->log('Virtual machine active date expired. Disabling VM '.$rec['veid'],LOGLEVEL_DEBUG);
}
}
#######################################################################################################
// Create website backups
#######################################################################################################
function formatBytes($size, $precision = 2) {
$base=log($size)/log(1024);
$suffixes=array('','k','M','G','T');
return round(pow(1024,$base-floor($base)),$precision).$suffixes[floor($base)];
}
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
$backup_dir = $server_config['backup_dir'];
tbrehm
committed
$backup_mode = $server_config['backup_mode'];
if($backup_mode == '') $backup_mode = 'userzip';
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
$http_server_user = $web_config['user'];
if($backup_dir != '') {
if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
$backup_dir_permissions = 0755;
} else {
$backup_dir_permissions = 0750;
}
if(!is_dir($backup_dir)) {
mkdir(escapeshellcmd($backup_dir), $backup_dir_permissions, true);
} else {
chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
$sql = "SELECT * FROM web_domain WHERE server_id = '".$conf['server_id']."' AND (type = 'vhost' OR type = 'vhostsubdomain') AND backup_interval != 'none'";
$records = $app->db->queryAllRecords($sql);
if(is_array($records)) {
foreach($records as $rec) {
tbrehm
committed
//* Do the website backup
if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
$web_path = $rec['document_root'];
$web_user = $rec['system_user'];
$web_group = $rec['system_group'];
$web_id = $rec['domain_id'];
$web_backup_dir = $backup_dir.'/web'.$web_id;
tbrehm
committed
if(!is_dir($web_backup_dir)) mkdir($web_backup_dir, 0750);
tbrehm
committed
//if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
tbrehm
committed
chgrp($web_backup_dir, $rec['system_group']);
/*} else {
tbrehm
committed
chown($web_backup_dir, 'root');
chgrp($web_backup_dir, 'root');
tbrehm
committed
}*/
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\* --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
if($retval == 0) 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\* --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
tbrehm
committed
} 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\* --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
tbrehm
committed
}
if($retval == 0){
chown($web_backup_dir.'/'.$web_backup_file, 'root');
chgrp($web_backup_dir.'/'.$web_backup_file, 'root');
chmod($web_backup_dir.'/'.$web_backup_file, 0750);
//* Insert web backup record in database
//$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
//$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
$app->db->query($sql);
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
} else {
if(is_file($web_backup_dir.'/'.$web_backup_file)) unlink($web_backup_dir.'/'.$web_backup_file);
}
tbrehm
committed
//* Remove old backups
$backup_copies = intval($rec['backup_copies']);
tbrehm
committed
$dir_handle = dir($web_backup_dir);
$files = array();
while (false !== ($entry = $dir_handle->read())) {
if($entry != '.' && $entry != '..' && substr($entry,0,3) == 'web' && is_file($web_backup_dir.'/'.$entry)) {
$files[] = $entry;
}
tbrehm
committed
}
tbrehm
committed
$dir_handle->close();
tbrehm
committed
rsort($files);
tbrehm
committed
for ($n = $backup_copies; $n <= 10; $n++) {
if(isset($files[$n]) && is_file($web_backup_dir.'/'.$files[$n])) {
unlink($web_backup_dir.'/'.$files[$n]);
//$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
//$tmp = $app->dbmaster->queryOneRecord($sql);
//$app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
//$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
$app->db->query($sql);
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
tbrehm
committed
unset($files);
unset($dir_handle);
tbrehm
committed
//* Remove backupdir symlink and create as directory instead
$app->uses('system');
$app->system->web_folder_protection($web_path,false);
tbrehm
committed
if(is_link($web_path.'/backup')) {
unlink($web_path.'/backup');
}
if(!is_dir($web_path.'/backup')) {
mkdir($web_path.'/backup');
tbrehm
committed
chgrp($web_path.'/backup', $rec['system_group']);
}
$app->system->web_folder_protection($web_path,true);
/* If backup_interval is set to none and we have a
backup directory for the website, then remove the backups */
if($rec['backup_interval'] == 'none') {
$web_id = $rec['domain_id'];
$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.'/*'));
}
}
}
}
tbrehm
committed
$sql = "SELECT * FROM web_database WHERE server_id = '".$conf['server_id']."' AND backup_interval != 'none'";
$records = $app->db->queryAllRecords($sql);
if(is_array($records)) {
tbrehm
committed
include('lib/mysql_clientdb.conf');
tbrehm
committed
foreach($records as $rec) {
tbrehm
committed
//* Do the database backup
if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
tbrehm
committed
$web_id = $rec['parent_domain_id'];
$db_backup_dir = $backup_dir.'/web'.$web_id;
if(!is_dir($db_backup_dir)) mkdir($db_backup_dir, 0750);
chmod($db_backup_dir, 0750);
chown($db_backup_dir, 'root');
chgrp($db_backup_dir, 'root');
tbrehm
committed
//* Do the mysql database backup with mysqldump or mongodump
tbrehm
committed
$db_id = $rec['database_id'];
$db_name = $rec['database_name'];
if ($rec['type'] == 'mysql') {
$db_backup_file = 'db_'.$db_name.'_'.date('Y-m-d_H-i').'.sql';
//$command = "mysqldump -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
$command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
exec($command, $tmp_output, $retval);
//* Compress the backup with gzip
if($retval == 0) exec("gzip -c '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file)."' > '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".gz'", $tmp_output, $retval);
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
if($retval == 0){
chmod($db_backup_dir.'/'.$db_backup_file.'.gz', 0750);
chown($db_backup_dir.'/'.$db_backup_file.'.gz', fileowner($db_backup_dir));
chgrp($db_backup_dir.'/'.$db_backup_file.'.gz', filegroup($db_backup_dir));
//* Insert web backup record in database
//$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
//$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
$app->db->query($sql);
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
} else {
if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')) unlink($db_backup_dir.'/'.$db_backup_file.'.gz');
}
//* Remove the uncompressed file
if(is_file($db_backup_dir.'/'.$db_backup_file)) unlink($db_backup_dir.'/'.$db_backup_file);
} else if ($rec['type'] == 'mongo') {
$db_backup_file = 'db_'.$db_name.'_'.date('Y-m-d_H-i');
try {
$connection = new MongoClient("mongodb://root:123456@127.0.0.1:27017/admin");
$db = $connection->selectDB($db_name);
// exclude not supported by mongodump, only get user collections
$collections = $db->getCollectionNames(false);
foreach ($collections as $collection) {
// mongodump -h 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d <db> -c <table> -o /tmp/test
$command = "mongodump -h 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d ".escapeshellcmd($db_name)." -c ".escapeshellcmd($collection)." -o ".escapeshellcmd($db_backup_dir.'/'.$db_backup_file);
exec($command);
}
if (is_dir(escapeshellcmd($db_backup_dir.'/'.$db_backup_file))) {
//* Compress the backup with gzip
exec("cd ".escapeshellcmd($db_backup_dir)." && tar -pczf ".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".tar.gz ".escapeshellcmd($db_backup_file));
chmod($db_backup_dir.'/'.$db_backup_file.'.tar.gz', 0750);
chown($db_backup_dir.'/'.$db_backup_file.'.tar.gz', fileowner($db_backup_dir));
chgrp($db_backup_dir.'/'.$db_backup_file.'.tar.gz', filegroup($db_backup_dir));
//* Insert web backup record in database
$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mongodb','rootgz',".time().",'".$app->db->quote($db_backup_file).".tar.gz')";
$app->db->query($sql);
if ($app->db->dbHost != $app->dbmaster->dbHost) {
$app->dbmaster->query($sql);
}
//* Remove the uncompressed file
exec("rm -rf ".escapeshellcmd($db_backup_dir.'/'.$db_backup_file));
}
} catch (MongoConnnectionException $e) {
// connecting to MongoDB failed - cannot create backup
}
tbrehm
committed
//* Remove old backups
$backup_copies = intval($rec['backup_copies']);
tbrehm
committed
$dir_handle = dir($db_backup_dir);
$files = array();
while (false !== ($entry = $dir_handle->read())) {
if($entry != '.' && $entry != '..' && (preg_match('/^db_(.*?)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.sql.gz$/', $entry, $matches) || preg_match('/^db_(.*?)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.tar.gz$/', $entry, $matches)) && is_file($db_backup_dir.'/'.$entry)) {
if(array_key_exists($matches[1], $files) == false) $files[$matches[1]] = array();
$files[$matches[1]][] = $entry;
tbrehm
committed
}
}
$dir_handle->close();
reset($files);
foreach($files as $db_name => $filelist) {
rsort($filelist);
for ($n = $backup_copies; $n <= 10; $n++) {
if(isset($filelist[$n]) && is_file($db_backup_dir.'/'.$filelist[$n])) {
unlink($db_backup_dir.'/'.$filelist[$n]);
//$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
//$tmp = $app->dbmaster->queryOneRecord($sql);
//$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
$app->db->query($sql);
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
}
}
}
tbrehm
committed
unset($files);
unset($dir_handle);
}
}
tbrehm
committed
unset($clientdb_host);
unset($clientdb_user);
unset($clientdb_password);
tbrehm
committed
}
tbrehm
committed
die("finished.\n");