function runRestoreJobForeign($continue = false) { $ei = false; $fcache = ''; $writes = 0; $old_charset = ''; $tab = ''; $seek = 0; $this->rtl[3] = ftell($this->fh_tmp); fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $c = 0; $log_sql = false; $fields = ''; $insert = ''; $last_tab = ''; $time_old = time(); $exit_time = $time_old + $this->CFG['time_web'] - 1; $delimiter = ";\n"; while ($q = sxd_read_sql($this->fh_tmp, $seek, $ei, $delimiter)) { $q = ltrim($q); if (empty($q)) { break; } if ($time_old < time()) { if (file_exists($this->JOB['file_stp'])) { $type = file_get_contents($this->JOB['file_stp']); $this->rtl[9] = !empty($type) ? $type : 2; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); /*if($type == 1) { }*/ unset($this->rtl); exit; } $time_old = time(); if ($time_old >= $exit_time) { $this->rtl[9] = 3; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); unset($this->rtl); exit; } clearstatcache(); } do { $repeat = false; switch ($q[0]) { case '(': if ($continue) { $this->addLog(sprintf("{$this->LNG['restore_TC']} {$this->LNG['continue_from']}", $this->rtl[5], $this->rtl[3])); $continue = false; } $q = $insert . $q; $ex = 1; $c = 1; break; case 'I': if (preg_match('/^(INSERT( INTO `(.+?)`)\\s+?VALUES)/', $q, $m)) { $insert = trim($m[1]) . ' '; $tab = $m[3]; $this->rtl[7] = 0; $this->rtl[8] = 0; $ex = 1; } break; case 'C': $ex = 1; $ei = 1; if (preg_match('/^CREATE TABLE.+?`(.+?)`/', $q, $m)) { $ex = 1; $tab = $m[1]; $this->addLog(sprintf($this->LNG['restore_TC'], $tab)); //mysql_query("DROP TABLE IF EXISTS `{$tab}`"); if (!empty($this->JOB['correct']) && !empty($this->JOB['charset'])) { $q = preg_replace('/(DEFAULT)?\\s*(CHARSET|CHARACTER SET|COLLATE)[=\\s]+\\w+/i', '', $q) . (V_MYSQL < 40100 ? '' : ' DEFAULT CHARSET=' . $this->JOB['charset']); } elseif (empty($this->JOB['charset'])) { if (preg_match("/(CHARACTER SET|CHARSET)[=\\s]+(\\w+)/i", $q, $charset)) { $this->setNames($charset[2]); } } } break; case '-' && $q[1] == '-': case '#': $repeat = true; $q = ltrim(substr($q, strpos($q, "\n"))); $ex = 0; break; case '/': case 'S': if (preg_match('/SET NAMES (\\w+)/', $q, $m)) { $this->JOB['charset'] = $m[1]; $this->setNames($this->JOB['charset']); $ex = 0; } else { $ex = 1; } break; default: $insert = ''; $ex = 1; $ei = 0; } } while ($repeat); if ($ex) { //error_log("-----------------\n{$q}\n", 3, "sql.log"); $this->rtl[3] = ftell($this->fh_tmp) - $seek; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); if (mysql_query($q)) { if ($insert) { $c = 1; } } else { error_log("-----------------\n{$q}\n", 3, "error.log"); sxd_my_error(); } if ($c) { $i = mysql_affected_rows(); $this->rtl[3] = ftell($this->fh_tmp) - $seek; $this->rtl[7] += $i; $this->rtl[10] += $i; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $c = 1; } } } $this->rtl[4] = 'EOJ'; $this->rtl[5] = round(array_sum(explode(' ', microtime())) - $this->rtl[11], 4); $this->rtl[7] = 0; $this->rtl[8] = 0; fseek($this->fh_rtl, 0); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $this->addLog(sprintf($this->LNG['restore_end'], $this->JOB['db'])); fclose($this->fh_log); fclose($this->fh_rtl); }
function runRestoreJob($continue = false) { $ei = false; if ($continue) { $this->fh_tmp = $this->openFile($this->JOB['file_tmp'], 'r'); fseek($this->fh_tmp, $this->rtl[3]); if (!empty($this->rtl[6])) { $this->setNames($this->JOB['correct'] == 1 && !empty($this->JOB['charset']) ? $this->JOB['charset'] : $this->rtl[6]); } if ($this->rtl[7] < $this->rtl[10]) { $ei = true; } } mysql_select_db($this->JOB['db']); //mysql_query("SET NAMES 'UTF8'"); $types = array('VI' => 'View', 'PR' => 'Procedure', 'FU' => 'Function', 'TR' => 'Trigger', 'EV' => 'Event'); $fcache = ''; $writes = 0; $old_charset = ''; $tab = ''; $seek = 0; $this->rtl[3] = ftell($this->fh_tmp); fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $c = 0; switch ($this->JOB['strategy']) { case 1: $tc = 'TRUNCATE'; $td = 'INSERT'; break; case 2: $tc = ''; $td = 'REPLACE'; break; case 3: $tc = ''; $td = 'INSERT IGNORE'; break; default: $tc = 'DROP TABLE IF EXISTS'; $td = 'INSERT'; } $tab_exists = array(); if ($this->JOB['strategy'] > 0) { $r = mysql_query("SHOW TABLES") or sxd_my_error(); while ($item = mysql_fetch_row($r)) { $tab_exists[$item[0]] = true; } } $insert = $continue && $this->rtl[7] < $this->rtl[10] ? "{$td} INTO `{$this->rtl[5]}` VALUES " : ''; //$enable_index = array(); if (V_MYSQL > 40014) { mysql_query("SET UNIQUE_CHECKS=0"); mysql_query("SET FOREIGN_KEY_CHECKS=0"); if (V_MYSQL > 40101) { mysql_query("SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'"); } if (V_MYSQL > 40111) { mysql_query("SET SQL_NOTES=0"); } } $log_sql = false; $fields = ''; $time_old = time(); $exit_time = $time_old + $this->CFG['time_web'] - 1; while ($q = sxd_read_sql($this->fh_tmp, $seek, $ei)) { if ($time_old < time()) { if (file_exists($this->JOB['file_stp'])) { $type = file_get_contents($this->JOB['file_stp']); $this->rtl[9] = !empty($type) ? $type : 2; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); /*if($type == 1) { }*/ unset($this->rtl); exit; } $time_old = time(); if ($time_old >= $exit_time) { $this->rtl[9] = 3; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); unset($this->rtl); exit; } clearstatcache(); } switch ($q[0]) { case '(': if ($continue) { $this->addLog(sprintf("{$this->LNG['restore_TC']} {$this->LNG['continue_from']}", $this->rtl[5], $this->rtl[3])); $continue = false; } $q = $insert . $q; $ex = 1; $c = 1; break; case 'I': if (preg_match('/^INSERT( INTO `(.+?)`) VALUES/', $q, $m)) { $insert = $td . $m[1] . $fields . ' VALUES '; $tab = $m[2]; $this->rtl[7] = 0; $this->rtl[8] = 0; foreach ($this->JOB['todo']['TA'] as $t) { if ($t[1] == $tab) { $this->rtl[8] = $t[2]; } } if ($this->JOB['strategy']) { $q = substr_replace($q, $insert, 0, strlen($m[0])); } //mysql_query("LOCK TABLES `{$tab}` WRITE") or die (mysql_error()); mysql_query("ALTER TABLE `{$tab}` DISABLE KEYS") or sxd_my_error(); if (!empty($this->JOB['autoinc'])) { mysql_query("ALTER TABLE `{$tab}` AUTO_INCREMENT = 1") or sxd_my_error(); } $ex = 1; } break; case 'C': $ex = 1; if (preg_match('/^CREATE TABLE `/', $q)) { if ($this->JOB['strategy'] != 0 && isset($tab_exists[$this->rtl[5]])) { $ex = 0; } else { $ex = 1; if (!empty($this->JOB['correct']) && !empty($this->JOB['charset'])) { $q = preg_replace('/(DEFAULT)?\\s*(CHARSET|CHARACTER SET|COLLATE)[=\\s]+\\w+/i', '', $q) . (V_MYSQL < 40100 ? '' : 'DEFAULT CHARSET=' . $this->JOB['charset']); } } // Достаем имена полей таблицы $fields = $this->JOB['strategy'] > 0 && preg_match_all('/^\\s+(`.+?`) /m', $q, $f, PREG_PATTERN_ORDER) ? '(' . implode(',', $f[1]) . ')' : ''; } break; case '#': // Команды для дампера if (preg_match("/\\#\t(TC|TD|VI|PR|FU|TR|EV)`(.+?)`(([^_]+?)_.+?)?\$/", $q, $m)) { //if(!empty($tab)) $enable_index[] = $tab; $this->setNames($this->JOB['correct'] == 1 && !empty($this->JOB['charset']) ? $this->JOB['charset'] : empty($m[3]) ? '' : $m[3]); if ($m[1] == 'TC') { $this->addLog(sprintf($this->LNG['restore_TC'], $m[2])); $insert = ''; $tab = ''; $this->rtl[4] = 'TD'; $this->rtl[5] = $m[2]; $ei = 0; if ($tc && ($this->JOB['strategy'] == 0 || isset($tab_exists[$m[2]]))) { mysql_query("{$tc} `{$m[2]}`") or sxd_my_error(); } } elseif ($m[1] == 'TD') { $ei = 1; } else { $this->rtl[4] = $m[1]; $this->rtl[5] = $m[2]; $this->rtl[7] = 0; $this->rtl[8] = 0; mysql_query("DROP {$types[$m[1]]} IF EXISTS `{$m[2]}`") or sxd_my_error(); $this->addLog(sprintf($this->LNG["restore_{$m[1]}"], $m[2])); $ei = 0; } } $ex = 0; break; default: $insert = ''; $ex = 1; } if ($ex) { //error_log("[index.php]\n{$q}\n", 3, "error.log"); $this->rtl[3] = ftell($this->fh_tmp) - $seek; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); if (mysql_query($q)) { if ($insert) { $c = 1; } } else { error_log("[index.php]\n{$q}\n", 3, "error.log"); sxd_my_error(); } if ($c) { $i = mysql_affected_rows(); $this->rtl[3] = ftell($this->fh_tmp) - $seek; $this->rtl[7] += $i; $this->rtl[10] += $i; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $c = 1; } } } // Включаем ключи $this->addLog($this->LNG['restore_keys']); $this->rtl[4] = 'EK'; $this->rtl[5] = ''; $this->rtl[6] = ''; $this->rtl[7] = 0; $this->rtl[8] = 0; foreach ($this->JOB['todo']['TA'] as $tab) { if ($tab[0] == 'TC') { continue; } mysql_query("ALTER TABLE `{$tab[1]}` ENABLE KEYS") or sxd_my_error(); $this->rtl[1] = time(); $this->rtl[5] = $tab[1]; fseek($this->fh_rtl, 0); fwrite($this->fh_rtl, implode("\t", $this->rtl)); } $this->rtl[4] = 'EOJ'; $this->rtl[5] = round(array_sum(explode(' ', microtime())) - $this->rtl[11], 4); fseek($this->fh_rtl, 0); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $this->addLog(sprintf($this->LNG['restore_end'], $this->JOB['db'])); fclose($this->fh_log); fclose($this->fh_rtl); }
function runRestoreJob($continue = false) { $ei = false; if ($continue) { $this->fh_tmp = $this->openFile($this->JOB["file_tmp"], "r"); fseek($this->fh_tmp, $this->rtl[3]); if (!empty($this->rtl[6])) { $this->setNames($this->JOB["correct"] == 1 && !empty($this->JOB["charset"]) ? $this->JOB["charset"] : $this->rtl[6]); } if ($this->rtl[7] < $this->rtl[10]) { $ei = true; } } mysql_select_db($this->JOB["db"]); if (is_null($this->JOB["obj"])) { $this->runRestoreJobForeign($continue); } $types = array("VI" => "View", "PR" => "Procedure", "FU" => "Function", "TR" => "Trigger", "EV" => "Event"); $fcache = ""; $writes = 0; $old_charset = ""; $tab = ""; $seek = 0; $this->rtl[3] = ftell($this->fh_tmp); fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $c = 0; switch ($this->JOB["strategy"]) { case 1: $tc = "TRUNCATE"; $td = "INSERT"; break; case 2: $tc = ""; $td = "REPLACE"; break; case 3: $tc = ""; $td = "INSERT IGNORE"; break; default: $tc = "DROP TABLE IF EXISTS"; $td = "INSERT"; } $tab_exists = array(); if ($this->JOB["strategy"] > 0) { $r = mysql_query("SHOW TABLES") or sxd_my_error(); while ($item = mysql_fetch_row($r)) { $tab_exists[$item[0]] = true; } } $this->query = $query = $this->JOB["savesql"] ? "save_query" : "mysql_query"; if ($this->JOB["savesql"] && file_exists($this->JOB["file_name"] . ".sxd.sql")) { unlink($this->JOB["file_name"] . ".sxd.sql"); } $insert = $continue && $this->rtl[7] < $this->rtl[10] ? "{$td} INTO `{$this->rtl[5]}` VALUES " : ""; if (V_MYSQL > 40014) { $query("SET UNIQUE_CHECKS=0"); $query("SET FOREIGN_KEY_CHECKS=0"); $query("SET autocommit=0;"); if (V_MYSQL > 40101) { $query("SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'"); } if (V_MYSQL > 40111) { $query("SET SQL_NOTES=0"); } } $log_sql = false; $fields = ""; $time_old = time(); $exit_time = $time_old + $this->CFG["time_web"] - 1; $prefix = empty($this->JOB["prefix_from"]) || empty($this->JOB["prefix_to"]) ? false : true; $skipto = $this->skipper($this->JOB["todo"][0][0]); while ($q = sxd_read_sql($this->fh_tmp, $seek, $ei, $skipto)) { if ($time_old < time()) { if (file_exists($this->JOB["file_stp"])) { $type = file_get_contents($this->JOB["file_stp"]); $this->rtl[9] = !empty($type) ? $type : 2; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); unset($this->rtl); exit; } $time_old = time(); if ($time_old >= $exit_time) { $this->rtl[9] = 3; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); unset($this->rtl); exit; } clearstatcache(); } switch ($q[0]) { case "(": if ($continue) { $this->addLog(sprintf("{$this->LNG['restore_TC']} {$this->LNG['continue_from']}", $this->rtl[5], $this->rtl[3])); $continue = false; } $q = $insert . $q; $ex = 1; $c = 1; break; case "I": if (preg_match("/^INSERT( INTO `(.+?)`) VALUES/", $q, $m)) { $insert = $td . $m[1] . $fields . " VALUES \n"; $tab = $m[2]; $this->rtl[7] = 0; $this->rtl[8] = 0; foreach ($this->JOB["todo"] as $t) { if ($t[1] == $tab) { $this->rtl[8] = $t[2]; } } if ($prefix) { $insert = preg_replace("/`{$this->JOB['prefix_from']}(.+?)`/", "`{$this->JOB['prefix_to']}\\1`", $insert); $tab = preg_replace("/^{$this->JOB['prefix_from']}(.+?)/", "{$this->JOB['prefix_to']}\\1", $tab); $q = substr_replace($q, $insert, 0, strlen($m[0]) + 2); } elseif ($this->JOB["strategy"]) { $q = substr_replace($q, $insert, 0, strlen($m[0]) + 2); } $query("ALTER TABLE `{$tab}` DISABLE KEYS") or sxd_my_error(); $ex = 1; } break; case "C": $ex = 1; if (preg_match("/^CREATE TABLE `/", $q)) { if ($this->JOB["strategy"] != 0 && isset($tab_exists[$this->rtl[5]])) { $ex = 0; } else { $ex = 1; if ($prefix) { $q = preg_replace("/^CREATE TABLE `{$this->JOB['prefix_from']}(.+?)` \\(/", "CREATE TABLE `{$this->JOB['prefix_to']}\\1` (", $q); } if (!empty($this->JOB["correct"]) && !empty($this->JOB["charset"])) { $q = preg_replace("/(DEFAULT)?\\s*(CHARSET|CHARACTER SET|COLLATE)[=\\s]+\\w+/i", "", $q) . (V_MYSQL < 40100 ? "" : " DEFAULT CHARSET=" . $this->JOB["charset"]); } if (!empty($this->JOB["autoinc"])) { $q = preg_replace("/AUTO_INCREMENT=\\d+/", "AUTO_INCREMENT=1", $q); } } $fields = $this->JOB["strategy"] > 0 && preg_match_all("/^\\s+(`.+?`) /m", $q, $f, PREG_PATTERN_ORDER) ? "(" . implode(",", $f[1]) . ")" : ""; } break; case "#": if (preg_match("/\\#\t(TC|TD|VI|PR|FU|TR|EV)`(.+?)`(([^_]+?)_.+?)?\$/", $q, $m)) { $skipto = $this->skipper($m[1], $m[2]); if ($skipto) { $ex = 0; continue; } if ($m[1] == "TD" || $m[1] == "TC") { $this->setNames($this->JOB["correct"] == 1 && !empty($this->JOB["charset"]) ? $this->JOB["charset"] : (empty($m[3]) ? "" : $m[3])); } else { $this->setNames("utf8"); } $m[2] = preg_replace("/^{$this->JOB['prefix_from']}(.+?)/", "{$this->JOB['prefix_to']}\\1", $m[2]); if ($m[1] == "TC") { $this->addLog(sprintf($this->LNG["restore_TC"], $m[2])); $insert = ""; $tab = ""; $this->rtl[4] = "TD"; $this->rtl[5] = $m[2]; $ei = 0; if ($tc && ($this->JOB["strategy"] == 0 || isset($tab_exists[$m[2]]))) { $query("{$tc} `{$m[2]}`") or sxd_my_error(); } } elseif ($m[1] == "TD") { $ei = 1; } else { $this->rtl[4] = $m[1]; $this->rtl[5] = $m[2]; $this->rtl[7] = 0; $this->rtl[8] = 0; $query("DROP {$types[$m[1]]} IF EXISTS `{$m[2]}`") or sxd_my_error(); $this->addLog(sprintf($this->LNG["restore_{$m[1]}"], $m[2])); $ei = 0; } } $ex = 0; break; default: $insert = ""; $ex = 1; } if ($ex) { $this->rtl[3] = ftell($this->fh_tmp) - $seek; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); if ($query($q)) { if ($insert) { $c = 1; } } else { error_log(date("r") . "\n----------\n{$q}\n", 3, "backup/sql_error.log"); sxd_my_error(); } if ($c) { $i = $this->JOB["savesql"] ? $this->nl_count : mysql_affected_rows(); $this->rtl[3] = ftell($this->fh_tmp) - $seek; $this->rtl[7] += $i; $this->rtl[10] += $i; fseek($this->fh_rtl, 0); $this->rtl[1] = time(); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $c = 1; } } } if (!$this->JOB["savesql"]) { $this->addLog($this->LNG["restore_keys"]); $this->rtl[4] = "EK"; $this->rtl[5] = ""; $this->rtl[6] = ""; $this->rtl[7] = 0; $this->rtl[8] = 0; foreach ($this->JOB["todo"] as $tab) { if ($tab[0] == "TA" && $tab[2] != "SKIP") { if ($prefix) { $tab[1] = preg_replace("/^{$this->JOB['prefix_from']}(.+?)/", "{$this->JOB['prefix_to']}\\1", $tab[1]); } $query("ALTER TABLE `{$tab[1]}` ENABLE KEYS") or sxd_my_error(); $query("COMMIT;") or sxd_my_error(); $this->rtl[1] = time(); $this->rtl[5] = $tab[1]; fseek($this->fh_rtl, 0); fwrite($this->fh_rtl, implode("\t", $this->rtl)); } } } else { $this->rtl[7] = 0; $this->rtl[8] = 0; } $this->rtl[4] = "EOJ"; $this->rtl[5] = round(array_sum(explode(" ", microtime())) - $this->rtl[11], 4); fseek($this->fh_rtl, 0); fwrite($this->fh_rtl, implode("\t", $this->rtl)); $this->addLog(sprintf($this->LNG["restore_end"], $this->JOB["db"])); fclose($this->fh_log); fclose($this->fh_rtl); }