From fab05d3e3da680f824cf884037401908904ea82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=89=E9=A3=8E=E6=9D=A5?= <136327134@qq.com> Date: Mon, 17 Sep 2018 15:01:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0tp5er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vendor/composer/autoload_psr4.php | 4 +- vendor/composer/autoload_static.php | 8 +- vendor/composer/installed.json | 91 +++++ vendor/tp5er/tp5-databackup/.gitignore | 1 + vendor/tp5er/tp5-databackup/README.md | 106 +++++ vendor/tp5er/tp5-databackup/composer.json | 19 + vendor/tp5er/tp5-databackup/src/Backup.php | 454 +++++++++++++++++++++ 7 files changed, 680 insertions(+), 3 deletions(-) create mode 100644 vendor/tp5er/tp5-databackup/.gitignore create mode 100644 vendor/tp5er/tp5-databackup/README.md create mode 100644 vendor/tp5er/tp5-databackup/composer.json create mode 100644 vendor/tp5er/tp5-databackup/src/Backup.php diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php index 08d413a9..26b3de46 100644 --- a/vendor/composer/autoload_psr4.php +++ b/vendor/composer/autoload_psr4.php @@ -6,10 +6,11 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( + 'tp5er\\' => array($vendorDir . '/tp5er/tp5-databackup/src'), 'think\\migration\\' => array($vendorDir . '/topthink/think-migration/src'), 'think\\composer\\' => array($vendorDir . '/topthink/think-installer/src'), 'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'), - 'think\\' => array($vendorDir . '/topthink/think-image/src'), + 'think\\' => array($baseDir . '/thinkphp/library/think', $vendorDir . '/topthink/think-image/src'), 'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'), 'Symfony\\Component\\HttpFoundation\\' => array($vendorDir . '/symfony/http-foundation'), 'Symfony\\Bridge\\PsrHttpMessage\\' => array($vendorDir . '/symfony/psr-http-message-bridge'), @@ -24,6 +25,5 @@ return array( 'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'), 'FormBuilder\\' => array($vendorDir . '/xaboy/form-builder/src'), 'EasyWeChat\\' => array($vendorDir . '/overtrue/wechat/src'), - 'FormBuilder\\' => array($vendorDir . '/xaboy/form-builder/src'), 'Doctrine\\Common\\Cache\\' => array($vendorDir . '/doctrine/cache/lib/Doctrine/Common/Cache'), ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 4e503437..93e0a6ce 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -19,6 +19,7 @@ class ComposerStaticInit34eab264abc3ceb8f815f4aeec1c4520 public static $prefixLengthsPsr4 = array ( 't' => array ( + 'tp5er\\' => 6, 'think\\migration\\' => 16, 'think\\composer\\' => 15, 'think\\captcha\\' => 14, @@ -66,6 +67,10 @@ class ComposerStaticInit34eab264abc3ceb8f815f4aeec1c4520 ); public static $prefixDirsPsr4 = array ( + 'tp5er\\' => + array ( + 0 => __DIR__ . '/..' . '/tp5er/tp5-databackup/src', + ), 'think\\migration\\' => array ( 0 => __DIR__ . '/..' . '/topthink/think-migration/src', @@ -80,7 +85,8 @@ class ComposerStaticInit34eab264abc3ceb8f815f4aeec1c4520 ), 'think\\' => array ( - 0 => __DIR__ . '/..' . '/topthink/think-image/src', + 0 => __DIR__ . '/../..' . '/thinkphp/library/think', + 1 => __DIR__ . '/..' . '/topthink/think-image/src', ), 'Symfony\\Polyfill\\Mbstring\\' => array ( diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 673c88da..49d13217 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -832,6 +832,59 @@ "psr-7" ] }, + { + "name": "topthink/framework", + "version": "v5.0.20", + "version_normalized": "5.0.20.0", + "source": { + "type": "git", + "url": "https://github.com/top-think/framework.git", + "reference": "e20f6e8cadbbc6e246a0c121c89bfd278f30c76b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/top-think/framework/zipball/e20f6e8cadbbc6e246a0c121c89bfd278f30c76b", + "reference": "e20f6e8cadbbc6e246a0c121c89bfd278f30c76b", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "topthink/think-installer": "~1.0" + }, + "require-dev": { + "johnkary/phpunit-speedtrap": "^1.0", + "mikey179/vfsstream": "~1.6", + "phpdocumentor/reflection-docblock": "^2.0", + "phploc/phploc": "2.*", + "phpunit/phpunit": "4.8.*", + "sebastian/phpcpd": "2.*" + }, + "time": "2018-05-11T08:02:47+00:00", + "type": "think-framework", + "installation-source": "dist", + "autoload": { + "psr-4": { + "think\\": "library/think" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "liu21st", + "email": "liu21st@gmail.com" + } + ], + "description": "the new thinkphp framework", + "homepage": "http://thinkphp.cn/", + "keywords": [ + "framework", + "orm", + "thinkphp" + ] + }, { "name": "topthink/think-captcha", "version": "v1.0.7", @@ -996,6 +1049,44 @@ } ] }, + { + "name": "tp5er/tp5-databackup", + "version": "1.0.0", + "version_normalized": "1.0.0.0", + "source": { + "type": "git", + "url": "https://github.com/tp5er/tp5-databackup.git", + "reference": "10abd4383cf78844e1c22584fd823ef1d18e75dc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tp5er/tp5-databackup/zipball/10abd4383cf78844e1c22584fd823ef1d18e75dc", + "reference": "10abd4383cf78844e1c22584fd823ef1d18e75dc", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "time": "2017-12-29T05:36:58+00:00", + "type": "library", + "installation-source": "dist", + "autoload": { + "psr-4": { + "tp5er\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "tp5er", + "email": "tp5er@qq.com" + } + ], + "description": "Implement the database backup restore" + }, { "name": "xaboy/form-builder", "version": "1.2.0", diff --git a/vendor/tp5er/tp5-databackup/.gitignore b/vendor/tp5er/tp5-databackup/.gitignore new file mode 100644 index 00000000..57872d0f --- /dev/null +++ b/vendor/tp5er/tp5-databackup/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/vendor/tp5er/tp5-databackup/README.md b/vendor/tp5er/tp5-databackup/README.md new file mode 100644 index 00000000..7a9a187a --- /dev/null +++ b/vendor/tp5er/tp5-databackup/README.md @@ -0,0 +1,106 @@ + +### 使用composer进行安装 +~~~ + composer require tp5er/tp5-databackup dev-master +~~~ + +### 使用composer update进行安装 +~~~ + "require": { + "tp5er/tp5-databackup": "dev-master" + }, +~~~ + +### 引入类文件 +~~~ +use \tp5er\Backup; +~~~ + +### 配置文件 +~~~ +$config=array( + 'path' => './Data/',//数据库备份路径 + 'part' => 20971520,//数据库备份卷大小 + 'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩 + 'level' => 9 //数据库备份文件压缩级别 1普通 4 一般 9最高 +); +~~~ + +### 实例化 +~~~ + $db= new Backup($config); +~~~ + +### 文件命名规则,请严格遵守(温馨提示) +~~~ +$file=['name'=>date('Ymd-His'),'part'=>1] +~~~ + +### 数据类表列表 +~~~ +return $this->fetch('index',['list'=>$db->dataList()]); +~~~ +### 备份文件列表 +~~~ + return $this->fetch('importlist',['list'=>$db->fileList()]); +~~~ + + +### 备份表 +~~~ + $tables="数据库表1"; + $start= $db->setFile($file)->backup($tables[$id], 0); + 当$start返回0的时候就表示备份成功 +~~~ + +### 导入表 +~~~ + $start=0; + $start= $db->setFile($file)->import($start); +~~~ + +### 删除备份文件 +~~~ + $db->delFile($time); +~~~ + +### 下载备份文件 +~~~ + $db->downloadFile($time); +~~~ + +### 修复表 +~~~ + $db->repair($tables) +~~~ + +### 优化表 +~~~ + $db->optimize($tables) +~~~ + + + +### 大数据备份采取措施1 +~~~ +如果备份数据比较大的情况下,需要修改如下参数 +//默认php代码能够申请到的最大内存字节数就是134217728 bytes,如果代码执行的时候再需要更多的内存,根据情况定义指定字节数 +memory_limit = 1024M +//默认php代码申请到的超时时间是20s,如果代码执行需要更长的时间,根据代码执行的超时时间定义版本运行超时时间 +max_execution_time =1000 +~~~ + +### 大数据备份采取措施2 + +~~~ + 自由设置超时时间。支持连贯操作,该方法主要使用在表备份和还原中,防止备份还原和备份不完整 + //备份 + $time=0//表示不限制超时时间,直到程序结束,(慎用) + $db->setTimeout($time)->setFile($file)->backup($tables[$id], 0); + //还原 + $db->setTimeout($time)->setFile($file)->import($start); +~~~ + + +无论您是大神,还是小白都希望您们加群进行交流,共同学习共同进步。 +# 技术交流与bug提交QQ群:368683534!!!! diff --git a/vendor/tp5er/tp5-databackup/composer.json b/vendor/tp5er/tp5-databackup/composer.json new file mode 100644 index 00000000..dff3f5ba --- /dev/null +++ b/vendor/tp5er/tp5-databackup/composer.json @@ -0,0 +1,19 @@ +{ + "name": "tp5er/tp5-databackup", + "description": "Implement the database backup restore", + "license": "MIT", + "authors": [ + { + "name": "tp5er", + "email": "tp5er@qq.com" + } + ], + "require": { + "php": ">=5.4.0" + }, + "autoload": { + "psr-4": { + "tp5er\\": "src/" + } + } +} diff --git a/vendor/tp5er/tp5-databackup/src/Backup.php b/vendor/tp5er/tp5-databackup/src/Backup.php new file mode 100644 index 00000000..d8fd969a --- /dev/null +++ b/vendor/tp5er/tp5-databackup/src/Backup.php @@ -0,0 +1,454 @@ + +// | QQ Group: 368683534 +// +---------------------------------------------------------------------- +namespace tp5er; +use think\Db; +use think\Config; +class Backup +{ + /** + * 文件指针 + * @var resource + */ + private $fp; + /** + * 备份文件信息 part - 卷号,name - 文件名 + * @var array + */ + private $file; + /** + * 当前打开文件大小 + * @var integer + */ + private $size = 0; + + /** + * 数据库配置 + * @var integer + */ + private $dbconfig = array(); + /** + * 备份配置 + * @var integer + */ + private $config = array( + 'path' => './Data/', + //数据库备份路径 + 'part' => 20971520, + //数据库备份卷大小 + 'compress' => 0, + //数据库备份文件是否启用压缩 0不压缩 1 压缩 + 'level' => 9, + ); + /** + * 数据库备份构造方法 + * @param array $file 备份或还原的文件信息 + * @param array $config 备份配置信息 + */ + public function __construct($config = []) + { + $this->config = array_merge($this->config, $config); + //初始化文件名 + $this->setFile(); + //初始化数据库连接参数 + $this->setDbConn(); + //检查文件是否可写 + if (!$this->checkPath($this->config['path'])) { + throw new \Exception("The current directory is not writable"); + } + } + /** + * 设置脚本运行超时时间 + * 0表示不限制,支持连贯操作 + */ + public function setTimeout($time=null) + { + if (!is_null($time)) { + set_time_limit($time)||ini_set("max_execution_time", $time); + } + return $this; + } + /** + * 设置数据库连接必备参数 + * @param array $dbconfig 数据库连接配置信息 + * @return object + */ + public function setDbConn($dbconfig = []) + { + if (empty($dbconfig)) { + $this->dbconfig = config('database'); + //$this->dbconfig = Config::get('database'); + } else { + $this->dbconfig = $dbconfig; + } + return $this; + } + /** + * 设置备份文件名 + * @param Array $file 文件名字 + * @return object + */ + public function setFile($file = null) + { + if (is_null($file)) { + $this->file = ['name' => date('Ymd-His'), 'part' => 1]; + } else { + if (!array_key_exists("name", $file) && !array_key_exists("part", $file)) { + $this->file = $file['1']; + } else { + $this->file = $file; + } + } + return $this; + } + //数据类连接 + public static function connect() + { + return Db::connect(); + } + //数据库表列表 + public function dataList($table = null,$type=1) + { + $db = self::connect(); + if (is_null($table)) { + $list = $db->query("SHOW TABLE STATUS"); + } else { + if ($type) { + $list = $db->query("SHOW FULL COLUMNS FROM {$table}"); + }else{ + $list = $db->query("show columns from {$table}"); + } + } + return array_map('array_change_key_case', $list); + //$list; + } + //数据库备份文件列表 + public function fileList() + { + if (!is_dir($this->config['path'])) { + mkdir($this->config['path'], 0755, true); + } + $path = realpath($this->config['path']); + $flag = \FilesystemIterator::KEY_AS_FILENAME; + $glob = new \FilesystemIterator($path, $flag); + $list = array(); + foreach ($glob as $name => $file) { + if (preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql(?:\\.gz)?$/', $name)) { + $name = sscanf($name, '%4s%2s%2s-%2s%2s%2s-%d'); + $date = "{$name[0]}-{$name[1]}-{$name[2]}"; + $time = "{$name[3]}:{$name[4]}:{$name[5]}"; + $part = $name[6]; + if (isset($list["{$date} {$time}"])) { + $info = $list["{$date} {$time}"]; + $info['part'] = max($info['part'], $part); + $info['size'] = $info['size'] + $file->getSize(); + } else { + $info['part'] = $part; + $info['size'] = $file->getSize(); + } + $extension = strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION)); + $info['compress'] = $extension === 'SQL' ? '-' : $extension; + $info['time'] = strtotime("{$date} {$time}"); + $list["{$date} {$time}"] = $info; + } + } + return $list; + } + public function getFile($type = '', $time = 0) + { + // + if (!is_numeric($time)) { + throw new \Exception("{$time} Illegal data type"); + } + switch ($type) { + case 'time': + $name = date('Ymd-His', $time) . '-*.sql*'; + $path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name; + return glob($path); + break; + case 'timeverif': + $name = date('Ymd-His', $time) . '-*.sql*'; + $path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name; + $files = glob($path); + $list = array(); + foreach ($files as $name) { + $basename = basename($name); + $match = sscanf($basename, '%4s%2s%2s-%2s%2s%2s-%d'); + $gz = preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql.gz$/', $basename); + $list[$match[6]] = array($match[6], $name, $gz); + } + $last = end($list); + if (count($list) === $last[0]) { + return $list; + } else { + throw new \Exception("File {$files['0']} may be damaged, please check again"); + } + break; + case 'pathname': + return "{$this->config['path']}{$this->file['name']}-{$this->file['part']}.sql"; + break; + case 'filename': + return "{$this->file['name']}-{$this->file['part']}.sql"; + break; + case 'filepath': + return $this->config['path']; + break; + default: + $arr = array('pathname' => "{$this->config['path']}{$this->file['name']}-{$this->file['part']}.sql", 'filename' => "{$this->file['name']}-{$this->file['part']}.sql", 'filepath' => $this->config['path'], 'file' => $this->file); + return $arr; + } + } + //删除备份文件 + public function delFile($time) + { + if ($time) { + $file = $this->getFile('time', $time); + array_map("unlink", $this->getFile('time', $time)); + if (count($this->getFile('time', $time))) { + throw new \Exception("File {$path} deleted failed"); + } else { + return $time; + } + } else { + throw new \Exception("{$time} Time parameter is incorrect"); + } + } + /** + * 下载备份 + * @Author: 浪哥 <939881475@qq.com> + * @param string $time + * @param integer $part + * @return array|mixed|string + */ + public function downloadFile($time, $part = 0) + { + $file = $this->getFile('time', $time); + $fileName = $file[$part]; + 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 { + throw new \Exception("{$time} File is abnormal"); + } + } + public function import($start) + { + //还原数据 + $db = self::connect(); + 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 array($start, $size); + } + /** + * 写入初始数据 + * @return boolean true - 写入成功,false - 写入失败 + */ + public function Backup_Init() + { + $sql = "-- -----------------------------\n"; + $sql .= "-- Think MySQL Data Transfer \n"; + $sql .= "-- \n"; + $sql .= "-- Host : " . $this->dbconfig['hostname'] . "\n"; + $sql .= "-- Port : " . $this->dbconfig['hostport'] . "\n"; + $sql .= "-- Database : " . $this->dbconfig['database'] . "\n"; + $sql .= "-- \n"; + $sql .= "-- Part : #{$this->file['part']}\n"; + $sql .= "-- Date : " . date("Y-m-d H:i:s") . "\n"; + $sql .= "-- -----------------------------\n\n"; + $sql .= "SET FOREIGN_KEY_CHECKS = 0;\n\n"; + return $this->write($sql); + } + /** + * 备份表结构 + * @param string $table 表名 + * @param integer $start 起始行数 + * @return boolean false - 备份失败 + */ + public function backup($table, $start) + { + $db = self::connect(); + // 备份表结构 + if (0 == $start) { + $result = $db->query("SHOW CREATE TABLE `{$table}`"); + $sql = "\n"; + $sql .= "-- -----------------------------\n"; + $sql .= "-- Table structure for `{$table}`\n"; + $sql .= "-- -----------------------------\n"; + $sql .= "DROP TABLE IF EXISTS `{$table}`;\n"; + $sql .= trim($result[0]['Create Table']) . ";\n\n"; + if (false === $this->write($sql)) { + return false; + } + } + //数据总数 + $result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`"); + $count = $result['0']['count']; + //备份表数据 + 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}, 1000"); + foreach ($result as $row) { + $row = array_map('addslashes', $row); + $sql = "INSERT INTO `{$table}` VALUES ('" . str_replace(array("\r", "\n"), array('\\r', '\\n'), implode("', '", $row)) . "');\n"; + if (false === $this->write($sql)) { + return false; + } + } + //还有更多数据 + if ($count > $start + 1000) { + //return array($start + 1000, $count); + return $this->backup($table, $start + 1000); + } + } + //备份下一表 + return 0; + } + /** + * 优化表 + * @param String $tables 表名 + * @return String $tables + */ + public function optimize($tables = null) + { + if ($tables) { + $db = self::connect(); + if (is_array($tables)) { + $tables = implode('`,`', $tables); + $list = $db->query("OPTIMIZE TABLE `{$tables}`"); + } else { + $list = $db->query("OPTIMIZE TABLE `{$tables}`"); + } + if ($list) { + return $tables; + } else { + throw new \Exception("data sheet'{$tables}'Repair mistakes please try again!"); + } + } else { + throw new \Exception("Please specify the table to be repaired!"); + } + } + /** + * 修复表 + * @param String $tables 表名 + * @return String $tables + */ + public function repair($tables = null) + { + if ($tables) { + $db = self::connect(); + if (is_array($tables)) { + $tables = implode('`,`', $tables); + $list = $db->query("REPAIR TABLE `{$tables}`"); + } else { + $list = $db->query("REPAIR TABLE `{$tables}`"); + } + if ($list) { + return $list; + } else { + throw new \Exception("data sheet'{$tables}'Repair mistakes please try again!"); + } + } else { + throw new \Exception("Please specify the table to be repaired!"); + } + } + /** + * 写入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 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->Backup_Init(); + } + } 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; + } + } + /** + * 检查目录是否可写 + * @param string $path 目录 + * @return boolean + */ + protected function checkPath($path) + { + if (is_dir($path)) { + return true; + } + if (mkdir($path, 0755, true)) { + return true; + } else { + return false; + } + } + /** + * 析构方法,用于关闭文件资源 + */ + public function __destruct() + { + $this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp); + } +} \ No newline at end of file