Exemplo n.º 1
0
function inject_extracted_attach($filepath)
{
    if ($GLOBALS["VERBOSE"]) {
        echo "Injecting {$filepath}\n";
    }
    $unix = new unix();
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    if (!is_dir($MYSQL_DATA_DIR)) {
        @mkdir($MYSQL_DATA_DIR, 0755, true);
        @chown($MYSQL_DATA_DIR, "mysql");
        @chgrp($MYSQL_DATA_DIR, "mysql");
    }
    $basename = basename($filepath);
    $tempfile = "{$MYSQL_DATA_DIR}/{$basename}";
    $last_modified = filemtime($filepath);
    $filetime = date("Y-m-d H:i:s", $last_modified);
    $filesize = $unix->file_size($filepath);
    @copy($filepath, $tempfile);
    @chmod($tempfile, 0777);
    $q = new mysql_mimedefang_builder();
    $sql = "SELECT `filename` FROM `storage` WHERE `filename`='{$basename}'";
    $ligne = mysql_fetch_array($q->QUERY_SQL($sql));
    if (strlen(trim($ligne["filename"])) > 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Skipped {$basename}, already exists in database\n";
        }
        @unlink($filepath);
        return;
    }
    $sql = "INSERT INTO `storage` (`filename`,`filetime`,`filesize`,`filedata`) VALUES ('{$basename}','{$filetime}','{$filesize}',LOAD_FILE('{$filepath}'))";
    $q->QUERY_SQL($sql);
    if (!$q->ok) {
        system_admin_events("Fatal: {$q->mysql_error}.", __FUNCTION__, __FILE__, __LINE__, "postfix");
        $returned = false;
    } else {
        $returned = true;
        @unlink($filepath);
    }
    @unlink($tempfile);
    return $returned;
}
Exemplo n.º 2
0
function mvdb($database, $encpath)
{
    $GLOBALS["LOGSDB"] = $database;
    $logFile = "/usr/share/artica-postfix/ressources/logs/web/empty-{$GLOBALS["LOGSDB"]}.txt";
    @unlink($logFile);
    if ($database == null) {
        ouputz("No database set", __LINE__);
        return;
    }
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = @file_get_contents($pidfile);
    if ($unix->process_exists($pid)) {
        ouputz("already running PID:{$pid}", __LINE__);
        return;
    }
    $t = time();
    @file_put_contents($pidfile, getmypid());
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $path = base64_decode($encpath);
    if (!is_link("{$MYSQL_DATA_DIR}/{$database}")) {
        $original_path = "{$MYSQL_DATA_DIR}/{$database}";
    } else {
        $original_path = readlink("{$MYSQL_DATA_DIR}/{$database}");
    }
    if ($original_path == null) {
        ouputz("original_path is null, aborting ", __LINE__);
        return;
    }
    if ($path == "{$MYSQL_DATA_DIR}/{$database}") {
        ouputz("{$path} cannot be the original path `{$MYSQL_DATA_DIR}/{$database}`", __LINE__);
        return;
    }
    if ($path == "{$original_path}") {
        ouputz("{$path} cannot be the same path `{$original_path}`", __LINE__);
        return;
    }
    $chattr = $unix->find_program("chattr");
    $cp = $unix->find_program("cp");
    $mv = $unix->find_program("mv");
    $ln = $unix->find_program("ln");
    $rm = $unix->find_program("rm");
    $chmod = $unix->find_program("chmod");
    $chown = $unix->find_program("chown");
    @mkdir($path, 0755, true);
    ouputz("[0%] Mark {$MYSQL_DATA_DIR}/{$database}/* as read only", __LINE__);
    exec2("{$chattr} -R +i {$MYSQL_DATA_DIR}/{$database}");
    ouputz("[5%] copy {$MYSQL_DATA_DIR}/{$database}/* to {$path}/", __LINE__);
    exec2("{$cp} -rf \"{$MYSQL_DATA_DIR}/{$database}/\"* \"{$path}/\"");
    ouputz("[15%] Mark {$path}/* as write", __LINE__);
    exec2("{$chattr} -R -i \"{$path}\"");
    if (is_dir("{$MYSQL_DATA_DIR}/{$database}-bak")) {
        exec2("{$chattr} -R -i {$MYSQL_DATA_DIR}/{$database}-bak");
        exec2("{$rm} -rf \"{$MYSQL_DATA_DIR}/{$database}-bak\"");
    }
    ouputz("[35%] move {$MYSQL_DATA_DIR}/{$database} to {$MYSQL_DATA_DIR}/{$database}-bak", __LINE__);
    exec2("{$chattr} -R -i \"{$MYSQL_DATA_DIR}/{$database}\"");
    exec2("{$mv} \"{$MYSQL_DATA_DIR}/{$database}\" \"{$MYSQL_DATA_DIR}/{$database}-bak\"");
    if (!is_dir("{$MYSQL_DATA_DIR}/{$database}-bak")) {
        ouputz("move {$MYSQL_DATA_DIR}/{$database} to {$MYSQL_DATA_DIR}/{$database}-bak failed, roolback", __LINE__);
        exec2("{$chattr} -R -i \"{$MYSQL_DATA_DIR}/{$database}\"");
        return;
    }
    ouputz("[55%] link {$path} to {$MYSQL_DATA_DIR}/{$database}");
    exec2("{$ln} -s \"{$path}\" \"{$MYSQL_DATA_DIR}/{$database}\"");
    ouputz("[75%] remove {$MYSQL_DATA_DIR}/{$database}-bak ", __LINE__);
    exec2("{$chattr} -R -i \"{$MYSQL_DATA_DIR}/{$database}-bak\"");
    exec2("{$rm} -rf \"{$MYSQL_DATA_DIR}/{$database}-bak\"");
    if ($original_path != "{$MYSQL_DATA_DIR}/{$database}") {
        exec2("{$rm} -rf \"{$original_path}\"");
    }
    ouputz("[85%] Apply permissions on {$path}", __LINE__);
    exec2("{$chmod} -R 0755 {$path}");
    exec2("{$chown} -R mysql:mysql {$path}");
    @unlink("/usr/share/artica-postfix/ressources/logs/web/squidlogs.stats");
    ouputz("[100%] Task finish took " . $unix->distanceOfTimeInWords($t, time(), true), __LINE__);
}
Exemplo n.º 3
0
function clean_events()
{
    $q = new mysql();
    $unix = new unix();
    $rm = $unix->find_program("rm");
    $nohup = $unix->find_program("nohup");
    $TABLES = $q->LIST_TABLES_EVENTS_SYSTEM();
    while (list($tablename, $line) = each($TABLES)) {
        echo "DROP {$tablename}\n";
        $q->QUERY_SQL("DROP TABLE `{$tablename}`", "artica_events");
    }
    $datadir = $unix->MYSQL_DATA_DIR();
    shell_exec("{$rm} -f {$datadir}/artica_events/*.BAK");
    if (is_dir("{$datadir}/syslogstore")) {
        $q->DELETE_DATABASE("syslogstore");
        shell_exec("{$rm} -f {$datadir}/syslogstore/*.BAK");
    }
    $files = $unix->DirFiles("{$datadir}/artica_events", "TaskSq[0-9]+\\.MYI");
    while (list($file, $line) = each($files)) {
        $file = str_replace(".MYI", "", $file);
        $q->QUERY_SQL("DROP TABLE `{$file}`", "artica_events");
    }
    $q->QUERY_SQL("TRUNCATE TABLE `nmap_events`", "artica_events");
    $q->QUERY_SQL("TRUNCATE TABLE `nmap_events`", "artica_events");
    $q->QUERY_SQL("TRUNCATE TABLE `avgreports`", "artica_events");
    $q->QUERY_SQL("TRUNCATE TABLE `events`", "artica_events");
    $q->QUERY_SQL("TRUNCATE TABLE `dhcpd_logs`", "artica_events");
    $q->QUERY_SQL("TRUNCATE TABLE `update_events`", "artica_events");
    shell_exec("{$nohup} /etc/init.d/mysql restart --framework=" . __FILE__ . " >/dev/null 2>&1 &");
    shell_exec($unix->LOCATE_PHP5_BIN() . " /usr/share/artica-postfix/exec.mysql.start.php --engines --verbose --framework=" . __FILE__ . " 2>&1 &");
}
Exemplo n.º 4
0
function database_path()
{
    $unix = new unix();
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $database = base64_decode($_GET["database-path"]);
    if (!is_link("{$MYSQL_DATA_DIR}/{$database}")) {
        echo "<articadatascgi>" . base64_encode("{$MYSQL_DATA_DIR}/{$database}") . "</articadatascgi>";
        return;
    }
    echo "<articadatascgi>" . base64_encode(readlink("{$MYSQL_DATA_DIR}/{$database}")) . "</articadatascgi>";
}
Exemplo n.º 5
0
function ufdbguard_recompile_missing_dbs()
{
    $sock = new sockets();
    $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance");
    if (!is_numeric($EnableRemoteStatisticsAppliance)) {
        $EnableRemoteStatisticsAppliance = 0;
    }
    if ($EnableRemoteStatisticsAppliance == 1) {
        return;
    }
    $unix = new unix();
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $touch = $unix->find_program("touch");
    @mkdir("/var/lib/squidguard", 0755, true);
    $q = new mysql_squid_builder();
    $sql = "SELECT table_name as c FROM information_schema.tables WHERE table_schema = 'squidlogs' AND table_name LIKE 'category_%'";
    $results = $q->QUERY_SQL($sql);
    while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) {
        $table = $ligne["c"];
        if (!preg_match("#^category_(.+)#", $table, $re)) {
            continue;
        }
        $categoryname = $re[1];
        echo "Starting......: " . date("H:i:s") . " Webfiltering service {$table} -> {$categoryname}\n";
        if (!is_file("/var/lib/squidguard/{$categoryname}/domains")) {
            @mkdir("/var/lib/squidguard/{$categoryname}", 0755, true);
            $sql = "SELECT LOWER(pattern) FROM {$ligne["c"]} WHERE enabled=1 AND pattern REGEXP '[a-zA-Z0-9\\_\\-]+\\.[a-zA-Z0-9\\_\\-]+' ORDER BY pattern INTO OUTFILE '{$table}.temp' FIELDS OPTIONALLY ENCLOSED BY 'n'";
            $q->QUERY_SQL($sql);
            if (!is_file("{$MYSQL_DATA_DIR}/squidlogs/{$table}.temp")) {
                echo "Starting......: " . date("H:i:s") . " Webfiltering service {$MYSQL_DATA_DIR}/squidlogs/{$table}.temp no such file\n";
                continue;
            }
            echo "Starting......: " . date("H:i:s") . " Webfiltering service {$MYSQL_DATA_DIR}/squidlogs/{$table}.temp done...\n";
            @copy("{$MYSQL_DATA_DIR}/squidlogs/{$table}.temp", "/var/lib/squidguard/{$categoryname}/domains");
            @unlink("{$MYSQL_DATA_DIR}/squidlogs/{$table}.temp");
            echo "Starting......: " . date("H:i:s") . " Webfiltering service UFDBGUARD_COMPILE_SINGLE_DB(/var/lib/squidguard/{$categoryname}/domains)\n";
            UFDBGUARD_COMPILE_SINGLE_DB("/var/lib/squidguard/{$categoryname}/domains");
        } else {
            echo "Starting......: " . date("H:i:s") . " Webfiltering service /var/lib/squidguard/{$categoryname}/domains OK\n";
        }
        if (!is_file("/var/lib/squidguard/{$categoryname}/expressions")) {
            shell_exec("{$touch} /var/lib/squidguard/{$categoryname}/expressions");
        }
    }
    build();
    if (is_file("/etc/init.d/ufdb")) {
        echo "Starting......: " . date("H:i:s") . " Webfiltering service reloading service\n";
        ufdbguard_admin_events("Service will be reloaded", __FUNCTION__, __FILE__, __LINE__, "config");
        build_ufdbguard_HUP();
    }
}
Exemplo n.º 6
0
function _repair_database($database)
{
    $q = new mysql();
    $sql = "SHOW TABLES";
    $results = $q->QUERY_SQL($sql, "squidlogs");
    $unix = new unix();
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $time1 = time();
    $myisamchk = $unix->find_program("myisamchk");
    $mysqlcheck = $unix->find_program("mysqlcheck");
    $myisamchk = $unix->find_program("myisamchk");
    $pgrep = $unix->find_program("pgrep");
    exec("{$pgrep} -l -f \"{$myisamchk}\"", $results);
    while (list($index, $line) = each($results)) {
        if (preg_match("#pgrep#", $line)) {
            continue;
        }
        if (preg_match("#^[0-9]+\\s+#", $line)) {
            writelogs("{$line} already executed", @implode("\r\n", $results), __FUNCTION__, __FILE__, __LINE__);
            return;
        }
    }
    $mysqlcheck_logs = null;
    $sql = "SHOW TABLES";
    $results = $q->QUERY_SQL($sql, $database);
    if (!$q->ok) {
        system_admin_events("Maintenance on database {$database} failed {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "mysql-error");
        return;
    }
    if (mysql_num_rows($results) == 0) {
        system_admin_events("Maintenance on database {$database} aborting, no table stored", __FUNCTION__, __FILE__, __LINE__, "mysql-error");
        return;
    }
    $user = $q->mysql_admin;
    $ty[] = " --user={$user}";
    if ($q->mysql_password != null) {
        $ty[] = "--password={$q->mysql_password}";
    }
    $BLACKS["events_waits_current"] = true;
    $BLACKS["events_waits_history"] = true;
    $BLACKS["events_waits_history_long"] = true;
    $BLACKS["cond_instances"] = true;
    $BLACKS["events_waits_summary_by_instance"] = true;
    $BLACKS["events_waits_summary_by_thread_by_event_name"] = true;
    $BLACKS["events_waits_summary_global_by_event_name"] = true;
    $BLACKS["file_instances"] = true;
    $BLACKS["file_summary_by_event_name"] = true;
    $BLACKS["file_summary_by_instance"] = true;
    $BLACKS["mutex_instances"] = true;
    $BLACKS["performance_timers"] = true;
    $BLACKS["rwlock_instances"] = true;
    $BLACKS["setup_consumers"] = true;
    $BLACKS["setup_instruments"] = true;
    $BLACKS["setup_timers"] = true;
    $BLACKS["threads"] = true;
    $BLACKS["schema"] = true;
    $credentials = @implode(" ", $ty);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $table = $ligne["Tables_in_{$database}"];
        if (isset($BLACKS[$table])) {
            continue;
        }
        $tt = time();
        if (is_file($mysqlcheck)) {
            exec("{$mysqlcheck}{$credentials} -r {$database} {$table} 2>&1", $mysqlcheck_array);
            $mysqlcheck_logs = $mysqlcheck_logs . "\n{$mysqlcheck} on {$table}:\n" . @implode("\n", $mysqlcheck_array);
            unset($mysqlcheck_array);
        }
        echo $table . "\n";
        if (is_file($myisamchk)) {
            shell_exec("{$myisamchk} --safe-recover --force {$MYSQL_DATA_DIR}/{$database}/{$table}");
        } else {
            $q->REPAIR_TABLE($database, $table);
        }
        $q->QUERY_SQL("OPTIMIZE table {$table}", "{$database}");
        $time_duration = distanceOfTimeInWords($tt, time());
        $p[] = "{$database}/{$table} {$time_duration}";
    }
    $t2 = time();
    $time_duration = distanceOfTimeInWords($time1, $t2);
    system_admin_events("Maintenance on database {$database} done: took {$time_duration}\nOperations has be proceed on \n" . @implode("\n", $p) . "\nmysqlchecks results:\n{$mysqlcheck_logs}", __FUNCTION__, __FILE__, __LINE__, "mysql");
}
Exemplo n.º 7
0
function repair_all()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        echo "repair_all Loading done...\nTimeFile:{$timefile}\n";
    }
    $unix = new unix();
    if (system_is_overloaded()) {
        mysql_admin_events("Overloaded system, aborting task", __FILE__, __LINE__);
        return;
    }
    $pid = @file_get_contents($pidfile);
    if (!$GLOBALS["FORCE"]) {
        if ($pid < 100) {
            $pid = null;
        }
        if ($unix->process_exists($pid, basename(__FILE__))) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Already executed pid {$pid}\n";
            }
            return;
        }
        $timeexec = $unix->file_time_min($timefile);
        if ($timeexec < 120) {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$timeexec} <> 120...\n";
            }
            return;
        }
    }
    $q = new mysql();
    $myisamchk = $unix->find_program("myisamchk");
    $sql = "SHOW TABLE STATUS FROM `artica_backup`";
    $q = new mysql();
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $results = $q->QUERY_SQL($sql, "artica_backup");
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $TableName = $ligne["Name"];
        $ligne2 = mysql_fetch_array($q->QUERY_SQL("ANALYZE TABLE {$TableName}", "artica_backup"));
        $comment = $ligne["Comment"];
        echo "{$TableName}: {$ligne2["Msg_type"]} - {$ligne2["Msg_text"]} ({$comment})\n{$myisamchk} --safe-recover {$MYSQL_DATA_DIR}/artica_backup/{$TableName}.MYI\n";
        if ($TableName == "squid_caches_center") {
            $ligne2["Msg_type"] = "error";
        }
        if ($ligne2["Msg_type"] == "error") {
            if (is_file("{$MYSQL_DATA_DIR}/artica_backup/{$TableName}.MYI")) {
                mysql_admin_events("Repair: {$MYSQL_DATA_DIR}/artica_backup/{$TableName}.MYI", __FUNCTION__, __FILE__, __LINE__);
                shell_exec("{$myisamchk} --safe-recover {$MYSQL_DATA_DIR}/artica_backup/{$TableName}.MYI");
            }
            continue;
        }
        echo "{$TableName} -> {$comment}\n";
        if (trim($comment) == null) {
            continue;
        }
    }
    $sql = "SHOW TABLE STATUS FROM `artica_events`";
    $results = $q->QUERY_SQL($sql, "artica_events");
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $TableName = $ligne["Name"];
        $comment = $ligne["Comment"];
        $ligne2 = mysql_fetch_array($q->QUERY_SQL("ANALYZE TABLE {$TableName}", "artica_events"));
        $comment = $ligne["Comment"];
        echo "{$TableName}: {$ligne2["Msg_type"]} - {$ligne2["Msg_text"]} ({$comment})\n";
        if ($ligne2["Msg_type"] == "error") {
            if (is_file("{$MYSQL_DATA_DIR}/artica_events/{$TableName}.MYI")) {
                mysql_admin_events("Repair: {$MYSQL_DATA_DIR}/artica_events/{$TableName}.MYI", __FUNCTION__, __FILE__, __LINE__);
                shell_exec("{$myisamchk} --safe-recover {$MYSQL_DATA_DIR}/artica_events/{$TableName}.MYI");
            }
            continue;
        }
        if (trim($comment) == null) {
            continue;
        }
        if (preg_match("#Incorrect key file for table#", $comment)) {
            if (is_file("{$MYSQL_DATA_DIR}/artica_events/{$TableName}.MYI")) {
                mysql_admin_events("Repair: {$MYSQL_DATA_DIR}/artica_events/{$TableName}.MYI", __FUNCTION__, __FILE__, __LINE__);
                shell_exec("{$myisamchk} --safe-recover {$MYSQL_DATA_DIR}/artica_events/{$TableName}.MYI");
            }
        }
    }
    $sock = new sockets();
    $WORKDIR = $sock->GET_INFO("SquidStatsDatabasePath");
    if ($WORKDIR == null) {
        $WORKDIR = "/opt/squidsql";
    }
    if (is_dir($WORKDIR)) {
        $q = new mysql_squid_builder();
        $sql = "SHOW TABLE STATUS FROM `squidlogs`";
        $results = $q->QUERY_SQL($sql);
        while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
            $TableName = trim($ligne["Name"]);
            if ($TableName == null) {
                continue;
            }
            $comment = $ligne["Comment"];
            $ligne2 = mysql_fetch_array($q->QUERY_SQL("ANALYZE TABLE {$TableName}", "squidlogs"));
            if ($ligne2["Msg_type"] == "error") {
                if (is_file("{$MYSQL_DATA_DIR}/squidsql/{$TableName}.MYI")) {
                    mysql_admin_events("Repair: {$MYSQL_DATA_DIR}/squidsql/{$TableName}.MYI", __FUNCTION__, __FILE__, __LINE__);
                    shell_exec("{$myisamchk} --safe-recover {$MYSQL_DATA_DIR}/squidsql/{$TableName}.MYI");
                }
            }
            if (preg_match("#Incorrect key file for table#", $comment)) {
                if (is_file("{$MYSQL_DATA_DIR}/squidsql/{$TableName}.MYI")) {
                    mysql_admin_events("Repair: {$MYSQL_DATA_DIR}/squidsql/{$TableName}.MYI", __FUNCTION__, __FILE__, __LINE__);
                    shell_exec("{$myisamchk} --safe-recover {$MYSQL_DATA_DIR}/squidsql/{$TableName}.MYI");
                }
            }
        }
    } else {
        echo "{$WORKDIR} no such dir\n";
    }
    optimize(true);
}
function remove_database()
{
    $q = new mysql();
    $unix = new unix();
    $sock = new sockets();
    $ZarafaDedicateMySQLServer = $sock->GET_INFO("ZarafaDedicateMySQLServer");
    if (!is_numeric($ZarafaDedicateMySQLServer)) {
        $ZarafaDedicateMySQLServer = 0;
    }
    if ($ZarafaDedicateMySQLServer == 1) {
        shell_exec($unix->LOCATE_PHP5_BIN() . " /usr/share/artica-postfix/exec.zarafa-db.php --remove-database");
        return;
    }
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $q->DELETE_DATABASE("zarafa");
    if (!$q->ok) {
        echo "Error while removing zarafa database...{$q->mysql_error}\n";
        return;
    }
    if (!is_dir($MYSQL_DATA_DIR)) {
        echo "Failed to locate {$MYSQL_DATA_DIR}\n";
        return;
    }
    if (is_file("/etc/artica-postfix/ZARFA_FIRST_INSTALL")) {
        @unlink("/etc/artica-postfix/ZARFA_FIRST_INSTALL");
    }
    $kill = $unix->find_program("kill");
    $pidof = $unix->find_program("pidof");
    $zarafa_server = $unix->find_program("zarafa-server");
    shell_exec("{$kill} -9 `{$pidof} {$zarafa_server}` >/dev/null 2>&1");
    echo "Starting zarafa..............: remove {$MYSQL_DATA_DIR}/ib_logfile*\n";
    shell_exec("/bin/rm -f {$MYSQL_DATA_DIR}/ib_logfile*");
    shell_exec("/bin/rm -f {$MYSQL_DATA_DIR}/ibdata*");
    echo "Starting zarafa..............: remove {$MYSQL_DATA_DIR}/zarafa*\n";
    shell_exec("/bin/rm -rf {$MYSQL_DATA_DIR}/zarafa");
    echo "Starting zarafa..............: restart MySQL\n";
    mysql_admin_mysql(1, "Restarting MySQL service...", null, __FILE__, __LINE__);
    shell_exec("/etc/init.d/mysql restart --force --framework=" . __FILE__ . " >/tmp/zarafa_removedb 2>&1");
    echo "Starting zarafa..............: restart Zarafa server\n";
    shell_exec("/etc/init.d/zarafa-server restart >>/tmp/zarafa_removedb 2>&1");
    $unix->send_email_events("Success removing zarafa databases", "removed {$MYSQL_DATA_DIR}/ib_logfile*\nremoved {$MYSQL_DATA_DIR}/ibdata*\nremoved {$MYSQL_DATA_DIR}/zarafa\n\n" . @file_get_contents("/tmp/zarafa_removedb"), "mailbox");
}
Exemplo n.º 9
0
function rebuild_database($nollop = false)
{
    $unix = new unix();
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    echo "Starting......: " . date("H:i:s") . " PowerDNS destroy database and recreate it\n";
    $q = new mysql();
    $q->DELETE_DATABASE("powerdns");
    $rm = $unix->find_program("rm");
    if (is_dir("{$MYSQL_DATA_DIR}/powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS removing {$MYSQL_DATA_DIR}/powerdns\n";
        shell_exec("{$rm} -rf {$MYSQL_DATA_DIR}/powerdns");
    }
    checkMysql($nollop);
    shell_exec("/etc/init.d/artica-postfix restart pdns");
}
Exemplo n.º 10
0
function start($skipGrant = false)
{
    if (is_file("/etc/artica-postfix/FROM_ISO")) {
        if (!is_file("/etc/artica-postfix/artica-iso-setup-launched")) {
            return;
        }
    }
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/squiddbstart.pid";
    $sock = new sockets();
    $WORKDIR = $sock->GET_INFO("SquidStatsDatabasePath");
    if ($WORKDIR == null) {
        $WORKDIR = "/opt/squidsql";
    }
    $SERV_NAME = "squid-db";
    $pid = $unix->get_pid_from_file($pidfile);
    $sock = new sockets();
    $nohup = $unix->find_program("nohup");
    $php5 = $unix->LOCATE_PHP5_BIN();
    $lnbin = $unix->find_program("ln");
    if (!$GLOBALS["FORCE"]) {
        if (!$GLOBALS["NOPID"]) {
            if ($unix->process_exists($pid, basename(__FILE__))) {
                $time = $unix->PROCCESS_TIME_MIN($pid);
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: Starting Task Already running PID {$pid} since {$time}mn\n";
                }
                return;
            }
        }
    }
    @file_put_contents($pidfile, getmypid());
    $GetStartedValues = GetStartedValues();
    $sock = new sockets();
    $ProxyUseArticaDB = $sock->GET_INFO("ProxyUseArticaDB");
    $DisableArticaProxyStatistics = $sock->GET_INFO("DisableArticaProxyStatistics");
    if (!is_numeric($ProxyUseArticaDB)) {
        $ProxyUseArticaDB = 0;
    }
    if (!is_numeric($DisableArticaProxyStatistics)) {
        $DisableArticaProxyStatistics = 0;
    }
    if (!is_dir($WORKDIR)) {
        @mkdir($WORKDIR, 0755, true);
    }
    if ($ProxyUseArticaDB == 0) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]:{$SERV_NAME} is disabled...\n";
        }
        stop();
        return;
    }
    $mysqld = $unix->find_program("mysqld");
    if (!is_file($mysqld)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]:{$SERV_NAME} is not installed...\n";
        }
        return;
    }
    $pid = SQUIDDB_PID();
    if ($unix->process_exists($pid)) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} MySQL Database Engine already running pid {$pid} since {$time}mn\n";
        }
        return;
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} writing init.d\n";
    }
    initd();
    $memory = get_memory();
    $swap = get_swap();
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} Server available memory `{$memory}MB`\n";
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} Server available swap `{$swap}MB`\n";
    }
    $SquidDBTuningParameters = unserialize(base64_decode($sock->GET_INFO("SquidDBTuningParameters")));
    $ListenPort = $SquidDBTuningParameters["ListenPort"];
    if (!is_numeric($ListenPort)) {
        $ListenPort = 0;
    }
    if ($ListenPort == 0) {
        $ListenPort = rand(8900, 45890);
        $SquidDBTuningParameters["ListenPort"] = $ListenPort;
        $sock->SET_INFO("SquidDBTuningParameters", base64_encode(serialize($SquidDBTuningParameters)));
    }
    @mkdir($WORKDIR, 0755, true);
    $mysqlserv = new mysql_services();
    $mysqlserv->WORKDIR = $WORKDIR;
    $mysqlserv->MYSQL_PID_FILE = "/var/run/squid-db.pid";
    $mysqlserv->MYSQL_SOCKET = "/var/run/mysqld/squid-db.sock";
    $mysqlserv->SERV_NAME = $SERV_NAME;
    $mysqlserv->TokenParams = "SquidDBTuningParameters";
    $mysqlserv->INSTALL_DATABASE = true;
    $mysqlserv->MYSQL_BIN_DAEMON_PATH = $unix->find_program("mysqld");
    //$mysqlserv->MYSQL_ERRMSG=$GLOBALS["MYSQL_ERRMSG"];
    $mysqlserv->InnoDB = false;
    $cmdline = $mysqlserv->BuildParams();
    $CREATEDB = false;
    if (!is_file("{$WORKDIR}/my.cnf")) {
        @file_put_contents("{$WORKDIR}/my.cnf", "\n");
    }
    if (!is_file("{$WORKDIR}/bin/my_print_defaults")) {
        $my_print_defaults = $unix->find_program("my_print_defaults");
        shell_exec("{$lnbin} -s {$my_print_defaults} {$WORKDIR}/bin/my_print_defaults");
    }
    if (!is_file("{$WORKDIR}/data/mysql/user.MYD")) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} Installing defaults databases, Please Wait...\n";
        }
        install_db($WORKDIR);
        $CREATEDB = true;
    }
    $topCopyMysql["host.frm"] = true;
    $topCopyMysql["host.MYD"] = true;
    $topCopyMysql["host.MYI"] = true;
    $topCopyMysql["servers.frm"] = true;
    $topCopyMysql["servers.MYD"] = true;
    $topCopyMysql["servers.MYI"] = true;
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $topCopyMysqlForce["tables_priv.frm"] = true;
    $topCopyMysqlForce["tables_priv.MYD"] = true;
    $topCopyMysqlForce["tables_priv.MYI"] = true;
    $topCopyMysqlForce["columns_priv.frm"] = true;
    $topCopyMysqlForce["columns_priv.MYD"] = true;
    $topCopyMysqlForce["columns_priv.MYI"] = true;
    $topCopyMysqlForce["procs_priv.frm"] = true;
    $topCopyMysqlForce["procs_priv.MYD"] = true;
    $topCopyMysqlForce["procs_priv.MYI"] = true;
    $topCopyMysqlForce["plugin.frm"] = true;
    $topCopyMysqlForce["plugin.MYD"] = true;
    $topCopyMysqlForce["plugin.MYI"] = true;
    $topCopyMysqlForce["user.frm"] = true;
    $topCopyMysqlForce["user.MYD"] = true;
    $topCopyMysqlForce["user.MYI"] = true;
    $topCopyMysqlForce["db.frm"] = true;
    $topCopyMysqlForce["db.MYD"] = true;
    $topCopyMysqlForce["db.MYI"] = true;
    $ToCopyForce = false;
    while (list($filename, $ligne) = each($topCopyMysql)) {
        if (!is_file("{$WORKDIR}/data/mysql/{$filename}")) {
            $ToCopyForce = true;
            if (is_file("{$MYSQL_DATA_DIR}/mysql/{$filename}")) {
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: Installing {$filename}\n";
                }
                @copy("{$MYSQL_DATA_DIR}/mysql/{$filename}", "{$WORKDIR}/data/mysql/{$filename}");
                $CREATEDB = true;
            }
        }
    }
    while (list($filename, $ligne) = each($topCopyMysqlForce)) {
        if (!is_file("{$WORKDIR}/data/mysql/{$filename}")) {
            if (is_file("{$MYSQL_DATA_DIR}/mysql/{$filename}")) {
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: Installing {$filename}\n";
                }
                @copy("{$MYSQL_DATA_DIR}/mysql/{$filename}", "{$WORKDIR}/data/mysql/{$filename}");
            }
        }
    }
    @mkdir("{$WORKDIR}/share/mysql/english", 0755, true);
    $Get_errmsgsys = Get_errmsgsys();
    if (!is_file("{$WORKDIR}/share/mysql/english/errmsg.sys")) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: Creating errmsg.sys\n";
        }
        ini_set('error_reporting', E_ALL);
        ini_set('error_prepend_string', null);
        ini_set('error_append_string', null);
        if (is_file($Get_errmsgsys)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: copy {$Get_errmsgsys} -> {$WORKDIR}/share/mysql/english/errmsg.sys\n";
            }
            copy(Get_errmsgsys(), "{$WORKDIR}/share/mysql/english/errmsg.sys");
        } else {
            file_put_contents("{$WORKDIR}/share/mysql/english/errmsg.sys", "\n");
        }
    } else {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: errmsg.sys OK\n";
        }
    }
    $TMP = $unix->FILE_TEMP();
    $nohup = $unix->find_program("nohup");
    if ($GLOBALS["VERBOSE"]) {
        echo $cmdline . "\n";
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: Starting MySQL daemon ({$SERV_NAME})\n";
    }
    @unlink("{$WORKDIR}/error.log");
    shell_exec("{$nohup} {$cmdline} >{$TMP} 2>&1 &");
    sleep(1);
    for ($i = 0; $i < 5; $i++) {
        $pid = SQUIDDB_PID();
        if ($unix->process_exists($pid)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon ({$SERV_NAME}) started pid .{$pid}..\n";
            }
            break;
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon wait {$i}/5\n";
        }
        sleep(1);
    }
    sleep(1);
    $pid = SQUIDDB_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon ({$SERV_NAME}) failed to start\n";
        }
        if (is_file($TMP)) {
            $f = explode("\n", @file_get_contents($TMP));
            while (list($num, $ligne) = each($f)) {
                if (trim($ligne) == null) {
                    continue;
                }
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: {$ligne}\n";
                }
            }
        }
        $f = explode("\n", @file_get_contents("{$WORKDIR}/error.log"));
        while (list($num, $ligne) = each($f)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL Results \"{$ligne}\"\n";
            }
            if (preg_match("#Incorrect information in file: './mysql/proxies_priv.frm'#", $ligne)) {
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: remove MySQL tables and install again...\n";
                }
                shell_exec("/bin/rm -rf {$WORKDIR}/data/mysql/*");
                shell_exec("{$nohup} {$php5} " . __FILE__ . " --start --recall >/dev/ null 2>&1 &");
                return;
            }
        }
    } else {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon ({$SERV_NAME}) success\n";
        }
        if ($CREATEDB) {
            $q = new mysql_squid_builder();
            $q->CheckTables();
        }
        $q = new mysql_squid_builder();
        $q->MEMORY_TABLES_RESTORE();
    }
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$cmdline}\n";
        }
    }
    $unix->THREAD_COMMAND_SET($unix->LOCATE_PHP5_BIN() . " " . __FILE__ . " --databasesize");
}
Exemplo n.º 11
0
function build_report($ID, $nopid = false)
{
    if (!is_numeric($ID)) {
        ufdbguard_admin_events("Not a numeric ID", __FUNCTION__, __FILE__, __LINE__, "reports");
        return;
    }
    $t = time();
    $unix = new unix();
    $tablename = "WebTrackMem{$ID}";
    $tableBlock = "WebTrackMeB{$ID}";
    if (!$nopid) {
        $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".{$ID}.pid";
        $pid = @file_get_contents($pidfile);
        if ($pid < 100) {
            $pid = null;
        }
        if ($unix->process_exists($pid, basename(__FILE__))) {
            ufdbguard_admin_events("Already executed pid {$pid}", __FUNCTION__, __FILE__, __LINE__, "reports");
            if ($GLOBALS["VERBOSE"]) {
                echo "Already executed pid {$pid}\n";
            }
            return;
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Building report {$ID}\n";
    }
    $q = new mysql_squid_builder();
    if ($q->TABLE_EXISTS($tablename)) {
        $q->DELETE_TABLE($tablename);
    }
    if ($q->TABLE_EXISTS($tableBlock)) {
        $q->DELETE_TABLE($tableBlock);
    }
    if (!$q->CreateMemberReportTable($tablename)) {
        ufdbguard_admin_events("could not create table {$tablename}", __FUNCTION__, __FILE__, __LINE__, "reports");
        return;
    }
    if (!$q->CreateMemberReportBlockTable($tableBlock)) {
        ufdbguard_admin_events("could not create table {$tablename}", __FUNCTION__, __FILE__, __LINE__, "reports");
        return;
    }
    $rp = new squid_report($ID);
    $LIST_TABLES_dansguardian_events = $q->LIST_TABLES_dansguardian_events();
    progress(10, $ID);
    $counttables = count($LIST_TABLES_dansguardian_events);
    $prefix = "INSERT IGNORE INTO {$tablename} (`zMD5`,`sitename`,`familysite`,`{$rp->userfield}`,`zDate`,`size`,`hits`,`category`) VALUES ";
    while (list($sourcetable, $ligne) = each($LIST_TABLES_dansguardian_events)) {
        $c++;
        $sql = $rp->BuildQuery($sourcetable);
        $results = $q->QUERY_SQL($sql);
        if (!$q->ok) {
            ufdbguard_admin_events("{$q->mysql_error}\n{$sql}", __FUNCTION__, __FILE__, __LINE__, "reports");
            return;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "Parsing {$sourcetable} \n{$sql}\n-> `" . mysql_num_rows($results) . "` rows\n";
        }
        if (mysql_num_rows($results) == 0) {
            continue;
        }
        $purc = round($c / $counttables, 2) * 100;
        progress($purc, $ID);
        $f = array();
        while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
            $md5 = md5(serialize($ligne));
            $sitename = $ligne["sitename"];
            $familysite = $q->GetFamilySites($sitename);
            if (!isset($GLOBALS["CATEGORY"][$sitename])) {
                $GLOBALS["CATEGORY"][$sitename] = $q->GET_CATEGORIES($sitename);
            }
            $category = $GLOBALS["CATEGORY"][$sitename];
            $source = addslashes($ligne["source"]);
            $zDate = $ligne["zDate"];
            $size = $ligne["size"];
            $hits = $ligne["hits"];
            $category = addslashes($category);
            $f[] = "('{$md5}','{$sitename}','{$familysite}','{$source}','{$zDate}','{$size}','{$hits}','{$category}')";
        }
        if (count($f) == 0) {
            continue;
        }
        $q->QUERY_SQL($prefix . @implode(",", $f));
        if (!$q->ok) {
            ufdbguard_admin_events("{$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "reports");
            return;
        }
        if (system_is_overloaded(__FILE__)) {
            sleep(5);
        }
    }
    if ($rp->csv == 1) {
        SaveCSV($ID);
    }
    $LIST_TABLES_BLOCKED = $q->LIST_TABLES_BLOCKED();
    $prefix = "INSERT IGNORE INTO {$tableBlock} (`zMD5`,`zDate`,`hits`,`website`,`category`,`rulename`,`event`,`why`,`explain`,`blocktype`,`{$rp->userfield}`) VALUES ";
    while (list($sourcetable, $ligne) = each($LIST_TABLES_BLOCKED)) {
        $c++;
        if ($GLOBALS["VERBOSE"]) {
            echo "Parsing {$sourcetable}\n";
        }
        $sql = $rp->BuildQueryBlock($sourcetable);
        if (!$q->FIELD_EXISTS("{$sourcetable}", "uid")) {
            $q->QUERY_SQL("ALTER TABLE `{$sourcetable}` ADD `uid` VARCHAR( 128 ) NOT NULL ,ADD INDEX ( `uid` )");
        }
        $results = $q->QUERY_SQL($sql);
        if (!$q->ok) {
            ufdbguard_admin_events("{$q->mysql_error}\n{$sql}", __FUNCTION__, __FILE__, __LINE__, "reports");
            return;
        }
        if (mysql_num_rows($results) == 0) {
            continue;
        }
        $purc = round($c / $counttables, 2) * 100;
        progress($purc, $ID);
        $f = array();
        while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
            $zMD5 = md5(serialize($ligne));
            $sitename = $ligne["sitename"];
            $category = $ligne["category"];
            if ($category == null) {
                if (!isset($GLOBALS["CATEGORY"][$sitename])) {
                    $GLOBALS["CATEGORY"][$sitename] = $q->GET_CATEGORIES($sitename);
                }
                $category = $GLOBALS["CATEGORY"][$sitename];
            }
            $source = addslashes($ligne["source"]);
            $zDate = $ligne["zDate"];
            $hits = $ligne["hits"];
            $rulename = $ligne["rulename"];
            $event = $ligne["event"];
            $why = $ligne["why"];
            $explain = $ligne["explain"];
            $blocktype = $ligne["blocktype"];
            $category = addslashes($category);
            $f[] = "('{$zMD5}','{$zDate}','{$hits}','{$sitename}','{$category}','{$rulename}','{$event}','{$why}','{$explain}','{$blocktype}','{$source}')";
        }
        if (system_is_overloaded(__FILE__)) {
            sleep(5);
        }
    }
    $myisamchk = $unix->find_program("myisamchk");
    $myisampack = $unix->find_program("myisampack");
    $mysql_data = $unix->MYSQL_DATA_DIR();
    echo "OPTIMIZE TABLE {$tableBlock}\n";
    $q->QUERY_SQL("OPTIMIZE TABLE {$tableBlock}");
    echo "OPTIMIZE TABLE {$tablename}\n";
    $q->QUERY_SQL("OPTIMIZE TABLE {$tablename}");
    echo "LOCK TABLE {$tablename}\n";
    $q->QUERY_SQL("LOCK TABLE {$tablename} WRITE");
    echo "LOCK TABLE {$tableBlock}\n";
    $q->QUERY_SQL("LOCK TABLE {$tableBlock} WRITE");
    $q->QUERY_SQL("FLUSH TABLE {$tableBlock}");
    $q->QUERY_SQL("FLUSH TABLE {$tablename}");
    echo "myisamchk {$tablename}\n";
    shell_exec("{$myisamchk} -cFU {$mysql_data}/squidlogs/{$tablename}.MYI");
    echo "myisamchk {$tableBlock}\n";
    shell_exec("{$myisamchk} -cFU {$mysql_data}/squidlogs/{$tableBlock}.MYI");
    echo "myisampack {$tablename}\n";
    shell_exec("{$myisampack} -f {$mysql_data}/squidlogs/{$tablename}.MYI");
    echo "myisampack {$tableBlock}\n";
    shell_exec("{$myisampack} -f {$mysql_data}/squidlogs/{$tableBlock}.MYI");
    $q->QUERY_SQL("FLUSH TABLE {$tablename}");
    $q->QUERY_SQL("FLUSH TABLE {$tableBlock}");
    $rp->set_duration($unix->distanceOfTimeInWords($t, time(), true));
    progress(100, $ID);
}
Exemplo n.º 12
0
function Dumpimport($innodb_file_pertable = 0)
{
    system_admin_events("Create pointer to block Artica ", __FUNCTION__, __FILE__, __LINE__, "mysql");
    @mkdir("/home/mysqldump", 0755, true);
    $tmpfile = "/home/mysqldump/all-database.sql";
    $tmpError = "/home/mysqldump/all-database.errors";
    $t1 = time();
    $sock = new sockets();
    $sock->SET_INFO("DisableMySqlTemp", 0);
    $unix = new unix();
    $q = new mysql();
    $mysql_admin = $q->mysql_admin;
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    if (trim($mysql_admin) == null) {
        system_admin_events("Error, unable to get mysql_admin credentials", __FUNCTION__, __FILE__, __LINE__, "mysql");
        die;
    }
    if (!is_file($tmpfile)) {
        system_admin_events("Error, {$tmpfile} no such file", __FUNCTION__, __FILE__, __LINE__, "mysql");
        die;
    }
    $password = $q->mysql_password;
    if ($password != null) {
        $passwordcmdline = " --password={$password}";
    }
    $mysqlbin = $unix->find_program("mysql");
    if (!is_file($mysqlbin)) {
        system_admin_events("Error, mysqldump no such binary, aborting", __FUNCTION__, __FILE__, __LINE__, "mysql");
        die;
    }
    $BaseDir = $MYSQL_DATA_DIR;
    $files = $unix->DirFiles($BaseDir);
    system_admin_events("Info, stopping mysql", __FUNCTION__, __FILE__, __LINE__, "mysql");
    @file_put_contents("/etc/artica-postfix/mysql.stop", time());
    shell_exec("/etc/init.d/artica-postfix stop mysql");
    while (list($index, $line) = each($files)) {
        $fsize = FormatBytes($unix->file_size("{$BaseDir}/{$line}"));
        if (preg_match("#ib_logfile#", $line)) {
            system_admin_events("Info, removing {$BaseDir}/{$line}", __FUNCTION__, __FILE__, __LINE__, "mysql");
            @unlink("/{$BaseDir}/{$line}");
            $deleted[] = $line . ": {$fsize}";
            continue;
        }
        if (preg_match("#ibdata#", $line)) {
            system_admin_events("Info, removing {$BaseDir}/{$line}", __FUNCTION__, __FILE__, __LINE__, "mysql");
            @unlink("/{$BaseDir}/{$line}");
            $deleted[] = $line . ": {$fsize}";
            continue;
        }
        $skipped[] = $line . ": {$fsize}";
    }
    if ($innodb_file_pertable == 1) {
        system_admin_events("Info, innodb_file_per_table is enabled", __FUNCTION__, __FILE__, __LINE__, "mysql");
        $sock->SET_INFO("InnodbFilePerTable", 1);
        $php5 = $unix->LOCATE_PHP5_BIN();
        shell_exec("{$php5} " . dirname(__FILE__) . "/exec.mysql.build.php");
        system_admin_events("Deleting, ibdata1,ib_logfile0,ib_logfile1 is enabled", __FUNCTION__, __FILE__, __LINE__, "mysql");
        @unlink("{$MYSQL_DATA_DIR}/ibdata1");
        @unlink("{$MYSQL_DATA_DIR}/ib_logfile0");
        @unlink("{$MYSQL_DATA_DIR}/ib_logfile1");
    }
    @unlink("/etc/artica-postfix/mysql.stop");
    system_admin_events("Info, Cleaning Mysql directory: delete files\n" . @implode($deleted, "\n") . "Skipped files:\n" . @implode($skipped, "\n"), __FUNCTION__, __FILE__, __LINE__, "mysql");
    system_admin_events("Info, starting mysql", __FUNCTION__, __FILE__, __LINE__, "mysql");
    exec("/etc/init.d/artica-postfix start mysql 2>&1", $results);
    system_admin_events("Info, starting mysql done\n" . @implode("\n", $results), __FUNCTION__, __FILE__, __LINE__, "mysql");
    unset($results);
    system_admin_events("Stamp artica to not trying to inject to mysql", __FUNCTION__, __FILE__, __LINE__, "mysql");
    sleep(2);
    for ($i = 0; $i < 50; $i++) {
        $q = new mysql();
        $sleep = true;
        if (!$q->DATABASE_EXISTS("artica_backup")) {
            $sleep = true;
        }
        $pid = @file_get_contents("/var/run/mysqld/mysqld.pid");
        if ($unix->process_exists($pid)) {
            echo "MySQL server running PID {$pid}\n";
            $sleep = false;
        } else {
            $sleep = true;
        }
        if ($sleep) {
            echo "Sleeping 1s pid:{$pid} not running, artica_backup not available...\n";
            continue;
        }
        break;
    }
    if (!$unix->process_exists($pid)) {
        $pid = @file_get_contents("/var/run/mysqld/mysqld.pid");
        echo "MySQL server not running...\n";
        system_admin_events("Task aborted, MySQL did not running...", __FUNCTION__, __FILE__, __LINE__, "mysql");
        return;
    }
    $t = time();
    @unlink($tmpError);
    $cmd = "{$mysqlbin} --batch --force --user={$mysql_admin}{$passwordcmdline} < {$tmpfile} >{$tmpError} 2>&1";
    if ($GLOBALS["VERBOSE"]) {
        echo "{$cmd}\n";
    }
    shell_exec($cmd);
    $sock->SET_INFO("DisableMySqlTemp", 0);
    $t2 = time();
    $took = $unix->distanceOfTimeInWords($t, $t2, true);
    system_admin_events("finish restoring all databases and tables took: {$took}", __FUNCTION__, __FILE__, __LINE__, "mysql");
    $ISERRORED = false;
    while (list($index, $line) = each($f)) {
        if (preg_match("#error:\\s+([0-9]+)#i", $line)) {
            system_admin_events("Failed with error {$line}", __FUNCTION__, __FILE__, __LINE__, "mysql");
            $ISERRORED = true;
        }
        if (preg_match("#ERROR\\s+([0-9]+)#i", $line)) {
            system_admin_events("Failed with error {$line}", __FUNCTION__, __FILE__, __LINE__, "mysql");
            $ISERRORED = true;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "\"{$line}\" Unknown....line\n";
        }
    }
    $took = $unix->distanceOfTimeInWords($t1, time(), true);
    if (!$ISERRORED) {
        system_admin_events("importing the entire Mysql done took:{$took}", __FUNCTION__, __FILE__, __LINE__, "mysql");
        @unlink($tmpfile);
        @unlink($tmpError);
    } else {
        system_admin_events("Procedure Failed the mysqldump file is skipped in {$tmpfile} path, you can retry yourself to import datas using this commandline:\n{$cmd}", __FUNCTION__, __FILE__, __LINE__, "mysql");
    }
    $unix = new unix();
    $zaraf = $unix->find_program("zarafa-server");
    if (is_file($zaraf)) {
        shell_exec("/etc/init.d/artica-postfix restart zarafa-server");
    }
}