function restore_container($servername, $path, $instance_id) { $unix = new unix(); $tmppath = "/var/tmp/" . time(); $t1 = time(); if (!is_numeric($instance_id)) { $instance_id = 0; } if (!is_file($path)) { writelogs("[{$servername}] fatal {$path} no such file...", __FUNCTION__, __FILE__, __LINE__); return; } $tar = $unix->find_program("tar"); $rm = $unix->find_program("rm"); $cp = $unix->find_program("cp"); $mysql = $unix->find_program("mysql"); @mkdir($tmppath, 0755, true); writelogs("[{$servername}] Uncompress {$path} to {$tmppath}", __FUNCTION__, __FILE__, __LINE__); exec("{$tar} -xf {$path} -C {$tmppath}/ 2>&1", $results); while (list($num_line, $evenement) = each($results)) { if (trim($evenement) != null) { writelogs("[{$servername}] {$evenement}", __FUNCTION__, __FILE__, __LINE__); } } $results = array(); if (!is_file("{$tmppath}/artica.restore")) { writelogs("[{$servername}] fatal {$tmppath}/artica.restore no such file...", __FUNCTION__, __FILE__, __LINE__); shell_exec("{$rm} -rf {$tmppath}"); return; } $CONF = unserialize(base64_decode(@file_get_contents("{$tmppath}/artica.restore"))); if (!is_array($CONF)) { writelogs("[{$servername}] fatal {$tmppath}/artica.restore no such array...", __FUNCTION__, __FILE__, __LINE__); shell_exec("{$rm} -rf {$tmppath}"); return; } $CONF["mysql_instance_id"] = $instance_id; if ($servername == "DEFAULT") { $servername = $CONF["servername"]; } else { $CONF["servername"] = $servername; } while (list($key, $value) = each($CONF)) { $fields[] = "`{$key}`"; $values[] = "'" . addslashes($value) . "'"; $edit[] = "`{$key}` = '" . addslashes($value) . "'"; } $sqlAdd = "INSERT IGNORE INTO freeweb (" . @implode(",", $fields) . ") VALUES (" . @implode(",", $values) . ")"; $sqledit = "UPDATE freeweb SET " . @implode(",", $edit) . " WHERE servername='{$servername}'"; writelogs("[{$servername}] restore settings", __FUNCTION__, __FILE__, __LINE__); $sql = "SELECT servername from freeweb WHERE servername='{$servername}'"; $q = new mysql(); $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup")); if ($ligne["servername"] == null) { $sql = $sqlAdd; writelogs("[{$servername}] Create the new website", __FUNCTION__, __FILE__, __LINE__); } else { writelogs("[{$servername}] restore the website settings", __FUNCTION__, __FILE__, __LINE__); $sql = $sqledit; } $q->QUERY_SQL($sql, "artica_backup"); if (!$q->ok) { writelogs("[{$servername}] fatal {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__); shell_exec("{$rm} -rf {$tmppath}"); return; } @unlink("{$tmppath}/artica.restore"); $free = new freeweb($servername); if (is_dir("{$tmppath}/MySQL")) { $filesArr = $unix->DirRecursiveFiles("{$tmppath}/MySQL", "*.sql"); $sql_file = $filesArr[0]; } writelogs("[{$servername}] database dump = `{$sql_file}`", __FUNCTION__, __FILE__, __LINE__); if (is_file($sql_file)) { writelogs("[{$servername}] Restoring database {$free->mysql_database} instance {$free->mysql_instance_id}", __FUNCTION__, __FILE__, __LINE__); $host = " --host={$q->mysql_server} --port={$q->mysql_port}"; if ($instance_id > 0) { $q = new mysql_multi($instance_id); $host = " --socket={$q->SocketPath}"; } $user = $q->mysql_admin; if ($q->mysql_password != null) { $adminpassword = $unix->shellEscapeChars($q->mysql_password); $adminpassword = str_replace("'", "", $adminpassword); $adminpassword = str_replace('$', '\\$', $adminpassword); $adminpassword = str_replace("'", '', $adminpassword); $adminpassword = "******"; $adminpassword_text = " --password=*****"; } if ($q->DATABASE_EXISTS($free->mysql_database)) { writelogs("[{$servername}] removing old database {$free->mysql_database}...", __FUNCTION__, __FILE__, __LINE__); $q->DELETE_DATABASE($free->mysql_database, true); } if (!$q->DATABASE_EXISTS($free->mysql_database)) { writelogs("[{$servername}] Creating database {$free->mysql_database}...", __FUNCTION__, __FILE__, __LINE__); $q->CREATE_DATABASE($free->mysql_database, true); } if (!$q->DATABASE_EXISTS($free->mysql_database)) { writelogs("[{$servername}] fatal Creating database {$free->mysql_database} failed...", __FUNCTION__, __FILE__, __LINE__); shell_exec("{$rm} -rf {$tmppath}"); return; } $cmdline = "{$mysql} --user={$user}{$adminpassword}{$host} \"{$free->mysql_database}\" < {$sql_file} 2>&1"; $cmdlineVer = "{$mysql} --user={$user}{$adminpassword_text}{$host} \"{$free->mysql_database}\" < {$sql_file} 2>&1"; writelogs("[{$servername}] {$cmdlineVer}", __FUNCTION__, __FILE__, __LINE__); exec($cmdline, $results); while (list($num_line, $evenement) = each($results)) { if (trim($evenement) != null) { writelogs("[{$servername}] {$evenement}", __FUNCTION__, __FILE__, __LINE__); } } $results = array(); if ($free->mysql_username != "root") { writelogs("[{$servername}] Setting privileges for {$free->mysql_username} on {$free->mysql_database}", __FUNCTION__, __FILE__, __LINE__); $q->PRIVILEGES($free->mysql_username, $free->mysql_password, $free->mysql_database); } } if (is_dir("{$tmppath}/MySQL")) { @unlink($sql_file); } writelogs("[{$servername}] restoring {$free->WORKING_DIRECTORY}", __FUNCTION__, __FILE__, __LINE__); @mkdir($free->WORKING_DIRECTORY, 0755, true); if (!is_dir($free->WORKING_DIRECTORY)) { writelogs("[{$servername}] fatal {$free->WORKING_DIRECTORY} permission denied...", __FUNCTION__, __FILE__, __LINE__); shell_exec("{$rm} -rf {$tmppath}"); return; } $cmdline = "{$cp} -rf {$tmppath}/* {$free->WORKING_DIRECTORY}/ 2>&1"; writelogs("[{$servername}] {$cmdline}", __FUNCTION__, __FILE__, __LINE__); exec($cmdline, $results); while (list($num_line, $evenement) = each($results)) { if (trim($evenement) != null) { writelogs("[{$servername}] {$evenement}", __FUNCTION__, __FILE__, __LINE__); } } $results = array(); writelogs("[{$servername}] Cleaning temporary directory", __FUNCTION__, __FILE__, __LINE__); shell_exec("{$rm} -rf {$tmppath}"); writelogs("[{$servername}] rebuild the website", __FUNCTION__, __FILE__, __LINE__); buildHost(null, $servername); if (!$GLOBALS["NO_HTTPD_CONF"]) { CheckHttpdConf(); } if (!$GLOBALS["NO_HTTPD_RELOAD"]) { reload_apache(); } $t2 = time(); $took = $unix->distanceOfTimeInWords($t1, $t2, true); writelogs("[{$servername}] Finish restoring the website took:{$took}", __FUNCTION__, __FILE__, __LINE__); }