Ejemplo n.º 1
0
if ($argv[1] == "--restart") {
    $GLOBALS["OUTPUT"] = true;
    restart();
    die;
}
if ($argv[1] == "--init") {
    $GLOBALS["OUTPUT"] = true;
    initd();
    die;
}
if ($argv[1] == "--changemysqldir") {
    changemysqldir($argv[2]);
    die;
}
if ($argv[1] == "--databasesize") {
    databasesize($GLOBALS["FORCE"]);
    die;
}
if ($argv[1] == "--restorefrom") {
    RestoreFromBackup($argv[2]);
    die;
}
function start()
{
    $unix = new unix();
    $sock = new sockets();
    if (!isset($GLOBALS["MYSQL_BIN_PATH"])) {
        $GLOBALS["MYSQL_BIN_PATH"] = null;
    }
    if (!isset($GLOBALS["mysql_install_db"])) {
        $GLOBALS["mysql_install_db"] = true;
Ejemplo n.º 2
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;
}