Created
September 6, 2013 21:02
-
-
Save NaszvadiG/6469991 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!defined('BASEPATH')) | |
exit('No direct script access allowed'); | |
class Backup { | |
private $path; | |
private $default; | |
var $CI; | |
public function getPath() { | |
return $this->path; | |
} | |
public function setPath($path) { | |
$this->path = $path; | |
} | |
public function initialize($Path = NULL){ | |
if ($Path) { | |
$this->path = $Path; | |
} | |
} | |
public function __construct($Path = NULL) { | |
$this->initialize($Path); | |
$this->CI = &get_instance(); | |
$this->CI->config->load('constantes'); | |
$this->default = $this->CI->config->item('configdb'); | |
$this->default = $this->default[0]; | |
} | |
public function __destruct() { | |
unset($path, $this->CI, $this->default, $this); | |
} | |
public function doBackup($layout_table = TRUE, $insert_table = TRUE, $save_sql = TRUE, $download = FALSE, $prefix=NULL){ | |
$tables = $this->CI->db | |
->query('SHOW TABLES') | |
->result_array(); | |
$nametb = ''; | |
for ($i=0; $i < count($tables); $i++) { | |
$n = array_values($tables[$i]); | |
if ($nametb===''){ | |
$nametb .= $n[0]; | |
} else { | |
$nametb .= ','.$n[0]; | |
} | |
} | |
$tables = explode(',', $nametb); | |
$return = ''; | |
foreach ($tables as $table) { | |
//CREATE TABLE LAYOUT | |
$result = $this->CI->db | |
->query('SELECT * FROM ' . $table); | |
if ($layout_table === TRUE) { | |
$return .= 'DROP TABLE ' . $table . ';'; | |
$row2 = $this->CI->db | |
->query('SHOW CREATE TABLE ' . $table) | |
->result_array(); | |
$return.= "\n\n" . $row2[0]['Create Table'] . ";\n\n"; | |
} | |
//CREATE INSERT | |
if ($insert_table === TRUE) { | |
foreach($result->result_array() as $row){ | |
$fields = implode(",", array_keys($row)); | |
$values = '"'.implode('","', array_values($row)).'"'; | |
$return.= 'INSERT INTO '.$table.'('.$fields.') VALUES('.$values.');'; | |
$return.= "\n"; | |
} | |
} | |
$return.="\n\n\n"; | |
} | |
//CREATE SAVE PATH ARQUIVO | |
$sql_save = $this->path.$prefix.'-db-backup.'.date('d_m_Y_G_i_s').'.sql'; | |
if ($save_sql === TRUE) { | |
chmod($this->path, 0777); | |
$handle = fopen($sql_save, 'w+'); | |
fwrite($handle, $return); | |
fclose($handle); | |
chmod($this->path, 0700); | |
} | |
if ($download === TRUE && file_exists($sql_save) && is_file($sql_save)) { | |
$this->CI->load->library('zip'); | |
$this->CI->zip->read_file($sql_save); | |
$this->CI->zip->download('db-backup.sql.zip'); | |
} | |
} | |
private function table_db_backup($Path = NULL) { | |
if ($Path) { | |
$this->path = $Path; | |
} | |
$dbhost = $this->default['localhost']; | |
$dbuser = $this->default['username']; | |
$dbpass = $this->default['password']; | |
$dbname = $this->default['database']; | |
$output = ''; | |
$filename = "database_backup_$dbname" . date('d-m-Y-G-i-s') . '.sql'; | |
exec("mysqldump -u $dbuser -p$dbpass $dbname > {$this->path}" . $filename, $output); | |
var_dump($output); | |
} | |
private function tables_db_backup($Path = NULL, $tables = '*') { | |
if ($Path) { | |
$this->path = $Path; | |
} | |
$host = $this->default['localhost']; | |
$user = $this->default['username']; | |
$pass = $this->default['password']; | |
$name = $this->default['database']; | |
$link = mysql_connect($host, $user, $pass); | |
mysql_select_db($name, $link); | |
//get all of the tables | |
if ($tables == '*') { | |
$tables = array(); | |
$result = mysql_query('SHOW TABLES'); | |
while ($row = mysql_fetch_row($result)) { | |
$tables[] = $row[0]; | |
} | |
} else { | |
$tables = is_array($tables) ? $tables : explode(',', $tables); | |
} | |
//cycle through | |
$return = ''; | |
foreach ($tables as $table) { | |
$result = mysql_query('SELECT * FROM ' . $table); | |
$num_fields = mysql_num_fields($result); | |
$return .= 'DROP TABLE ' . $table . ';'; | |
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table)); | |
$return.= "\n\n" . $row2[1] . ";\n\n"; | |
for ($i = 0; $i < $num_fields; $i++) { | |
while ($row = mysql_fetch_row($result)) { | |
$return.= 'INSERT INTO ' . $table . ' VALUES('; | |
for ($j = 0; $j < $num_fields; $j++) { | |
$row[$j] = addslashes($row[$j]); | |
$row[$j] = str_replace("\n", "\\n", $row[$j]); | |
if (isset($row[$j])) { | |
$return.= '"' . $row[$j] . '"'; | |
} else { | |
$return.= '""'; | |
} | |
if ($j < ($num_fields - 1)) { | |
$return.= ','; | |
} | |
} | |
$return.= ");\n"; | |
} | |
} | |
$return.="\n\n\n"; | |
} | |
//save file | |
$handle = fopen($this->path . '/db-backup-' . time() . '-' . (md5(implode(',', $tables))) . '.sql', 'w+'); | |
fwrite($handle, $return); | |
fclose($handle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment