function CleanMysqlDatabase($PURGE_ALL = false) { $filter = null; $users = new usersMenus(); $unix = new unix(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $timefile = "/etc/artica-postfix/pids/logrotate." . __FUNCTION__ . ".time"; $pid = @file_get_contents("{$pidfile}"); if ($unix->process_exists($pid, basename(__FILE__))) { system_admin_events("Already executed PID {$pid}", __FUNCTION__, __FILE__, __LINE__, "logrotate"); die; } @file_put_contents($pidfile, getmypid()); $time = $unix->file_time_min($timefile); if (!$PURGE_ALL) { if (!$GLOBALS["FORCE"]) { if ($time < 15) { events("No less than 15mn or delete {$timefile} file to force..."); system_admin_events("No less than 15mn or delete {$timefile} file", __FUNCTION__, __FILE__, __LINE__, "logrotate"); return; } } } @unlink($timefile); @file_put_contents($timefile, time()); $sock = new sockets(); $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType"); $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB"); if (!is_numeric($EnableSyslogDB)) { $EnableSyslogDB = 0; } if (!is_numeric($MySQLSyslogType)) { $MySQLSyslogType = 1; } if ($MySQLSyslogType == 0) { $MySQLSyslogType = 4; } $LogRotatePath = $sock->GET_INFO("LogRotatePath"); if ($LogRotatePath == null) { $LogRotatePath = "/home/logrotate"; } $TuningParameters = unserialize(base64_decode($sock->GET_INFO("MySQLSyslogParams"))); if ($EnableSyslogDB == 1) { if ($MySQLSyslogType == 2) { events("Is a client of remote MySQL server , aborting"); if ($GLOBALS["VERBOSE"]) { echo "Is a client of remote MySQL server , aborting\n"; } return; } } $LogRotateCompress = 1; $LogRotatePath = $sock->GET_INFO("LogRotatePath"); $SystemLogsPath = $sock->GET_INFO("SystemLogsPath"); $BackupMaxDays = $sock->GET_INFO("BackupMaxDays"); $BackupMaxDaysDir = $sock->GET_INFO("BackupMaxDaysDir"); $BackupSquidLogsUseNas = $sock->GET_INFO("BackupSquidLogsUseNas"); if ($SystemLogsPath == null) { $SystemLogsPath = "/var/log"; } $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType"); $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB"); if (!is_numeric($EnableSyslogDB)) { $EnableSyslogDB = 0; } if (!is_numeric($MySQLSyslogType)) { $MySQLSyslogType = 1; } if (!is_numeric($BackupSquidLogsUseNas)) { $BackupSquidLogsUseNas = 0; } if ($EnableSyslogDB == 1) { if ($MySQLSyslogType != 1) { return; } } if (!is_numeric($BackupMaxDays)) { $BackupMaxDays = 30; } if ($LogRotatePath == null) { $LogRotatePath = "/home/logrotate"; } if ($BackupMaxDaysDir == null) { $BackupMaxDaysDir = "/home/logrotate_backup"; } $mount = new mount("/var/log/artica-postfix/logrotate.debug"); if ($BackupSquidLogsUseNas == 1) { $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr"); $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder"); $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser"); $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword"); $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry"); if (!is_numeric($BackupSquidLogsNASRetry)) { $BackupSquidLogsNASRetry = 0; } $mountPoint = "/mnt/BackupSquidLogsUseNas"; if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) { events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}"); if ($BackupSquidLogsNASRetry == 0) { return; } sleep(3); $mount = new mount("/var/log/artica-postfix/logrotate.debug"); if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) { events("Unable to connect to NAS storage system (2): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}"); return; } } $BackupMaxDaysDir = "{$mountPoint}/artica-backup-syslog/{$users->hostname}"; } @mkdir("{$BackupMaxDaysDir}", 0755, true); if (!is_dir($BackupMaxDaysDir)) { if ($GLOBALS["VERBOSE"]) { echo "Fatal {$BackupMaxDaysDir} permission denied\n"; } events("Fatal {$BackupMaxDaysDir} permission denied"); squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__); system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate"); if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return false; } $t = time(); @file_put_contents("{$BackupMaxDaysDir}/{$t}", time()); if (!is_file("{$BackupMaxDaysDir}/{$t}")) { events("Fatal {$BackupMaxDaysDir} permission denied"); if ($GLOBALS["VERBOSE"]) { echo "Fatal {$BackupMaxDaysDir} permission denied\n"; } squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__); system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate"); if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return false; } @unlink("{$BackupMaxDaysDir}/{$t}"); if ($BackupSquidLogsUseNas == 1) { if (is_dir("/home/logrotate_backup")) { $files = $unix->DirFiles("/home/logrotate_backup"); events("Scanning the old storage systems.. " . count($files) . " file(s)"); while (list($basename, $none) = each($files)) { $filepath = "/home/logrotate_backup/{$basename}"; if ($GLOBALS["VERBOSE"]) { echo "Checking \"{$filepath}\"\n"; } $size = @filesize($filepath); if ($size < 20) { events("Removing {$filepath}"); @unlink($filepath); continue; } if (!@copy($filepath, "{$BackupMaxDaysDir}/{$basename}")) { events("copy Failed {$filepath} to \"{$BackupMaxDaysDir}/{$basename}\" permission denied..."); continue; } events("Move {$filepath} to {$BackupSquidLogsNASIpaddr} success..."); @unlink($filepath); } } } if ($PURGE_ALL == false) { $filter = "WHERE filetime<DATE_SUB(NOW(),INTERVAL {$BackupMaxDays} DAY)"; } if ($EnableSyslogDB == 1) { $q = new mysql_storelogs(); $sql = "SELECT `filename`,`hostname`,`storeid` FROM `files_info` {$filter}"; if ($GLOBALS["VERBOSE"]) { echo "{$sql}\n"; } $results = $q->QUERY_SQL($sql); if (!$q->ok) { system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__); return; } $Count = mysql_num_rows($results); $c = 0; while ($ligne = mysql_fetch_assoc($results)) { $c++; if ($GLOBALS["VERBOSE"]) { echo "{$c}/{$Count} ******** {$ligne["filename"]} {$ligne["storeid"]} *********\n"; } if (!$q->ExtractFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) { if ($GLOBALS["VERBOSE"]) { echo "{$c}/{$Count} ******** {$ligne["filename"]} ExtractFile() = FALSE !!! *********\n"; } continue; } if ($GLOBALS["VERBOSE"]) { echo "{$c}/{$Count} DelteItem({$ligne["storeid"]}) *********\n"; } $q->DelteItem($ligne["storeid"]); if ($GLOBALS["VERBOSE"]) { echo "********* EVENTS NOW --->\n"; } $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}"); if ($GLOBALS["VERBOSE"]) { echo "\n\n###### {$c}/{$Count} Continue to Next ##########\n"; } } $sql = "SELECT `filename`,`hostname`,`storeid` FROM `accesslogs` {$filter}"; if ($GLOBALS["VERBOSE"]) { echo "{$sql}\n"; } $results = $q->QUERY_SQL($sql); if (!$q->ok) { system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__); return; } $Count = mysql_num_rows($results); $c = 0; while ($ligne = mysql_fetch_assoc($results)) { $c++; if (!$q->ExtractAccessFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) { continue; } $q->DelteAccessItem($ligne["storeid"]); $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}"); } if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return; } $q = new mysql_syslog(); $sql = "SELECT `filename`,`taskid`,`filesize`,`filetime` FROM `store` {$filter}"; $results = $q->QUERY_SQL($sql); if ($GLOBALS["VERBOSE"]) { echo "{$sql} ({$q->mysql_error}) " . mysql_num_rows($results) . " file(s)\n"; } if (!$q->ok) { system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__); return; } while ($ligne = mysql_fetch_assoc($results)) { if ($GLOBALS["VERBOSE"]) { echo "Processing {$ligne["filename"]}\n"; } if (!ExtractFileFromDatabase($ligne["filename"], $BackupMaxDaysDir)) { events("Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}"); squid_admin_notifs("SYSLOG: Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}", __FUNCTION__, __FILE__, __LINE__); if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return false; } else { events("Success extracting {$ligne["filename"]} to {$BackupMaxDaysDir}"); } } if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } }