/** * 打包下载备份包 *@param $file 文件路劲 *@examlpe */ public function downzip($file) { import('ORG.Util.phpzip'); $addzip = new phpzip(); import('ORG.Net.FileSystem'); $path = new FileSystem(); $path->root = ITEM; $path->charset = C('CFG_CHARSET'); load("@.download"); //main $file = strval($file); $realpath = CONF_PATH . 'Backup/' . $file; $bakfile = RUNTIME_PATH . 'Temp/Zip/'; if (!file_exists($bakfile)) { $path->putDir($bakfile); } $zipname = 'Backup_' . $file . '.zip'; $zippath = $bakfile . $zipname; $addzip->zip($realpath, $zippath); if (file_exists($zippath)) { download($zippath); $path->delFile($zippath); } unset($addzip, $path); }
/** * 备份数据库 */ public function dbbak() { @set_time_limit(0); $pageSize = 1000; $dataDir = "../#data/db/"; if (!empty($_POST['btnSubmit'])) { if (!count($_POST['arrTables'])) { $this->assign("msg", "请选择要备份的数据表。"); $this->assign("backUrl", "dbbak"); $this->display("_msg"); exit; } if (!file_exists($dataDir)) { $this->assign("msg", "数据备份目录 " . $dataDir . " 不存在。"); $this->assign("backUrl", "dbbak"); $this->display("_msg"); exit; } if (!is_writeable($dataDir)) { $this->assign("msg", "数据备份目录 " . $dataDir . " 不不可写。"); $this->assign("backUrl", "dbbak"); $this->display("_msg"); exit; } $filename = date("Ymd-His") . ".sql"; $file = $dataDir . $filename; $i = 0; for (; $i < count($_POST['arrTables']); ++$i) { $table_one = $_POST['arrTables'][$i]; $table = str_replace("jee_", "", $table_one); $table_num = strlen($table); $table_num = $table_num - 1; $table = substr($table, 0, $table_num); $tables = M("{$table}"); $recCount = $tables->select(); $recCount = count($recCount); $pageCount = ceil($recCount / $pageSize); $pageIndex = 1; for (; $pageIndex <= $pageCount; ++$pageIndex) { $recFrom = ($pageIndex - 1) * $pageSize; $arr = $tables->select(); $rowsCount = count($arr); if (!(0 < $rowsCount)) { continue; } $j = 0; for (; $j < $rowsCount; ++$j) { $arrValues = array(); foreach ($arr[$j] as $value) { if (NULL === $value) { $arrValues[] = "NULL"; } else { $value = "'" . addslashes($value) . "'"; $value = str_replace("\r", "\\r", $value); $value = str_replace("\n", "\\n", $value); $arrValues[] = $value; } } writesql($file, "REPLACE INTO `" . $table_one . "` VALUES (" . implode(", ", $arrValues) . ");\n"); } } writesql($file, "\n"); } $filename = eregi_replace(".sql\$", ".zip", $filename); $zip = new phpzip(); $zip->zip($file, $dataDir . $filename); unlink($file); $this->assign("msg", "数据备份成功!<br/>备份文件为:<a href='#'>{$filename}</a>"); $this->assign("backUrl", "dbbak"); $this->display('_msg'); exit; } // $arr = $db->fetchrows( "SHOW TABLE STATUS" ); $Model = new Model(); $arr = $Model->query("SHOW TABLE STATUS"); //dump($arr); $this->assign("arr", $arr); $this->display(); }