CI資料庫備份與還原入門小例子
阿新 • • 發佈:2018-12-31
function backup(){
function backup(){
$this->load->dbutil();
$this->load->helper('file');
$prefs = array(
'tables' => array(), // 包含了需備份的表名的陣列.
'ignore' => array(), // 備份時需要被忽略的表
'format' => 'txt',
// gzip, zip, txt
'filename' => 'mybackup.sql', // 檔名 - 如果選擇了ZIP壓縮,此項就是必需的
'add_drop' => TRUE, // 是否要在備份檔案中新增 DROP TABLE 語句
'add_insert' => TRUE, // 是否要在備份檔案中新增 INSERT 語句
'newline' => "\\n"
// 備份檔案中的換行符
);
$backup = $this->dbutil->backup($prefs);
write_file('./database.sql', $backup);
}
function restore(){
$this->load->helper('file');
//$content = read_file('./database.sql');
//$content = preg_replace("/#(.*)\\s#(.*)TABLE(.*)(.*)\\s#/i","",$content);//去掉註釋部分
$conn=mysql_connect("localhost","root","");//指定資料庫連線引數
mysql_select_db("test");
$file = BASEPATH . "database.sql";
mysql_query("source '".$file."';");
mysql_close($conn);
/*$sqls = explode(";\\r",$content);
foreach($sqls as $sql){
if(str_replace(" ","",$sql)){
$this->db->query($sql);
}
}*/
}
$this->load->dbutil();
$this->load->helper('file');
$prefs = array(
'tables' => array(), // 包含了需備份的表名的陣列.
'ignore' => array(), // 備份時需要被忽略的表
'format' =>
'filename' => 'mybackup.sql', // 檔名 - 如果選擇了ZIP壓縮,此項就是必需的
'add_drop' => TRUE, // 是否要在備份檔案中新增 DROP TABLE 語句
'add_insert' => TRUE, // 是否要在備份檔案中新增 INSERT 語句
'newline'
);
$backup = $this->dbutil->backup($prefs);
write_file('./database.sql', $backup);
}
function restore(){
$this->load->helper('file');
//$content = read_file('./database.sql');
//$content = preg_replace("/#(.*)\\s#(.*)TABLE(.*)(.*)\\s#/i","",$content);//去掉註釋部分
$conn=mysql_connect("localhost","root","");//指定資料庫連線引數
mysql_select_db("test");
$file = BASEPATH . "database.sql";
mysql_query("source '".$file."';");
mysql_close($conn);
/*$sqls = explode(";\\r",$content);
foreach($sqls as $sql){
if(str_replace(" ","",$sql)){
$this->db->query($sql);
}
}*/
}