Reply to Expensive function for restoring GBytes of data into database!
Expensive function for restoring GBytes of data into database! Posted by Alexander Shatalov Nobody has this except Ajaxel CMS, why didn't, I don't know)) But I did it.. PHP private function run_restore() { $this->fromSession(); $this->start = true; if ($this->ext=='gz') { $func_open = 'gzopen'; $func_close = 'gzclose'; $func_seek = 'gzseek'; $func_tell = 'gztell'; $func_getc = 'gzgetc'; $func_gets = 'gzgets'; } else { $func_open = 'fopen'; $func_close = 'fclose'; $func_seek = 'fseek'; $func_tell = 'ftell'; $func_getc = 'fgetc'; $func_gets = 'fgets'; } if (!$this->db_name) $this->db_name = DB_NAME; if (!$this->total) { if ($this->ext=='gz') { $fp = fopen($this->backup_path.$this->filename, 'rb'); fseek($fp, -4, SEEK_END); $unpack = unpack('V', fread($fp, 4)); $this->total = end($unpack); unset($unpack); fclose($fp); if (!$this->total) $this->total = filesize($this->backup_path.$this->filename) * 9.5; } else $this->total = filesize($this->backup_path.$this->filename); } $handle = $func_open($this->backup_path.$this->filename,'r'); if (!$handle) { $this->error = 'ERROR: Couldn\'t open '.$this->backup_path.$this->filename.''; return $this->msg('Error in opening the file'); } $func_seek($handle,$this->aff); /* if ($tell!=$this->aff) { $this->error = $tell.' != '.$this->aff.', ftell and fseek mismatch with '.$func_tell.''; return $this->msg('For fuck sake!'); } */ $sql = ''; $i = 0; $size = 0; $prev = ''; if ($this->db_name) { mysql_select_db($this->db_name); } while (($buffer = $func_gets($handle))!==false) { if (!$buffer) continue; $s2 = substr($buffer,0,2); if ($s2==='/*') continue; $s7 = substr($buffer,0,7); $insert = false; if (!$this->aff && !$i) { $i++; $size += strlen($buffer); continue; } if ($s7==='INSERT ' || $s7==='UPDATE ') { $prev = $buffer; $sql .= $prev; $insert = true; } if ($s2==='--' || $insert) { if ($this->allow_restore_sql($sql)) { $sql = self::prefix($sql); @mysql_query($sql); $this->restored_sql(); if ($e = mysql_error()) { $this->aff = $func_tell($handle); if ($this->count_as_error($e)) { $this->errors++; if ($this->errors > 100) { $tell = $func_tell($handle); $this->error = $e.' '.$sql.' ('.$tell.' < '.$this->aff.' ?)'; $this->start = false; $this->aff = 0; $this->total = 0; $this->backup = array(); } return $this->msg('MySQL ERROR: '.$e.' '.$sql); } } } $sql = ''; if ($size > self::MAX_RESTORE_SIZE) { $this->aff = $func_tell($handle); $this->toSession(); $i = 0; $ex = explode(' ',$prev,5); return $this->msg($ex[0].' '.$ex[1].' '.$ex[2].' ('.File::display_size($this->aff, true).')'); } $size += strlen($buffer); $i++; } else { $prev = self::prefix($buffer); $sql .= $prev; } } $func_close($handle); $this->backup = array(); $this->start = false; $this->done = true; $this->toSession(); if (!$this->aff && !$i) { $this->error = 'ERROR: Couldn\'t open '.$this->backup_path.$this->filename.'. File is damaged.'; return $this->msg('Error in opening the file'); } return $this->msg('Backup file '.$this->filename.' has been imported to '.$this->db_name.' database.'); } |