tbrehm
2012-01-04 5c38f2cb6efe14ec205f6b312d057bbba3d98e02
commit | author | age
313e33 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
8e9a5f 31 define('SCRIPT_PATH', dirname($_SERVER["SCRIPT_FILENAME"]));
N 32 require(SCRIPT_PATH."/lib/config.inc.php");
33 require(SCRIPT_PATH."/lib/app.inc.php");
313e33 34
T 35 set_time_limit(0);
36
37 // make sure server_id is always an int
85c4e5 38 $conf['server_id'] = intval($conf['server_id']);
313e33 39
695825 40
313e33 41 // Load required base-classes
58c210 42 $app->uses('ini_parser,file,services,getconf');
313e33 43
T 44
45 #######################################################################################################
46 // store the mailbox statistics in the database
47 #######################################################################################################
48
85c4e5 49 $sql = "SELECT mailuser_id,maildir FROM mail_user WHERE server_id = ".$conf['server_id'];
313e33 50 $records = $app->db->queryAllRecords($sql);
T 51 foreach($records as $rec) {
85c4e5 52     if(@is_file($rec['maildir'].'/ispconfig_mailsize')) {
695825 53
313e33 54         // rename file
85c4e5 55         rename($rec['maildir'].'/ispconfig_mailsize',$rec['maildir'].'/ispconfig_mailsize_save');
695825 56
313e33 57         // Read the file
85c4e5 58         $lines = file($rec['maildir'].'/ispconfig_mailsize_save');
313e33 59         $mail_traffic = 0;
T 60         foreach($lines as $line) {
61             $mail_traffic += intval($line);
62         }
63         unset($lines);
695825 64
313e33 65         // Delete backup file
85c4e5 66         if(@is_file($rec['maildir'].'/ispconfig_mailsize_save')) unlink($rec['maildir'].'/ispconfig_mailsize_save');
695825 67
313e33 68         // Save the traffic stats in the sql database
85c4e5 69         $tstamp = date('Y-m');
695825 70
85c4e5 71         $sql = "SELECT * FROM mail_traffic WHERE month = '$tstamp' AND mailuser_id = ".$rec['mailuser_id'];
a73335 72         $tr = $app->dbmaster->queryOneRecord($sql);
695825 73
85c4e5 74         $mail_traffic += $tr['traffic'];
J 75         if($tr['traffic_id'] > 0) {
76             $sql = "UPDATE mail_traffic SET traffic = $mail_traffic WHERE traffic_id = ".$tr['traffic_id'];
313e33 77         } else {
85c4e5 78             $sql = "INSERT INTO mail_traffic (month,mailuser_id,traffic) VALUES ('$tstamp',".$rec['mailuser_id'].",$mail_traffic)";
313e33 79         }
a73335 80         $app->dbmaster->query($sql);
313e33 81         echo $sql;
695825 82
313e33 83     }
695825 84
313e33 85 }
T 86
87 #######################################################################################################
88 // Create webalizer statistics
89 #######################################################################################################
90
035886 91 function setConfigVar( $filename, $varName, $varValue ) {
T 92     if($lines = @file($filename)) {
93         $out = '';
94         $found = 0;
95         foreach($lines as $line) {
1ca823 96             @list($key, $value) = preg_split('/[\t= ]+/', $line, 2);
035886 97             if($key == $varName) {
85c4e5 98                 $out .= $varName.' '.$varValue."\n";
035886 99                 $found = 1;
T 100             } else {
101                 $out .= $line;
102             }
103         }
104         if($found == 0) {
105             //* add \n if the last line does not end with \n or \r
106             if(substr($out,-1) != "\n" && substr($out,-1) != "\r") $out .= "\n";
107             //* add the new line at the end of the file
85c4e5 108             if($append == 1) $out .= $varName.' '.$varValue."\n";
035886 109         }
T 110
111         file_put_contents($filename,$out);
112     }
113 }
114
313e33 115
85c4e5 116 $sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE stats_type = 'webalizer' AND server_id = ".$conf['server_id'];
313e33 117 $records = $app->db->queryAllRecords($sql);
035886 118
313e33 119 foreach($records as $rec) {
13b41c 120     //$yesterday = date('Ymd',time() - 86400);
T 121     $yesterday = date('Ymd',strtotime("-1 day", time()));
85c4e5 122     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log');
8c034e 123     if(!@is_file($logfile)) {
85c4e5 124         $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log.gz');
58c210 125         if(!@is_file($logfile)) {
T 126             continue;
127         }
313e33 128     }
035886 129
85c4e5 130     $domain = escapeshellcmd($rec['domain']);
J 131     $statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
58c210 132     $webalizer = '/usr/bin/webalizer';
T 133     $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
85c4e5 134     $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');
d81a4c 135     
9d0baa 136     if(is_file($statsdir.'/index.php')) unlink($statsdir.'/index.php');
035886 137
58c210 138     if(!@is_file($webalizer_conf)) {
8e725d 139         copy($webalizer_conf_main,$webalizer_conf);
58c210 140     }
035886 141
58c210 142     if(@is_file($webalizer_conf)) {
T 143         setConfigVar($webalizer_conf, 'Incremental', 'yes');
144         setConfigVar($webalizer_conf, 'IncrementalName', $statsdir.'/webalizer.current');
145         setConfigVar($webalizer_conf, 'HistoryName', $statsdir.'/webalizer.hist');
146     }
8388ae 147
T 148
58c210 149     if(!@is_dir($statsdir)) mkdir($statsdir);
8c034e 150     exec("$webalizer -c $webalizer_conf -n $domain -s $domain -r $domain -q -T -p -o $statsdir $logfile");
313e33 151 }
T 152
153 #######################################################################################################
58c210 154 // Create awstats statistics
T 155 #######################################################################################################
156
85c4e5 157 $sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE stats_type = 'awstats' AND server_id = ".$conf['server_id'];
58c210 158 $records = $app->db->queryAllRecords($sql);
T 159
85c4e5 160 $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
58c210 161
T 162 foreach($records as $rec) {
13b41c 163     //$yesterday = date('Ymd',time() - 86400);
T 164     $yesterday = date('Ymd',strtotime("-1 day", time()));
85c4e5 165     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log');
58c210 166     if(!@is_file($logfile)) {
85c4e5 167         $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log.gz');
58c210 168         if(!@is_file($logfile)) {
T 169             continue;
170         }
171     }
172     
85c4e5 173     $domain = escapeshellcmd($rec['domain']);
J 174     $statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
58c210 175     $awstats_pl = $web_config['awstats_pl'];
2a6eac 176     $awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
58c210 177     
fb3a98 178     $awstats_conf_dir = $web_config['awstats_conf_dir'];
T 179     $awstats_website_conf_file = $web_config['awstats_conf_dir'].'/awstats.'.$domain.'.conf';
180     
04620b 181     if(is_file($awstats_website_conf_file)) unlink($awstats_website_conf_file);
T 182     
d860c9 183     $sql = "SELECT domain FROM web_domain WHERE (type = 'alias' OR type = 'subdomain') server_id = ".$conf['server_id'];
320e4e 184     $aliases = $app->db->queryAllRecords($sql);
T 185     $aliasdomain = '';
186     
187     if(is_array($aliases)) {
188         foreach ($aliases as $alias) {
189             $aliasdomain.= ' '.$alias['domain']. ' www.'.$alias['domain'];
190         }
191     }
192     
fb3a98 193     if(!is_file($awstats_website_conf_file)) {
T 194         $awstats_conf_file_content = 'Include "'.$awstats_conf_dir.'/awstats.conf"
04620b 195 LogFile="/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log"
fb3a98 196 SiteDomain="'.$domain.'"
320e4e 197 HostAliases="www.'.$domain.' localhost 127.0.0.1"'.$aliasdomain;
fb3a98 198         file_put_contents($awstats_website_conf_file,$awstats_conf_file_content);
T 199     }
200     
58c210 201     if(!@is_dir($statsdir)) mkdir($statsdir);
1ca823 202     if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
04620b 203     symlink($logfile,'/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
58c210 204     
ff765b 205     $awmonth = date("n");
39bc77 206     $awyear = date("Y");
ff765b 207
T 208     if (date("d") == 1) {
209         $awmonth = date("m")-1;
210         if (date("m") == 1) {
211             $awyear = date("Y")-1;
212             $awmonth = "12";
213         }
39bc77 214     }
T 215     
58c210 216     // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/web/stats -awstatsprog=/path/to/awstats.pl
39bc77 217     // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
T 218     
219     $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
220
221     if (date("d") == 2) {
222         $awmonth = date("m")-1;
223         if (date("m") == 1) {
224             $awyear = date("Y")-1;
225             $awmonth = "12";
226         }
227
228         $statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
229         mkdir($statsdirold);
230         $files = scandir($statsdir);
231         foreach ($files as $file) {
8506fc 232             if (substr($file,0,1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file,0,1) != "w" && substr($file,0,1) != "i") copy("$statsdir"."/"."$file","$statsdirold"."$file");
39bc77 233         }
T 234     }
235     
58c210 236     
2a6eac 237     if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
58c210 238         exec($command);
39bc77 239         if(is_file($rec['document_root'].'/web/stats/index.html')) unlink($rec['document_root'].'/web/stats/index.html');
T 240         rename($rec['document_root'].'/web/stats/awstats.'.$domain.'.html',$rec['document_root'].'/web/stats/awsindex.html');
241         if(!is_file($rec['document_root']."/web/stats/index.php")) copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$rec['document_root']."/web/stats/index.php");
242         
85c4e5 243         $app->log('Created awstats statistics with command: '.$command,LOGLEVEL_DEBUG);
58c210 244     } else {
2a6eac 245         $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.",LOGLEVEL_WARN);
58c210 246     }
T 247     
248 }
249
250
251 #######################################################################################################
033c80 252 // Make the web logfiles directories world readable to enable ftp access
T 253 #######################################################################################################
254
255 exec('chmod +r /var/log/ispconfig/httpd/*');
256
257 #######################################################################################################
313e33 258 // Manage and compress web logfiles
T 259 #######################################################################################################
260
85c4e5 261 $sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE server_id = ".$conf['server_id'];
313e33 262 $records = $app->db->queryAllRecords($sql);
T 263 foreach($records as $rec) {
04620b 264     $yesterday = date('Ymd',time() - 86400*2);
85c4e5 265     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log');
313e33 266     if(@is_file($logfile)) {
T 267         // Compress yesterdays logfile
268         exec("gzip -c $logfile > $logfile.gz");
b28db4 269         unlink($logfile);
313e33 270     }
3e0034 271     
T 272     // rotate and compress the error.log when it exceeds a size of 10 MB
85c4e5 273     $logfile = escapeshellcmd($rec['document_root'].'/log/error.log');
3e0034 274     if(is_file($logfile) && filesize($logfile) > 10000000) {
T 275         exec("gzip -c $logfile > $logfile.1.gz");
276         exec("cat /dev/null > $logfile");
277     }
695825 278
313e33 279     // delete logfiles after 30 days
85c4e5 280     $month_ago = date('Ymd',time() - 86400 * 30);
J 281     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$month_ago.'-access.log.gz');
313e33 282     if(@is_file($logfile)) {
T 283         unlink($logfile);
284     }
5ef60a 285     
T 286     //* Delete older Log files, in case that we missed them before due to serverdowntimes.
287     $datepart = date('Ym',time() - 86400 * 31 * 2);
288     
289     $logfile = escapeshellcmd($rec['document_root']).'/log/'.$datepart.'*-access.log.gz';
290     exec('rm -f '.$logfile);
291     
292     $logfile = escapeshellcmd($rec['document_root']).'/log/'.$datepart.'*-access.log';
293     exec('rm -f '.$logfile);
313e33 294 }
T 295
d7f14d 296 #######################################################################################################
3e0034 297 // Rotate the ispconfig.log file
T 298 #######################################################################################################
299
300 // rotate the ispconfig.log when it exceeds a size of 10 MB
e38d14 301 $logfile = $conf['ispconfig_log_dir'].'/ispconfig.log';
3e0034 302 if(is_file($logfile) && filesize($logfile) > 10000000) {
T 303     exec("gzip -c $logfile > $logfile.1.gz");
304     exec("cat /dev/null > $logfile");
305 }
306
cafdec 307 // rotate the cron.log when it exceeds a size of 10 MB
e38d14 308 $logfile = $conf['ispconfig_log_dir'].'/cron.log';
cafdec 309 if(is_file($logfile) && filesize($logfile) > 10000000) {
T 310     exec("gzip -c $logfile > $logfile.1.gz");
311     exec("cat /dev/null > $logfile");
312 }
313
3e0034 314 #######################################################################################################
2155fb 315 // Cleanup website tmp directories
T 316 #######################################################################################################
317
85c4e5 318 $sql = "SELECT domain_id, domain, document_root, system_user FROM web_domain WHERE server_id = ".$conf['server_id'];
2155fb 319 $records = $app->db->queryAllRecords($sql);
ab6140 320 $app->uses('system');
2155fb 321 if(is_array($records)) {
T 322     foreach($records as $rec){
85c4e5 323         $tmp_path = realpath(escapeshellcmd($rec['document_root'].'/tmp'));
ab6140 324         if($tmp_path != '' && strlen($tmp_path) > 10 && is_dir($tmp_path) && $app->system->is_user($rec['system_user'])){
85c4e5 325             exec('cd '.$tmp_path."; find . -mtime +1 -name 'sess_*' | grep -v -w .no_delete | xargs rm > /dev/null 2> /dev/null");
2b849b 326         }
T 327     }
328 }
2155fb 329
T 330 #######################################################################################################
e265dc 331 // Cleanup logs in master database (only the "master-server")
d7f14d 332 #######################################################################################################
313e33 333
e265dc 334 if ($app->dbmaster == $app->db) {
V 335     /** 7 days */
336     $tstamp = time() - (60*60*24*7);
313e33 337
e265dc 338     /*
V 339      *  Keep 7 days in sys_log
340      * (we can delete the old items, because if they are OK, they don't interrest anymore
341      * if they are NOT ok, the server will try to process them in 1 minute and so the
342      * error appears again after 1 minute. So it is no problem to delete the old one!
343      */
45841f 344     $sql = "DELETE FROM sys_log WHERE tstamp < " . $tstamp . " AND server_id != 0";
V 345     $app->dbmaster->query($sql);
346
347     /*
9db145 348      * Delete all remote-actions "done" and older than 7 days
V 349      * ATTENTION: We have the same problem as described in cleaning the datalog. We must not
350      * delete the last entry
45841f 351      */
9db145 352     $sql = "SELECT max(action_id) FROM sys_remoteaction";
V 353     $res = $app->dbmaster->queryOneRecord($sql);
354     $maxId = $res['max(action_id)'];
355     $sql =  "DELETE FROM sys_remoteaction " .
356             "WHERE tstamp < " . $tstamp . " " .
9f56bd 357             " AND action_state = 'ok' " .
9db145 358             " AND action_id <" . intval($maxId);
e265dc 359     $app->dbmaster->query($sql);
V 360
361     /*
362      * The sys_datalog is more difficult.
363      * 1) We have to keet ALL entries with
364      *    server_id=0, because they depend on ALL servers (even if they are not
365      *    actually in the system (and will be insered in 3 days or so).
366      * 2) We have to keey ALL entries which are not actually precessed by the
367      *    server never mind how old they are!
0061e0 368      * 3) We have to keep the entry with the highest autoinc-id, because mysql calculates the
V 369      *    autoinc-id as "new value = max(row) +1" and does not store this in a separate table.
370      *    This means, if we delete to entry with the highest autoinc-value then this value is
371      *    reused as autoinc and so there are more than one entries with the same value (over
372      *    for example 4 Weeks). This is confusing for our system.
373      *    ATTENTION 2) and 3) is in some case NOT the same! so we have to check both!
e265dc 374      */
V 375
376     /* First we need all servers and the last sys_datalog-id they processed */
377     $sql = "SELECT server_id, updated FROM server ORDER BY server_id";
378     $records = $app->dbmaster->queryAllRecords($sql);
379
0061e0 380     /* Then we need the highest value ever */
V 381     $sql = "SELECT max(datalog_id) FROM sys_datalog";
382     $res = $app->dbmaster->queryOneRecord($sql);
383     $maxId = $res['max(datalog_id)'];
384
e265dc 385     /* Then delete server by server */
V 386     foreach($records as $server) {
942538 387         $tmp_server_id = intval($server['server_id']);
T 388         if($tmp_server_id > 0) {
0061e0 389             $sql =     "DELETE FROM sys_datalog " .
V 390                     "WHERE tstamp < " . $tstamp .
942538 391                     " AND server_id = " . intval($server['server_id']) .
0061e0 392                     " AND datalog_id < " . intval($server['updated']) .
V 393                     " AND datalog_id < " . intval($maxId);
942538 394         }
e265dc 395 //        echo $sql . "\n";
V 396         $app->dbmaster->query($sql);
397     }
398 }
313e33 399
8d0c6b 400 #######################################################################################################
942538 401 // enforce traffic quota (run only on the "master-server")
8d0c6b 402 #######################################################################################################
T 403
404 if ($app->dbmaster == $app->db) {
405
406     $current_month = date('Y-m');
695825 407
8d0c6b 408     //* Check website traffic quota
9c7f61 409     $sql = "SELECT sys_groupid,domain_id,domain,traffic_quota,traffic_quota_lock FROM web_domain WHERE (traffic_quota > 0 or traffic_quota_lock = 'y') and type = 'vhost'";
8d0c6b 410     $records = $app->db->queryAllRecords($sql);
T 411     if(is_array($records)) {
412         foreach($records as $rec) {
695825 413
8d0c6b 414             $web_traffic_quota = $rec['traffic_quota'];
b0ebbd 415             $domain = $rec['domain'];
695825 416
8d0c6b 417             // get the client
T 418             /*
419             $client_group_id = $rec["sys_groupid"];
420             $client = $app->db->queryOneRecord("SELECT limit_traffic_quota,parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
421             $reseller = $app->db->queryOneRecord("SELECT limit_traffic_quota FROM client WHERE client_id = ".intval($client['parent_client_id']));
695825 422
8d0c6b 423             $client_traffic_quota = intval($client['limit_traffic_quota']);
T 424             $reseller_traffic_quota = intval($reseller['limit_traffic_quota']);
425             */
695825 426
8d0c6b 427             //* get the traffic
695825 428             $tmp = $app->db->queryOneRecord("SELECT SUM(traffic_bytes) As total_traffic_bytes FROM web_traffic WHERE traffic_date like '$current_month%' AND hostname = '$domain'");
M 429             $web_traffic = (int)$tmp['total_traffic_bytes']/1024/1024;
430
8d0c6b 431             //* Website is over quota, we will disable it
T 432             /*if( ($web_traffic_quota > 0 && $web_traffic > $web_traffic_quota) ||
433                 ($client_traffic_quota > 0 && $web_traffic > $client_traffic_quota) ||
434                 ($reseller_traffic_quota > 0 && $web_traffic > $reseller_traffic_quota)) {*/
435             if($web_traffic_quota > 0 && $web_traffic > $web_traffic_quota) {
b0ebbd 436                 $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'y',active = 'n'", 'domain_id', $rec['domain_id']);
85c4e5 437                 $app->log('Traffic quota for '.$rec['domain_id'].' exceeded. Disabling website.',LOGLEVEL_DEBUG);
8d0c6b 438             } else {
T 439                 //* unlock the website, if traffic is lower then quota
440                 if($rec['traffic_quota_lock'] == 'y') {
b0ebbd 441                     $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'n',active = 'y'", 'domain_id', $rec['domain_id']);
85c4e5 442                     $app->log('Traffic quota for '.$rec['domain_id'].' ok again. Re-enabling website.',LOGLEVEL_DEBUG);
8d0c6b 443                 }
T 444             }
445         }
446     }
695825 447
M 448
8d0c6b 449 }
T 450
b74ef5 451
T 452 #######################################################################################################
453 // deactivate virtual servers (run only on the "master-server")
454 #######################################################################################################
455
456 if ($app->dbmaster == $app->db) {
457     $current_date = date('Y-m-d');
458
459     //* Check which virtual machines have to be deactivated
460     $sql = "SELECT * FROM openvz_vm WHERE active = 'y' AND active_until_date != '0000-00-00' AND active_until_date < '$current_date'";
461     $records = $app->db->queryAllRecords($sql);
462     if(is_array($records)) {
463         foreach($records as $rec) {
464             $app->dbmaster->datalogUpdate('openvz_vm', "active = 'n'", 'vm_id', $rec['vm_id']);
465             $app->log('Virtual machine active date expired. Disabling VM '.$rec['veid'],LOGLEVEL_DEBUG);
466         }
467     }
468
469
470 }
471
49029c 472 #######################################################################################################
T 473 // Create website backups
474 #######################################################################################################
475
85c4e5 476 $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
8e725d 477 $backup_dir = $server_config['backup_dir'];
49029c 478
T 479 if($backup_dir != '') {
480     
d12a90 481     if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
T 482         $backup_dir_permissions = 0755;
483     } else {
484         $backup_dir_permissions = 0750;
485     }
486     
49029c 487     if(!is_dir($backup_dir)) {
d12a90 488         mkdir(escapeshellcmd($backup_dir), $backup_dir_permissions, true);
T 489     } else {
490         chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
49029c 491     }
T 492     
493     $sql = "SELECT * FROM web_domain WHERE type = 'vhost'";
494     $records = $app->db->queryAllRecords($sql);
495     if(is_array($records)) {
496         foreach($records as $rec) {
497             
498             // Create a backup
60bd07 499             if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
49029c 500                 
T 501                 $web_path = $rec['document_root'];
502                 $web_user = $rec['system_user'];
503                 $web_group = $rec['system_group'];
504                 $web_id = $rec['domain_id'];
505                 $web_backup_dir = $backup_dir.'/web'.$web_id;
f5ea1b 506                 if(!is_dir($web_backup_dir)) mkdir($web_backup_dir, 0750);
T 507                 chmod($web_backup_dir, 0750); 
508                 if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
509                     chown($web_backup_dir, $rec['system_user']); 
510                     chgrp($web_backup_dir, $rec['system_group']);
511                 } else {
512                     chown($web_backup_dir, 'root');
513                     chgrp($web_backup_dir, 'root');
514                 }
8e725d 515                 exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print | zip -y '.escapeshellarg($web_backup_dir.'/web.zip').' -@');
f5ea1b 516                 chown($web_backup_dir.'/web.zip', $rec['system_user']); 
T 517                 chgrp($web_backup_dir.'/web.zip', $rec['system_group']);
518                 chmod($web_backup_dir.'/web.zip', 0750);
49029c 519                 
T 520                 // Rename or remove old backups
521                 $backup_copies = intval($rec['backup_copies']);
763507 522                 
T 523                 //* delete any older backup copies that previously existed
524                 for ($n = $backup_copies; $n <= 10; $n++) {
525                     if(is_file($web_backup_dir.'/web.'.$n.'.zip')) unlink($web_backup_dir.'/web.'.$n.'.zip');
526                 }
527                 
49029c 528                 for($n = $backup_copies - 1; $n >= 1; $n--) {
8e725d 529                     if(is_file($web_backup_dir.'/web.'.$n.'.zip')) {
J 530                         rename($web_backup_dir.'/web.'.$n.'.zip',$web_backup_dir.'/web.'.($n+1).'.zip');
49029c 531                     }
T 532                 }
533             
8e725d 534                 if(is_file($web_backup_dir.'/web.zip')) rename($web_backup_dir.'/web.zip',$web_backup_dir.'/web.1.zip');
49029c 535             
T 536                 // Create backupdir symlink
537                 if(is_link($web_path.'/backup')) unlink($web_path.'/backup');
538                 symlink($web_backup_dir,$web_path.'/backup');
f5ea1b 539                 // chmod($web_path.'/backup', 0755);
T 540                 chown($web_path.'/backup', $rec['system_user']); 
541                 chgrp($web_path.'/backup', $rec['system_group']);
49029c 542                 
T 543             }
544             
545             /* If backup_interval is set to none and we have a 
546             backup directory for the website, then remove the backups */
547             
548             if($rec['backup_interval'] == 'none') {
549                 $web_id = $rec['domain_id'];
550                 $web_user = $rec['system_user'];
551                 $web_backup_dir = realpath($backup_dir.'/web'.$web_id);
552                 if(is_dir($web_backup_dir)) {
8e725d 553                     exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
49029c 554                 }
T 555             }
556         }
557     }
558 }
559
8d0c6b 560
313e33 561 die("finished.\n");
8e725d 562 ?>