%PDF- <> %âãÏÓ endobj 2 0 obj <> endobj 3 0 obj <>/ExtGState<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 28 0 R 29 0 R] /MediaBox[ 0 0 595.5 842.25] /Contents 4 0 R/Group<>/Tabs/S>> endobj ºaâÚÎΞ-ÌE1ÍØÄ÷{òò2ÿ ÛÖ^ÔÀá TÎ{¦?§®¥kuµùÕ5sLOšuY>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<> endobj 2 0 obj<>endobj 2 0 obj<>es 3 0 R>> endobj 2 0 obj<> ox[ 0.000000 0.000000 609.600000 935.600000]/Fi endobj 3 0 obj<> endobj 7 1 obj<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>>/Subtype/Form>> stream
<?php class Dbbackup extends CI_Model { function __construct() { parent::__construct(); ini_set('memory_limit', '-1'); set_time_limit(0); $this->mysqli=$this->dba->mysqli(); $this->filename="backup.sql"; $this->backup_path=UP_PATHF.'db_backups/'; $this->is_download=FALSE; } function unconvert_field($field, $return) { if (preg_match("~binary~", $field["type"])) { $return = "UNHEX($return)"; } if ($field["type"] == "bit") { $return = "CONV($return, 2, 10) + 0"; } if (preg_match("~geometry|point|linestring|polygon~", $field["type"])) { $return = (min_version(8) ? "ST_" : "") . "GeomFromText($return)"; } return $return; } function number_type() { return '((?<!o)int(?!er)|numeric|real|float|double|decimal|money)'; // not point, not interval } function q($v){ return $this->db->escape($v); } function idf_escape($idf) { return "`" . str_replace("`", "``", $idf) . "`"; } function dump_headers($type) { $output = $type; $ext = "sql"; header("Content-Type: " . ($output == "gz" ? "application/x-gzip" : ($ext == "tar" ? "application/x-tar" : ($ext == "sql" || $output != "file" ? "text/plain" : "text/csv") . "; charset=utf-8" ))); if ($output == "gz") { ob_start('ob_gzencode', 1e6); } return $ext; } function set_output($type='file'){ if(!$this->is_download){ return; } $identifier=$this->filename; $output = $type; $return = $this->dump_headers($type); if ($output != "text") { header("Content-Disposition: attachment; filename=$identifier" . ".$return" . ($output != "file" && !preg_match('~[^0-9a-z]~', $output) ? ".$output" : "")); } session_write_close(); ob_flush(); flush(); } function output($str){ if(!$this->is_download){ //write_file($this->backup_path.$this->filename, $str, "a"); file_put_contents($this->backup_path.$this->filename, $str, FILE_APPEND | LOCK_EX); }else{ echo $str; } } function dump_table($table, $style, $is_view = 0){ $create=""; if($is_view){ }else{ $create.=array_values($this->db->query("SHOW CREATE TABLE ".$table)->row_array())[1].";\n"; } if ($style && $create) { if ($style == "DROP+CREATE" || $is_view) { $create="DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " .$table. ";\n".$create."\n\n"; } $this->output($create); } } function dump_data($table, $style){ $max_packet = 1048576; $tbl_fields=$this->db->list_fields($table); $insert = ""; $buffer = ""; $keys = array(); $suffix = ""; $result=$this->mysqli->query("SELECT * FROM $table", 1); //1 - MYSQLI_USE_RESULT (for unbuffered query) if(!$result){ return; } while($row = $result->fetch_assoc()) { if(!$keys){ $values = array(); $i=0; foreach ($row as $val) { /*$field = $result->fetch_field(); $keys[] = $this->idf_escape($field->name); $key = $this->idf_escape($field->name);*/ $keys[]=$this->idf_escape($tbl_fields[$i]); $key=$this->idf_escape($tbl_fields[$i]); $values[] = "$key = VALUES($key)"; $i++; } $suffix=";\n"; } if(!$insert){ $insert = "INSERT INTO " . $table . " (" . implode(", ", $keys) . ") VALUES"; } foreach($row as $key => $val){ $field = $fields[$key]; $row[$key] = ($val !== null ? $this->unconvert_field($field, preg_match($this->number_type(), $field["type"]) && $val != '' && !preg_match('~\[~', $field["full_type"]) ? $val : $this->q(($val === false ? 0 : $val))) : "NULL" ); } $s = ($max_packet ? "\n" : " ") . "(" . implode(",\t", $row) . ")"; if (!$buffer) { $buffer = $insert . $s; } elseif ((strlen($buffer) + 4 + strlen($s) + strlen($suffix)) < $max_packet) { // 4 - length specification $buffer .= ",$s"; } else { $this->output($buffer . $suffix); $buffer = $insert . $s; } } if($buffer){ $this->output($buffer . $suffix); } } function takebackup($filename="", $backup_path="", $is_download=FALSE){ if($filename){ $this->filename=$filename; } if($backup_path){ $this->backup_path=$backup_path; } $this->is_download=$is_download; del_file($this->backup_path.$this->filename); $db=$this->db->database; $tables=$this->db->list_tables(); $config=[ 'output'=>'file', //gz 'format'=>'sql', 'db_style'=>'', 'routines'=>1, 'events'=>1, 'triggers'=>1, 'table_style'=>'DROP+CREATE', 'data_style'=>'INSERT', 'tables'=>$tables, 'data'=>$tables, ]; //$tables = array_flip((array) $config["tables"]) + array_flip((array) $config["data"]); $this->set_output($config['output']); $head="SET NAMES utf8; SET time_zone = '+00:00';\nSET foreign_key_checks = 0;\nSET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';\n\n"; //$head.="USE `$db`;\n\n"; $this->output($head); $out = ""; /** FUNCTION & PROCEDURE */ foreach (array("FUNCTION", "PROCEDURE") as $routine) { $rows=$this->db->query("SHOW $routine STATUS WHERE Db='".$db."'")->result_array(); if($rows){ foreach($rows as $r){ $create=array_values($this->db->query("SHOW CREATE $routine ".$r['Name'])->row_array())[1]; $out.="DROP $routine IF EXISTS ".$r['Name'].";;\n".$create.";;\n\n"; } } } /** EVENTS */ $rows=$this->db->query("SHOW EVENTS")->result_array(); if($rows){ foreach($rows as $r){ $create=array_values($this->db->query("SHOW CREATE EVENTS ".$r['Name'])->row_array())[1]; $out.="DROP EVENTS IF EXISTS ".$r['Name'].";;\n".$create.";;\n\n"; } } if($out){ $out="DELIMITER ;;\n\n$out" . "DELIMITER ;\n\n"; $this->output($out); } /** */ $table_status=$this->db->query("SHOW TABLE STATUS")->result_array(); if($table_status){ $views = array(); foreach($table_status as $r){ $name=$r['Name']; $table=in_array($name, (array) $config["tables"]); $data=in_array($name, (array) $config["data"]); if($r["Engine"] == "InnoDB") { // ignore internal comment, unnecessary since MySQL 5.1.21 $r["Comment"] = preg_replace('~(?:(.+); )?InnoDB free: .*~', '\1', $r["Comment"]); } if(!isset($r["Engine"])) { $r["Comment"] = ""; } $this->dump_table($name, ($table ? $config["table_style"] : ""), ($r['Engine']===null)); if($r['Engine']===null){ $views[] = $name; }elseif($data){ $this->dump_data($name, $config["data_style"]); } } foreach ($views as $view) { $this->dump_table($view, $config["table_style"], 1); } } //pr($config); } } //End of file