Exemplo n.º 1
0
function RestoreFromBackup($backuppath)
{
    $unix = new unix();
    $PidRestore = "/etc/artica-postfix/pids/zarafaRestore.pid";
    $rm = $unix->find_program("rm");
    $pid = $unix->get_pid_from_file($PidRestore);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n";
        }
        return;
    }
    $pid = $unix->PIDOF_PATTERN("exec.zarafa-db.php --restorefrom");
    if ($pid != getmypid()) {
        if ($unix->process_exists($pid)) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n";
            }
            return;
        }
    }
    $mysql = $unix->find_program("mysql");
    $pid = $unix->PIDOF_PATTERN("{$mysql}\\s+.*?--socket=/var/run/mysqld/zarafa-db.sock.*?database=zarafa");
    if ($unix->process_exists($pid)) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n";
        }
        return;
    }
    @file_put_contents($PidRestore, getmypid());
    $sock = new sockets();
    $SourceDir = dirname($backuppath);
    $WORKDIR = $sock->GET_INFO("ZarafaDedicateMySQLWorkDir");
    if ($WORKDIR == null) {
        $WORKDIR = "/home/zarafa-db";
    }
    if (is_file("{$SourceDir}/ldap.ldif")) {
        RestoreFromBackup_progress("{restore_ldap_database}", 10);
        RestoreFromBackup_ldap("{$SourceDir}/ldap.ldif");
    }
    $unix = new unix();
    if (!is_file($backuppath)) {
        echo "Action: `{$backuppath}` no such file: ABORT!\n";
        RestoreFromBackup_progress("{failed}", 100);
        return;
    }
    echo "Action: Removing Zarafa Database MySQL client `{$mysql}`....\n";
    RestoreFromBackup_progress("Removing Zarafa Database", 30);
    $cmd = "{$mysql} --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa --execute=\"DROP DATABASE zarafa\" 2>&1";
    $results = array();
    exec("{$cmd}", $results);
    while (list($num, $ligne) = each($results)) {
        echo "MySQL: (Delete Database) {$ligne}\n";
    }
    RestoreFromBackup_progress("Removing all content", 32);
    if (is_dir("{$WORKDIR}/data/zarafa")) {
        recursive_remove_directory("{$WORKDIR}");
    }
    RestoreFromBackup_progress("Restarting MySQL service (recovery)", 40);
    echo "Action: Restarting MySQL service...\n";
    echo "Action: Stopping MySQL service...\n";
    stop(true);
    echo "Action: Starting MySQL service (InnoDB recovery mode)...\n";
    start(true, true);
    while (list($num, $ligne) = each($results)) {
        echo "Service: {$ligne}\n";
    }
    sleep(5);
    $ZARAFADB_PID = ZARAFADB_PID();
    if (!$unix->process_exists($ZARAFADB_PID)) {
        RestoreFromBackup_progress("Failed to restart dedicated MySQL", 100);
        return;
    }
    RestoreFromBackup_progress("Stopping Zarafa server", 43);
    @unlink("/tmp/zarafa-upgrade-lock");
    shell_exec("/etc/init.d/zarafa-server stop --force");
    $pid = XZARAFA_SERVER_PID();
    if ($unix->process_exists($pid)) {
        $kill = $unix->find_program("kill");
        unix_system_kill_force($pid);
    }
    RestoreFromBackup_progress("Restarting MySQL service (normal)", 45);
    echo "Action: Restarting MySQL service...\n";
    echo "Action: Stopping MySQL service...\n";
    stop(true);
    echo "Action: Starting MySQL service (InnoDB normal mode)...\n";
    start(true, false);
    while (list($num, $ligne) = each($results)) {
        echo "Service: {$ligne}\n";
    }
    sleep(2);
    $ZARAFADB_PID = ZARAFADB_PID();
    if (!$unix->process_exists($ZARAFADB_PID)) {
        RestoreFromBackup_progress("Failed to restart dedicated MySQL", 100);
        return;
    }
    if (!$unix->is_socket("/var/run/mysqld/zarafa-db.sock")) {
        echo "Action: /var/run/mysqld/zarafa-db.sock waiting socket\n";
        for ($i = 0; $i < 5; $i++) {
            if ($unix->is_socket("/var/run/mysqld/zarafa-db.sock")) {
                break;
            }
            echo "Action: Waiting zarafa-db.sock {$i}/4\n";
            sleep(1);
        }
    }
    if (!$unix->is_socket("/var/run/mysqld/zarafa-db.sock")) {
        echo "Action: /var/run/mysqld/zarafa-db.sock no such socket\n";
        RestoreFromBackup_progress("zarafa-db.sock no such socket", 100);
        return;
    }
    echo "Action: /var/run/mysqld/zarafa-db.sock OK\n";
    echo "Action: create a freshed Zarafa database\n";
    $ZarafaIndexPath = $sock->GET_INFO("ZarafaIndexPath");
    if ($ZarafaIndexPath == null) {
        $ZarafaIndexPath = "/var/lib/zarafa/index";
    }
    RestoreFromBackup_progress("Cleaning/Stopping Zarafa search DBs", 50);
    if (is_dir($ZarafaIndexPath)) {
        recursive_remove_directory("{$ZarafaIndexPath}");
        shell_exec("/etc/init.d/zarafa-search stop");
    }
    RestoreFromBackup_progress("Create a freshed Zarafa database", 50);
    $results = array();
    $cmd = "{$mysql} --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --execute=\"CREATE DATABASE zarafa\" 2>&1";
    $results = array();
    exec("{$cmd}", $results);
    while (list($num, $ligne) = each($results)) {
        echo "MySQL: (Create Database) {$ligne}\n";
    }
    RestoreFromBackup_progress("Testing Database...", 51);
    if (!is_dir("{$WORKDIR}/data/zarafa")) {
        echo "Action: FAILED TO create a freshed Zarafa database: ABORT!!\n";
        echo "Action: {$WORKDIR}/data/zarafa no such directory\n";
        RestoreFromBackup_progress("FAILED to create a freshed Zarafa database", 100);
        return;
    }
    RestoreFromBackup_progress("Checks Database size", 53);
    databasesize(true);
    $gunzip = $unix->find_program("gunzip");
    $SourceFileBase = basename($backuppath);
    $file_ext = $unix->file_ext($SourceFileBase);
    $tStart = time();
    $nohup = $unix->find_program("nohup");
    $backuppath1 = $unix->shellEscapeChars($backuppath);
    $cmd = "{$nohup} {$mysql} --show-warnings --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa < {$backuppath1} >/root/mysqllog.txt 2>&1 &";
    echo "Action: {$SourceFileBase} extension {$file_ext}\n";
    echo "Action: Restoring From {$backuppath1}\n";
    if ($file_ext == "gz") {
        echo "Action: Restoring From {$backuppath1} with uncompress..\n";
        $cmd = "{$nohup} {$gunzip} -c {$backuppath1} |{$mysql} --show-warnings --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa >/root/mysqllog.txt 2>&1 &";
    }
    $size = @filesize($backuppath);
    $size = FormatBytes($size / 1024);
    echo "Action: Please wait, it should take time...\nAction: Do not shutdown the computer or restart the MySQL service!\n";
    $results = array();
    RestoreFromBackup_progress("{restoring_data} {$size} {please_wait} !", 70);
    $lastmd5 = null;
    $continue = true;
    shell_exec($cmd);
    $ALRDLO = array();
    while ($continue) {
        $fileMD5 = @md5_file("/root/mysqllog.txt");
        if ($fileMD5 != $lastmd5) {
            $LOGS = explode("\n", @file_get_contents("/root/mysqllog.txt"));
            while (list($num, $ligne) = each($LOGS)) {
                if (trim($ligne) == null) {
                    continue;
                }
                if (isset($ALRDLO[md5($ligne)])) {
                    continue;
                }
                $ALRDLO[md5($ligne)] = true;
                if (preg_match("#ERROR\\s+([0-9]+)\\s+\\(#", $ligne, $re)) {
                    echo date("Y-m-d H:i:s") . " MySQL: FAILED !!! {$ligne}\n";
                    RestoreFromBackup_progress("{failed} {error} {$re[1]} ", 100);
                    return;
                }
                echo date("Y-m-d H:i:s") . " MySQL: {$ligne}\n";
            }
            $lastmd5 = $fileMD5;
        }
        $pid = $unix->PIDOF_PATTERN("{$mysql}\\s+.*?--socket=/var/run/mysqld/zarafa-db.sock.*?database=zarafa");
        echo "Action: PID: {$pid}\n";
        if (!$unix->process_exists($pid)) {
            echo "Action: injection stopped running since " . $unix->distanceOfTimeInWords($tStart, time(), true) . "\n";
            $continue = false;
            break;
        }
        echo "Action: PID {$pid} running since " . $unix->distanceOfTimeInWords($tStart, time(), true) . ", please wait...\n";
        RestoreFromBackup_progress($unix->distanceOfTimeInWords($tStart, time(), true) . " {please_wait} !", 71);
        $continue = true;
        sleep(30);
        continue;
    }
    echo "Action: Done, took: " . $unix->distanceOfTimeInWords($tStart, time(), true) . "\n";
    echo "Action: Please wait, Checks Database size\n";
    RestoreFromBackup_progress("Checks Database size", 75);
    databasesize(true);
    RestoreFromBackup_progress("{restoring_data} {success}", 80);
    echo "Action: restart_services\n";
    RestoreFromBackup_progress("{restart_services}", 90);
    $unix->THREAD_COMMAND_SET("/etc/init.d/zarafa-server restart");
    echo "Action: Restore task done...\n";
    echo "Action: You can close the windows now...\n";
    RestoreFromBackup_progress("{done}", 100);
    die;
}
Exemplo n.º 2
0
function start($aspid = false)
{
    $unix = new unix();
    $sock = new sockets();
    $pidfile = "/etc/artica-postfix/pids/zarafa-server-starter.pid";
    $PidRestore = "/etc/artica-postfix/pids/zarafaRestore.pid";
    $PidLock = "/etc/artica-postfix/LOCK_ZARAFA";
    $pid = $unix->get_pid_from_file($PidRestore);
    if ($unix->process_exists($pid)) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine Artica Restore running PID {$pid} since {$time}mn\n";
        }
        return;
    }
    if (!$aspid) {
        $pid = $unix->get_pid_from_file($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine Artica Task Already running PID {$pid} since {$time}mn\n";
            }
            return;
        }
    }
    @file_put_contents($pidfile, getmypid());
    $serverbin = $unix->find_program("zarafa-server");
    if (!is_file($serverbin)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine is not installed...\n";
        }
        return;
    }
    if (is_file($PidLock)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server !! Locked !! ( {$PidLock} ) aborting...\n";
        }
        return;
    }
    $SLAPD_PID_FILE = $unix->SLAPD_PID_PATH();
    $pid = $unix->get_pid_from_file($SLAPD_PID_FILE);
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine OpenLDAP server is not running start it...\n";
        }
        shell_exec("/etc/init.d/slapd start");
        return;
    }
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine Failed, OpenLDAP server is not running...\n";
        }
    } else {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine OpenLDAP server is running...\n";
        }
    }
    if (!is_file("/usr/lib/libmapi.so")) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server fix /usr/lib/libmapi.so\n";
        }
        if (is_file("/home/artica/zarafa.tar.gz.old")) {
            $tar = $unix->find_program("tar");
            shell_exec("{$tar} -xf /home/artica/zarafa.tar.gz.old -C /");
        }
    }
    if (is_dir("/usr/share/zarafa-webapp/webapp-1.4.svn42633")) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server fix webapp-1.4.svn42633\n";
        }
        $cp = $unix->find_program("cp");
        $rm = $unix->find_program("rm");
        shell_exec("{$cp} -rf /usr/share/zarafa-webapp/webapp-1.4.svn42633/ /usr/share/zarafa-webapp/");
        shell_exec("{$rm} -rf /usr/share/zarafa-webapp/webapp-1.4.svn42633");
    }
    $ZarafaMySQLServiceType = $sock->GET_INFO("ZarafaMySQLServiceType");
    $ZarafaDedicateMySQLServer = $sock->GET_INFO("ZarafaDedicateMySQLServer");
    if (!is_numeric($ZarafaMySQLServiceType)) {
        $ZarafaMySQLServiceType = 1;
    }
    if (!is_numeric($ZarafaDedicateMySQLServer)) {
        $ZarafaDedicateMySQLServer = 0;
    }
    if ($ZarafaDedicateMySQLServer == 1) {
        if ($ZarafaMySQLServiceType == 3) {
            $PID = ZARAFADB_PID();
            if (!$unix->process_exists($PID)) {
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine Failed, Zarafa Database is not running\n";
                }
            }
        }
    }
    $pid = XZARAFA_SERVER_PID();
    if ($unix->process_exists($pid)) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine already running pid {$pid} since {$time}mn\n";
        }
        return;
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server Engine reconfigure...\n";
    }
    system("/usr/share/artica-postfix/bin/artica-install --zarafa-reconfigure");
    @unlink("/usr/share/artica-postfix/ressources/logs/zarafa.notify");
    @unlink("/usr/share/artica-postfix/ressources/logs/zarafa.notify.MySQLIssue");
    $f[] = $serverbin;
    $f[] = "--config=/etc/zarafa/server.cfg";
    $f[] = "--ignore-database-version-conflict";
    $f[] = "--ignore-unknown-config-options";
    $f[] = "--ignore-attachment-storage-conflict";
    $cmdline = @implode(" ", $f);
    if ($GLOBALS["VERBOSE"]) {
        echo $cmdline . "\n";
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: Starting zarafa-server daemon\n";
    }
    shell_exec("{$cmdline} 2>&1");
    sleep(1);
    for ($i = 0; $i < 5; $i++) {
        $pid = XZARAFA_SERVER_PID();
        if ($unix->process_exists($pid)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server daemon started pid .{$pid}..\n";
            }
            break;
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server daemon wait {$i}/5\n";
        }
        sleep(1);
    }
    $pid = XZARAFA_SERVER_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server daemon failed to start\n";
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$cmdline}\n";
        } else {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: zarafa-server daemon success PID {$pid}\n";
            }
        }
    }
}