ThinkPHP5.0_tp5数据库的备份、下载、还原、删除

参考文章:

TP---数据库的备份、下载、还原、删除

(这篇文章写的不是特别详细,个人感觉,但是,没有这篇文章就没有我的这篇文章)

https://blog.csdn.net/qq_42455095/article/details/84313754#commentBox

tp5进行数据库的备份、还原等一系列操作

(这篇文章还是挺详细的,不过和第一篇比还是比较细腻的,页面没有第一篇好看)

https://blog.csdn.net/qq_42249896/article/details/85238523

 

Baksql.php

config = $config;
        
        $this->begin = microtime(true);
        header("Content-type: text/html;charset=utf-8");
        $this->connect();
    }
    //首次进行pdo连接
    private function connect() {
        try{
           $this->handler =new \PDO("{$this->config['type']}:host={$this->config['hostname']};port={$this->config['hostport']};dbname={$this->config['database']};",
                $this->config['username'],
                $this->config['password'], 
                array(
                    \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$this->config['charset']};",
                    \PDO::ATTR_ERRMODE =>  \PDO::ERRMODE_EXCEPTION, 
                    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
                )); 
        }catch (PDOException $e) {
            die ("Error!: " . $e->getMessage() . "
"); } } /** * 查询 * @param string $sql * @return mixed */ private function query($sql = '') { $stmt = $this->handler->query($sql); $stmt->setFetchMode(\PDO::FETCH_NUM); $list = $stmt->fetchAll(); return $list; } /** * 获取全部表 * @param string $dbName * @return array */ private function get_dbname($dbName = '*') { $sql = 'SHOW TABLES'; $list = $this->query($sql); $tables = array(); foreach ($list as $value) { $tables[] = $value[0]; } return $tables; } /** * 获取表定义语句 * @param string $table * @return mixed */ private function get_dbhead($table = '') { $sql = "SHOW CREATE TABLE `{$table}`"; $ddl = $this->query($sql)[0][1] . ';'; return $ddl; } /** * 获取表数据 * @param string $table * @return mixed */ private function get_dbdata($table = '') { $sql = "SHOW COLUMNS FROM `{$table}`"; $list = $this->query($sql); //字段 $columns = ''; //需要返回的SQL $query = ''; foreach ($list as $value) { $columns .= "`{$value[0]}`,"; } $columns = substr($columns, 0, -1); $data = $this->query("SELECT * FROM `{$table}`"); foreach ($data as $value) { $dataSql = ''; foreach ($value as $v) { $dataSql .= "'{$v}',"; } $dataSql = substr($dataSql, 0, -1); $query .= "INSERT INTO `{$table}` ({$columns}) VALUES ({$dataSql});\r\n"; } return $query; } /** * 写入文件 * @param array $tables * @param array $ddl * @param array $data */ private function writeToFile($tables = array(), $ddl = array(), $data = array()) { $str = "/*\r\nMySQL Database Backup Tools\r\n"; $str .= "Server:{$this->config['hostname']}:{$this->config['hostport']}\r\n"; $str .= "Database:{$this->config['database']}\r\n"; $str .= "Data:" . date('Y-m-d H:i:s', time()) . "\r\n*/\r\n"; $str .= "SET FOREIGN_KEY_CHECKS=0;\r\n"; $i = 0; foreach ($tables as $table) { $str .= "-- ----------------------------\r\n"; $str .= "-- Table structure for {$table}\r\n"; $str .= "-- ----------------------------\r\n"; $str .= "DROP TABLE IF EXISTS `{$table}`;\r\n"; $str .= $ddl[$i] . "\r\n"; $str .= "-- ----------------------------\r\n"; $str .= "-- Records of {$table}\r\n"; $str .= "-- ----------------------------\r\n"; $str .= $data[$i] . "\r\n"; $i++; } if(!file_exists($this->config['path'])){mkdir($this->config['path']);} return file_put_contents($this->config['path'].$this->config['sqlbakname'], $str) ? '备份成功!花费时间' . round(microtime(true) - $this->begin,2) . 'ms' : '备份失败!'; } /** * 设置要备份的表 * @param array $tables */ private function setTables($tables = array()) { if (!empty($tables) && is_array($tables)) { //备份指定表 $this->tables = $tables; } else { //备份全部表 $this->tables = $this->get_dbname(); } } /** * 备份 * @param array $tables * @return bool */ public function backup($tables = array()) { //存储表定义语句的数组 $ddl = array(); //存储数据的数组 $data = array(); $this->setTables($tables); if (!empty($this->tables)) { foreach ($this->tables as $table) { $ddl[] = $this->get_dbhead($table); $data[] = $this->get_dbdata($table); } //开始写入 return $this->writeToFile($this->tables, $ddl, $data); } else { $this->error = '数据库中没有表!'; return false; } } /** * 错误信息 * @return mixed */ public function getError() { return $this->error; } public function restore($filename = '') { $path=$this->config['path'].$filename; if (!file_exists($path)) { $this->error('SQL文件不存在!'); return false; } else { $sql = $this->parseSQL($path); //dump($sql);die; try { $this->handler->exec($sql); echo '还原成功!花费时间', round(microtime(true) - $this->begin,2) . 'ms'; } catch (PDOException $e) { $this->error = $e->getMessage(); return false; } } } /** * 解析SQL文件为SQL语句数组 * @param string $path * @return array|mixed|string */ private function parseSQL($path = '') { $sql = file_get_contents($path); $sql = explode("\r\n", $sql); //先消除--注释 $sql = array_filter($sql, function ($data) { if (empty($data) || preg_match('/^--.*/', $data)) { return false; } else { return true; } }); $sql = implode('', $sql); //删除/**/注释 $sql = preg_replace('/\/\*.*\*\//', '', $sql); return $sql; } /** * 下载备份 * @param string $fileName * @return array|mixed|string */ public function downloadFile($fileName) { $fileName=$this->config['path'].$fileName; if (file_exists($fileName)){ ob_end_clean(); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Length: ' . filesize($fileName)); header('Content-Disposition: attachment; filename=' . basename($fileName)); readfile($fileName); }else{ $this->error="文件有错误!"; } } /** * 获取文件是时间 * @param string $file * @return string */ private function getfiletime($file){ $path=$this->config['path'].$file; $a = filemtime($path); $time = date("Y-m-d H:i:s", $a); return $time; } /** * 获取文件是大小 * @param string $file * @return string */ private function getfilesize($file){ $perms=stat($this->config['path'].$file); $size = $perms['size']; $a = ['B', 'KB', 'MB', 'GB', 'TB']; $pos = 0; while ($size >= 1024) { $size /= 1024; $pos++; } return round($size, 2). $a[$pos]; } /** * 获取文件列表 * @param string $Order 级别 * @return array */ public function get_filelist($Order = 0) { $FilePath=$this->config['path']; // print_r($FilePath);die; $FilePath = opendir($FilePath); // $FilePath = scandir($FilePath); $FileAndFolderAyy=array(); $i=1; while (false !== ($filename = readdir($FilePath))) { if ($filename!="." && $filename!=".."){ $i++; $FileAndFolderAyy[$i]['name'] = $filename; $FileAndFolderAyy[$i]['time'] = $this->getfiletime($filename); $FileAndFolderAyy[$i]['size'] = $this->getfilesize($filename); } } $Order == 0 ? sort($FileAndFolderAyy) : rsort($FileAndFolderAyy); return $FileAndFolderAyy; } public function delfilename($filename){ $path=$this->config['path'].$filename; if (@unlink($path)) {return '删除成功';} } } ?>

 

控制器文件:

backup();
                $this->success("$info", 'index/Ceshibaksql/index');
                break;
            //下载
            case "dowonload":
                $info = $backup->downloadFile($name);
                $this->success("$info", 'index/Ceshibaksql/index');
                break;
            //还原
            case "restore":
                $info = $backup->restore($name);
                $this->success("$info", 'index/Ceshibaksql/index');
                break;
            //删除
            case "del":
                $info = $backup->delfilename($name);
                $this->success("$info", 'index/Ceshibaksql/index');
                break;
            //如果没有操作,则查询已备份的所有数据库信息
            default:
                return $this->fetch("Ceshibaksql/index", ["list" => array_reverse($backup->get_filelist())]);//将信息由新到老排序
        }
    }
}

视图文件:


 



{volist name="list" id="vo"} {/volist}
序号 备份名称 备份时间 备份大小 操作
{$key+1} {$vo.name} {$vo.time} {$vo.size} 下载 还原 删除

重点说明:

根据第一篇参考文章我改动的地方

我把Baksql.php文件放到了app>index>common 文件夹下了

所以在顶部有了这两句代码

namespace app\index\common;
use think\Controller;

控制器Ceshibaksql.php文件,必须引用Baksql.php文件

namespace app\index\controller;

use think\Controller; 
use app\index\common\Baksql;  

在控制器Ceshibaksql.php文件读取数据库配置信息的时候代码必须这样写

$backup = new Baksql(\think\Config::get("database"));

另外注意,视图数据赋值的时候,是用读取到的数据库$backup变量进行渲染数据的

  return $this->fetch("Ceshibaksql/index", ["list" => array_reverse($backup->get_filelist())]);//将信息由新到老排序

操作成功后要返回的路径,其实就是你的控制器Ceshibaksql.php文件的index()方法

 $this->success("$info", 'index/Ceshibaksql/index');

原来的Baksql.php文件的代码第15行是(这个路径就是备份数据库的文件路径)

  $config['path']=ROOT_PATH . 'public' . DS .'static'. DS .'data/'; //默认目录

这个只适合服务器的那种网站配置,

而我的是虚拟机服务器,我改成了

$config['path']=ROOT_PATH . 'static'. DS .'data/'; //默认目录

 

大坑

发现数据生成后,进行还原成功,数据库里有数据,但是,数据读取不出来;

经过数据库生成的代码程序生成的代码进行对比发现,

1.程序生成的代码,字段没有字符集;主要出现在varchar类型

2.程序生成的代码,int字段的数据被加单引号''

varchar

-- ----------------------------
-- 程序生成
-- Table structure for admin_user
-- ----------------------------
DROP TABLE IF EXISTS `admin_user`;
CREATE TABLE `admin_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '人员ID',
  `groups_id` int(11) DEFAULT '2' COMMENT '管理组ID',
  `admin_class_id` int(11) NOT NULL COMMENT '父ID',
  `admin_name` varchar(255) DEFAULT NULL COMMENT '登录名',
-- ----------------------------
-- 数据库生成
-- Table structure for admin_user
-- ----------------------------
DROP TABLE IF EXISTS `admin_user`;
CREATE TABLE `admin_user`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '人员ID',
  `groups_id` int(11) NULL DEFAULT 2 COMMENT '管理组ID',
  `admin_class_id` int(11) NOT NULL COMMENT '父ID',
  `admin_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '登录名',

int

-- ----------------------------
-- 程序生成
-- ----------------------------
INSERT INTO `admin_user` (`id`,`groups_id`,`admin_class_id`,`admin_name`,`admin_password`,`name`,`phone`,`state`,`login_count`,`last_time`,`delete_time`,`add_date`,`alter_date`) VALUES ('1','1','1','admin','0192023a7bbd73250516f069df18b500','超级管理员','18866668886','1','83','1544512329','','599590861','1544512315');
-- ----------------------------
-- 数据库生成 
-- ----------------------------

INSERT INTO `admin_user` VALUES (1, 1, 1, 'admin', '0192023a7bbd73250516f069df18b500', '超级管理员', '18866668886', 1, 83, 1544512329, NULL, 599590861, 1544512315);

 

解决方案

修改表数据get_dbdata()方法

 //原来的代码
// foreach ($value as $v)
// {
//     $dataSql .= "'{$v}',";
// }
            /** 
             * 2019-01-16 程鹏qq527592435 修改后
             * */ 

            //cp循环次数
            $cp_loop_time=0;
            foreach ($value as $v)
            {
                if(strpos($list[$cp_loop_time][1],'int') !== false || strpos($list[$cp_loop_time][1],'double') !== false )
                {
                    if($v==null)
                    { 
                        $dataSql .= "NULL,"; 
                    }
                    else{
                        $dataSql .= "{$v},";
                    }
                }
                else{
                $dataSql .= "'{$v}',";
                  }
                $cp_loop_time++;
            }
             /** 
             * 2019-01-16 程鹏qq527592435 修改结束
             * */ 

修改后BUG修复!

 

遇见特殊字符转义

感谢:@程wasd 的支持贡献!

 场景:/cms/public/conffile/20190201\0a3dbcaacbd2eb0a71bf1ec4324af1b3.png

比如这种路径中,如数数据库时候路径中的反斜杠会丢失,所以导出的时候要再给他加一个反斜杠

$value=array_map('addslashes', $value);//给预定义字符加反斜杠

 

 

你可能感兴趣的:(PHP)