function backup($bakfile = null, $tables = array())
{
if (empty($bakfile)) {
$bakfile = $this->dbname . date(ymdhis) . '.sql';
} elseif (is_dir($bakfile)) {
if (preg_match('//$/', $bakfile)) {
$bakfile = $bakfile . $this->dbname . date(ymdhis) . '.sql';
} else {
$bakfile = $bakfile . '/' . $this->dbname . date(ymdhis) . '.sql';
}
}
if (!$tables) {
$this->query(show tables);
while ($row = mysql_fetch_row($this->queryid)) {
$tables[] = $row[0];
}
} else {
foreach ($tables as $k => $v) {
$tables[$k] = $this->tableprefix . $v;
}
}
if ($fp = fopen($bakfile, 'wb')) {
if ($this->dbcharset) {
fwrite($fp, set names . $this->dbcharset . ;nn);
}
foreach ($tables as $table) {
$this->dumptable($table, $fp);
fwrite($fp, n);
}//foreach
fclose($fp);
return true;
} else {
return false;
}//if
}
//私有方法 导出表格
function dumptable($fulltablename, $fp)
{
//备份表结构
fwrite($fp, -- n-- {$fulltablename}n-- n);
$row = $this->findbysql(show create table `{$fulltablename}`);
fwrite($fp, $row['create table'] . ;nn );
//备份表库数据
$this->query(select * from `{$fulltablename}`);
while ($row = mysql_fetch_assoc($this->queryid)) {
foreach ($row as $k=>$v) {
$row[$k] = ' . $this->qstr($v) . ' ;
}
$sql = insert into `$fulltablename` values ( . join(,, $row) . );n;
fwrite($fp, $sql);
}
mysql_free_result($this->queryid);
fwrite($fp, n);
}
//恢复数据库文件
function restore($bakfile)
{
if ($fp = fopen($bakfile, 'r')) {
$sql = '';
while (!feof($fp)) {
$line = fgets($fp);
if (strpos($line,'--')!==0)
{
$sql .= $line;
//pp($sql);
}
if (preg_match('/;s*$/', $sql)) {
$this->query($sql);
$sql = '';
}
}
fclose($fp);
return true;
} else {
return false;
}
}