JSP database backup Export code
function Export ($tables, $sqlcompat, $sqlcharset, $sizelimit, $action, $fileid, $random, $tableid, $startfrom, $ Tabletype)
{
$dumpcharset = $sqlcharset? $sqlcharset: Str_replace ('-', ', ', CHARSET);
$fileid = Isset ($fileid)? $fileid: 1;
if ($fileid ==1 && $tables)
{
if (!isset ($tables) | | |!is_array ($tables)) showmessage (' select_bak_table ');
$random = Mt_rand (1000, 9999);
Cache_write (' bakup_tables.php ', $tables);
}
Else
{
if (! $tables = Cache_read (' bakup_tables.php ')) showmessage (' select_bak_table ');
}
if ($this->db->version () > ' 4.1 ')
{
if ($sqlcharset)
{
$this->db->query ("SET NAMES '") $sqlcharset. "'; nn ");
}
if ($sqlcompat = = ' MYSQL40 ')
{
$this->db->query ("SET sql_mode= ' MYSQL40 '");
}
ElseIf ($sqlcompat = = ' MYSQL41 ')
{
$this->db->query ("SET sql_mode= '");
}
}
$tabledump = ';
$tableid = Isset ($tableid)? $tableid-1:0;
$startfrom = Isset ($startfrom)? Intval ($startfrom): 0;
for ($i = $tableid; $i < count ($tables) && strlen ($tabledump) < $sizelimit * 1000; $i + +)
{
Global $startrow;
$offset = 100;
if (! $startfrom)
{
$tabledump. = "DROP TABLE IF EXISTS ' $tables [$i] '; n";
$createtable = $this->db->query ("Show CREATE TABLE ' $tables [$i]");
$create = $this->db->fetch_row ($createtable);
$tabledump. = $create [1]. "; nn ";
if ($sqlcompat = = ' MYSQL41 ' && $this->db->version () < ' 4.1 ')
{
$tabledump = Preg_replace ("/type= ([a-za-z0-9]+)/", "Engine=\1 DEFAULT charset=". $dumpcharset, $tabledump);
}
if ($this->db->version () > ' 4.1 ' && $sqlcharset)
{
$tabledump = Preg_replace ("/(default) *s*charset=[a-za-z0-9]+/", "Default charset=". $sqlcharset, $tabledump);
}
}
$numrows = $offset;
while (strlen ($tabledump) < $sizelimit * 1000 && $numrows = = $offset)
{
$rows = $this->db->query ("select * from ' $tables [$i] ' LIMIT $startfrom, $offset");
$numfields = $this->db->num_fields ($rows);
$numrows = $this->db->num_rows ($rows);
while ($row = $this->db->fetch_row ($rows))
{
$comma = "";
$tabledump. = "INSERT into" $tables [$i] ' VALUES (";
for ($j = 0; $j < $numfields; $j + +)
{
$tabledump. = $comma. "'". Mysql_escape_string ($row [$j]). "'";
$comma = ",";
}
$tabledump. = "); n";
}
$this->db->free_result ($rows);
$startfrom + + $offset;
}
$tabledump. = "n";
$startrow = $startfrom;
$startfrom = 0;
}
if (Trim ($tabledump))
{
$tabledump = "#bakfilen # version:couvoln# Time:". Date (' y-m-d h:i:s '). " n# type:hl_databasen# email:263645928@qq.comn# sourc:http://www.u731.com/?1n#------------------------------------ --------------------nnn ". $tabledump;
$tableid = $i;
$filename = ' Hl_ '. Date (' Ymd '). _ '. $random. ' _ '. $fileid. '. SQL ';
$fileid + +;
$bakfile = Phpcms_root. ' /data/bakup/'. $filename;
if (!is_writable (phpcms_root.) /data/bakup/') showmessage (' data cannot be backed up to the server! Please check '. $bakfile. ' Directory is writable ', $forward);
File_put_contents ($bakfile, $tabledump);
@chmod ($bakfile, 0777);
ShowMessage (' Bak_file '. "$filename". " Database backup completed! ', ' mod= '. $this->mod. ' &file= '. $this->file. ' &action= '. $action. ' &sizelimit= '. $sizelimit. ' &sqlcompat= '. $sqlcompat. ' &sqlcharset= '. $sqlcharset. ' &tableid= '. $tableid. ' &fileid= '. $fileid. ' &startfrom= '. $startrow. ' &random= '. $random. ' &dosubmit=1&tabletype= '. $tabletype);
}
Else
{
Cache_delete (' bakup_tables.php ');
ShowMessage (' database_bak_success ', '? File=database&action=export ');
}
}