| // +------------------------------------------------+ namespace tools; /** * 数据导出工具 */ class Database { /** * 文件指针 * @var resource */ private $fp; /** * 备份文件信息 part - 卷号,name - 文件名 * @var array */ private $file; /** * 当前打开文件大小 * @var integer */ private $size = 0; /** * 备份配置 * @var integer */ private $config = [ 'path' => '', // 数据库备份根路径 'part' => 2097152, // 数据库备份卷大小 2Mb 'compress' => true, // 数据库备份文件是否启用压缩 'level' => 9, // 数据库备份文件压缩级别 'line' => 1000, ]; /** * 数据库备份构造方法 * @param array $file 备份或还原的文件信息 * @param array $config 备份配置信息 */ public function __construct($file, $config) { $this->file = $file; $this->config = array_merge($this->config, $config); } /** * 打开一个卷,用于写入数据 * @param integer $size 写入数据的大小 */ private function open($size) { if ($this->fp) { $this->size += $size; if ($this->size > $this->config['part']) { $this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp); $this->fp = null; $this->file['part']++; session('backup_file', $this->file); $this->create(); } } else { $backuppath = $this->config['path']; $filename = "{$backuppath}{$this->file['name']}-{$this->file['part']}.sql"; if ($this->config['compress']) { $filename = "{$filename}.gz"; $this->fp = @gzopen($filename, "a{$this->config['level']}"); } else { $this->fp = @fopen($filename, 'a'); } $this->size = filesize($filename) + $size; } } /** * 写入初始数据 * @return boolean true - 写入成功,false - 写入失败 */ public function create() { $sql = "-- -----------------------------\n"; $sql .= "-- c.Jango MySQL Data Transfer\n"; $sql .= "--\n"; $sql .= "-- Host : " . \think\Config::get('database.hostname') . "\n"; $sql .= "-- UserName : " . \think\Config::get('database.username') . "\n"; $sql .= "-- Database : " . \think\Config::get('database.database') . "\n"; $sql .= "--\n"; $sql .= "-- Part : #{$this->file['part']}\n"; $sql .= "-- Date : " . date("Y-m-d H:i:s") . "\n"; $sql .= "-- -----------------------------\n\n"; return $this->write($sql); } /** * 写入SQL语句 * @param string $sql 要写入的SQL语句 * @return boolean true - 写入成功,false - 写入失败! */ private function write($sql) { $size = strlen($sql); // 由于压缩原因,无法计算出压缩后的长度,这里假设压缩率为50%, // 一般情况压缩率都会高于50%; $size = $this->config['compress'] ? $size / 2 : $size; $this->open($size); return $this->config['compress'] ? @gzwrite($this->fp, $sql) : @fwrite($this->fp, $sql); } /** * 备份表结构 * @param string $table 表名 * @param integer $start 起始行数 * @return boolean false - 备份失败 */ public function backup($table, $start) { // 创建DB对象 $db = \think\Loader::db(); // 备份表结构 if (0 == $start) { $result = $db->query("SHOW CREATE TABLE `{$table}`"); $result = array_change_key_case($result[0]); $sql = "\n"; $sql .= "-- -----------------------------\n"; $sql .= "-- Table structure for `{$table}`\n"; $sql .= "-- -----------------------------\n"; $sql .= "SET FOREIGN_KEY_CHECKS = 0;\n"; $sql .= "DROP TABLE IF EXISTS `{$table}`;\n"; $sql .= trim($result['create table']) . ";\n\n"; if (false === $this->write($sql)) { return false; } } // 数据总数 $result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`"); $count = $result[0]['count']; $line = $this->config['line']; // 备份表数据 if ($count) { // 写入数据注释 if (0 == $start) { $sql = "-- -----------------------------\n"; $sql .= "-- Records of `{$table}`\n"; $sql .= "-- -----------------------------\n"; $this->write($sql); } // 备份数据记录 $result = $db->query("SELECT * FROM `{$table}` LIMIT {$start}, $line"); $columns = $db->query("SHOW COLUMNS FROM `{$table}`"); $columns = array_column($columns, 'Field'); $columns = implode('`, `', $columns); $sql = "INSERT INTO `{$table}` (`{$columns}`) VALUES\r\n"; foreach ($result as $row) { $sql .= "('" . str_replace(["\r", "\n"], ['\r', '\n'], implode("', '", $row)) . "'),\r\n"; } $sql = rtrim(rtrim($sql), ',') . ";\r\n"; if (false === $this->write($sql)) { return false; } // 还有更多数据 if ($count > $start + $line) { return [$start + $line, $count]; } } // 备份下一表 return 0; } /** * 还原数据库 * @param integer $start [description] * @return [type] [description] */ public function import($start = 0) { //还原数据 $db = \think\Loader::db(); if ($this->config['compress']) { $gz = gzopen($this->file[1], 'r'); $size = 0; } else { $size = filesize($this->file[1]); $gz = fopen($this->file[1], 'r'); } $sql = ''; if ($start) { $this->config['compress'] ? gzseek($gz, $start) : fseek($gz, $start); } for ($i = 0; $i < 1000; $i++) { $sql .= $this->config['compress'] ? gzgets($gz) : fgets($gz); if (preg_match('/.*;$/', trim($sql))) { if (false !== $db->execute($sql)) { $start += strlen($sql); } else { return false; } $sql = ''; } elseif ($this->config['compress'] ? gzeof($gz) : feof($gz)) { return 0; } } return [$start, $size]; } /** * 析构方法,用于关闭文件资源 */ public function __destruct() { if ($this->config['compress'] && !is_null($this->fp)) { @gzclose($this->fp); } elseif (!is_null($this->fp)) { @fclose($this->fp); } else { } } }