function removeall() { $unix = new unix(); $array = array(); $tables = ScanDays(true); $q = new mysql_squid_builder(); while (list($tablename, $line) = each($tables)) { $array[$tablename] = $tablename; } $sql = "SELECT table_name as c FROM information_schema.tables WHERE table_schema = 'squidlogs' AND table_name LIKE '%'"; $results = $q->QUERY_SQL($sql); if ($GLOBALS["VERBOSE"]) { echo $sql . " => " . mysql_num_rows($results) . "\n"; } while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { if (preg_match("#[0-9]+_.*?#", $ligne["c"])) { $array[$ligne["c"]] = $ligne["c"]; continue; } if (preg_match("#www_.*?#", $ligne["c"])) { $array[$ligne["c"]] = $ligne["c"]; continue; } if (preg_match("#youtube_.*?#", $ligne["c"])) { $array[$ligne["c"]] = $ligne["c"]; continue; } } while (list($tablename, $line) = each($array)) { echo "removing {$tablename}\n"; $q->QUERY_SQL("DROP TABLE `{$tablename}`"); } $Clean["tables_day"] = true; $Clean["UserAgents"] = true; $Clean["UserSizeRTT"] = true; $Clean["UserAuthDaysGrouped"] = true; $Clean["UserAuthDays"] = true; $Clean["members_uid"] = true; $Clean["tables_hours"] = true; $Clean["visited_sites"] = true; $Clean["visited_sites_catz"] = true; $Clean["visited_sites_days"] = true; $Clean["visited_sites_tot"] = true; $Clean["webcacheperfs"] = true; while (list($tablename, $line) = each($Clean)) { echo "Purge {$tablename}\n"; $q->QUERY_SQL("TRUNCATE TABLE `{$tablename}`"); } $cmd = $unix->LOCATE_PHP5_BIN() . " /usr/share/artica-postfix/exec.squid-db.php --databasesize --force --verbose >/dev/null 2>&1"; echo $cmd . "\n"; shell_exec($cmd); }
function restore($sourcefile, $nopid = false) { $unix = new unix(); $md5 = md5($sourcefile); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".{$md5}.pid"; $pid = @file_get_contents($pidfile); if ($pid < 100) { $pid = null; } $workdir = "/home/" . time(); $pass = null; if (!$nopid) { if ($unix->process_exists($pid, basename(__FILE__))) { $timepid = $unix->PROCCESS_TIME_MIN($pid); ufdbguard_admin_events("Already executed pid {$pid} since {$timepid}", __FUNCTION__, __FILE__, __LINE__, "reports"); if ($GLOBALS["VERBOSE"]) { echo "Already executed pid {$pid}\n"; } return; } @file_put_contents($pidfile, getmypid()); } $sock = new sockets(); $users = new usersMenus(); $LICENSE = 0; $mysqlbin = $unix->find_program("mysql"); $tar = $unix->find_program("tar"); $q = new mysql(); if (!$q->TABLE_EXISTS("squidlogs_restores", "artica_events")) { $q->BuildTables(); } $sql = "SELECT fullpath FROM squidlogs_restores WHERE fullpath='{$sourcefile}'"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_events")); if ($ligne["fullpath"] != null) { if ($GLOBALS["VERBOSE"]) { echo "{$sourcefile}, Already restored...\n"; return true; } } $results[] = "Compressed: " . FormatBytes(@filesize($sourcefile) / 1024); if (!is_file($mysqlbin)) { echo "mysql, no such binary\n"; ufdbguard_admin_events("mysql, no such binary", __FUNCTION__, __FILE__, __LINE__, "backup"); return; } if (!is_file($tar)) { echo "tar, no such binary\n"; ufdbguard_admin_events("tar, no such binary", __FUNCTION__, __FILE__, __LINE__, "backup"); return; } $tarok["gz"] = true; $tarok["tar"] = true; $tarok["tar.gz"] = true; $ext = $unix->file_extension($sourcefile); if (preg_match("#.tar\\.gz\$#", $sourcefile)) { $ext = "tar.gz"; } if ($GLOBALS["VERBOSE"]) { echo "{$sourcefile}, ext:{$ext}\n"; } if (!is_file($sourcefile)) { if ($GLOBALS["VERBOSE"]) { echo "{$sourcefile}, ext:{$ext} no such file\n"; } ufdbguard_admin_events("{$sourcefile}, ext:{$ext} no such file", __FUNCTION__, __FILE__, __LINE__, "backup"); return; } if ($GLOBALS["VERBOSE"]) { echo "uncompress {$sourcefile}...\n"; } if (!isset($tarok[$ext])) { if ($GLOBALS["VERBOSE"]) { echo "uncompress {$sourcefile} error, unable to understand {$ext}\n"; } ufdbguard_admin_events("uncompress {$sourcefile} error, unable to understand {$ext}", __FUNCTION__, __FILE__, __LINE__, "backup"); return; } @mkdir($workdir, 0755, true); $sourcefileSQL = "{$workdir}/" . basename($sourcefile) . ".sql"; $cmd = "{$tar} -xf {$sourcefile} --to-stdout > {$sourcefileSQL} 2>&1"; shell_exec("{$tar} -xf {$sourcefile} --to-stdout > {$sourcefileSQL} 2>&1"); $results[] = "Uncompressed: " . FormatBytes(@filesize($sourcefileSQL) / 1024); if ($GLOBALS["VERBOSE"]) { echo "uncompress {$sourcefile} to {$sourcefileSQL} done...\n"; } if (!is_file($sourcefileSQL)) { if ($GLOBALS["VERBOSE"]) { echo "uncompress to {$sourcefileSQL} error\n"; } @rmdir($workdir); ufdbguard_admin_events("uncompress {$sourcefile} error, {$sourcefileSQL} no such file", __FUNCTION__, __FILE__, __LINE__, "backup"); return; } $q = new mysql_squid_builder(true); if ($q->mysql_server == "localhost") { $q->mysql_server = "127.0.0.1"; } if (strlen($q->mysql_password) > 1) { $q->mysql_password = $unix->shellEscapeChars($q->mysql_password); $pass = "******"; } if ($q->mysql_server == "127.0.0.1") { $serv = "--protocol=socket --socket={$q->SocketName}"; } else { $serv = "--protocol=tcp --host={$q->mysql_server} --port={$q->mysql_port}"; } $sourcefileSQLT = $unix->shellEscapeChars($sourcefileSQL); $cmdline = "{$mysqlbin} --force {$serv} -u {$q->mysql_admin}{$pass} {$q->database} < {$sourcefileSQLT} 2>&1"; if ($GLOBALS["VERBOSE"]) { echo "{$cmdline}\n"; } $t = time(); exec($cmdline, $results); @unlink($sourcefileSQL); @rmdir($workdir); $took = $unix->distanceOfTimeInWords($t, time(), true); $results[] = "Took:{$took} "; $q = new mysql(); $fres = mysql_escape_string2(@implode("\n", $results)); $sourcefile = mysql_escape_string2($sourcefile); $q->QUERY_SQL("INSERT IGNORE INTO squidlogs_restores (fullpath,zDate,`results`) VALUES ('{$sourcefile}',NOW(),'{$fres}');", "artica_events"); if (!$q->ok) { ufdbguard_admin_events("{$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "backup"); return false; } if (!$nopid) { ScanDays(); } return true; }