MysqlBackupService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use crmeb\exceptions\AdminException;
  13. use think\facade\Db;
  14. class MysqlBackupService
  15. {
  16. /**
  17. * 文件指针
  18. * @var resource
  19. */
  20. private $fp;
  21. /**
  22. * 备份文件信息 part - 卷号,name - 文件名
  23. * @var array
  24. */
  25. private $file;
  26. /**
  27. * 当前打开文件大小
  28. * @var integer
  29. */
  30. private $size = 0;
  31. /**
  32. * 数据库配置
  33. * @var integer
  34. */
  35. private $dbconfig = array();
  36. /**
  37. * 备份配置
  38. * @var integer
  39. */
  40. private $config = array(
  41. 'path' => './Data/',
  42. //数据库备份路径
  43. 'part' => 20971520,
  44. //数据库备份卷大小
  45. 'compress' => 0,
  46. //数据库备份文件是否启用压缩 0不压缩 1 压缩
  47. 'level' => 9,
  48. );
  49. /**
  50. * 数据库备份构造方法
  51. *
  52. * @param array $file 备份或还原的文件信息
  53. * @param array $config 备份配置信息
  54. */
  55. public function __construct($config = [])
  56. {
  57. $this->config['path'] = app()->getRootPath() . 'backup/';
  58. $this->config = array_merge($this->config, $config);
  59. //初始化文件名
  60. $this->setFile();
  61. //初始化数据库连接参数
  62. $this->setDbConn();
  63. //检查文件是否可写
  64. if (!$this->checkPath($this->config['path'])) {
  65. throw new AdminException(400734);
  66. }
  67. }
  68. /**
  69. * 设置脚本运行超时时间
  70. * 0表示不限制,支持连贯操作
  71. */
  72. public function setTimeout($time = null)
  73. {
  74. if (!is_null($time)) {
  75. set_time_limit($time) || ini_set("max_execution_time", $time);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * 设置数据库连接必备参数
  81. *
  82. * @param array $dbconfig 数据库连接配置信息
  83. * @return $this
  84. */
  85. public function setDbConn($dbconfig = [])
  86. {
  87. if (empty($dbconfig)) {
  88. $this->dbconfig = config('database.connections.' . config('database.default'));
  89. //$this->dbconfig = Config::get('database');
  90. } else {
  91. $this->dbconfig = $dbconfig;
  92. }
  93. return $this;
  94. }
  95. /**
  96. * 设置备份文件名
  97. *
  98. * @param null $file
  99. * @return $this
  100. */
  101. public function setFile($file = null)
  102. {
  103. if (is_null($file)) {
  104. $this->file = ['name' => date('Ymd-His'), 'part' => 1];
  105. } else {
  106. if (!array_key_exists("name", $file) && !array_key_exists("part", $file)) {
  107. $this->file = $file['1'];
  108. } else {
  109. $this->file = $file;
  110. }
  111. }
  112. return $this;
  113. }
  114. //数据类连接
  115. public static function connect()
  116. {
  117. return Db::connect();
  118. }
  119. /**
  120. * 数据库表列表
  121. *
  122. * @param null $table
  123. * @param int $type
  124. * @return array
  125. * @throws \think\db\exception\BindParamException
  126. * @throws \think\exception\PDOException
  127. */
  128. public function dataList(?string $table = null, int $type = 1)
  129. {
  130. $db = self::connect();
  131. if (is_null($table)) {
  132. $list = $db->query("SHOW TABLE STATUS");
  133. } else {
  134. if ($type) {
  135. $list = $db->query("SHOW FULL COLUMNS FROM {$table}");
  136. } else {
  137. $list = $db->query("show columns from {$table}");
  138. }
  139. }
  140. return array_map('array_change_key_case', $list);
  141. //$list;
  142. }
  143. /**
  144. * 数据库备份文件列表
  145. *
  146. * @return array
  147. */
  148. public function fileList()
  149. {
  150. if (!is_dir($this->config['path'])) {
  151. mkdir($this->config['path'], 0755, true);
  152. }
  153. $path = realpath($this->config['path']);
  154. $flag = \FilesystemIterator::KEY_AS_FILENAME;
  155. $glob = new \FilesystemIterator($path, $flag);
  156. $list = array();
  157. foreach ($glob as $name => $file) {
  158. if (preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql(?:\\.gz)?$/', $name)) {
  159. $info['filename'] = $name;
  160. $name = sscanf($name, '%4s%2s%2s-%2s%2s%2s-%d');
  161. $date = "{$name[0]}-{$name[1]}-{$name[2]}";
  162. $time = "{$name[3]}:{$name[4]}:{$name[5]}";
  163. $part = $name[6];
  164. if (isset($list["{$date} {$time}"])) {
  165. $info = $list["{$date} {$time}"];
  166. $info['part'] = max($info['part'], $part);
  167. $info['size'] = $info['size'] + $file->getSize();
  168. } else {
  169. $info['part'] = $part;
  170. $info['size'] = $file->getSize();
  171. }
  172. $extension = strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
  173. $info['compress'] = $extension === 'SQL' ? '-' : $extension;
  174. $info['time'] = strtotime("{$date} {$time}");
  175. $list["{$date} {$time}"] = $info;
  176. }
  177. }
  178. return $list;
  179. }
  180. /**
  181. * @param string $type
  182. * @param int $time
  183. * @return array|false|string
  184. * @throws \Exception
  185. */
  186. public function getFile($type = '', $time = 0)
  187. {
  188. //
  189. if (!is_numeric($time)) {
  190. throw new AdminException(400735);
  191. }
  192. switch ($type) {
  193. case 'time':
  194. $name = date('Ymd-His', $time) . '-*.sql*';
  195. $path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name;
  196. return glob($path);
  197. case 'timeverif':
  198. $name = date('Ymd-His', $time) . '-*.sql*';
  199. $path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name;
  200. $files = glob($path);
  201. $list = array();
  202. foreach ($files as $name) {
  203. $basename = basename($name);
  204. $match = sscanf($basename, '%4s%2s%2s-%2s%2s%2s-%d');
  205. $gz = preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql.gz$/', $basename);
  206. $list[$match[6]] = array($match[6], $name, $gz);
  207. }
  208. $last = end($list);
  209. if (count($list) === $last[0]) {
  210. return $list;
  211. } else {
  212. throw new AdminException(400736);
  213. }
  214. case 'pathname':
  215. return "{$this->config['path']}{$this->file['name']}-{$this->file['part']}.sql";
  216. case 'filename':
  217. return "{$this->file['name']}-{$this->file['part']}.sql";
  218. case 'filepath':
  219. return $this->config['path'];
  220. default:
  221. $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);
  222. return $arr;
  223. }
  224. }
  225. /**
  226. * 删除备份文件
  227. * @param $time
  228. * @return mixed
  229. * @throws \Exception
  230. */
  231. public function delFile($time)
  232. {
  233. if ($time) {
  234. $file = $this->getFile('time', $time);
  235. array_map("unlink", $this->getFile('time', $time));
  236. if (count($this->getFile('time', $time))) {
  237. throw new AdminException(100008);
  238. } else {
  239. return $time;
  240. }
  241. } else {
  242. throw new AdminException(400735);
  243. }
  244. }
  245. /**
  246. * 下载备份
  247. *
  248. * @param $time
  249. * @param int $part
  250. * @throws \Exception
  251. */
  252. public function downloadFile($time, int $part = 0, bool $isFile = false)
  253. {
  254. $file = $this->getFile('time', $time);
  255. $fileName = $file[$part];
  256. if (file_exists($fileName)) {
  257. if ($isFile) {
  258. $key = password_hash(time() . $fileName, PASSWORD_DEFAULT);
  259. CacheService::set($key, ['path' => $fileName, 'fileName' => substr(strstr($fileName, 'backup'), 7)], 300);
  260. return $key;
  261. }
  262. ob_end_clean();
  263. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  264. header('Content-Description: File Transfer');
  265. header('Access-Control-Allow-Origin: ' . request()->domain());
  266. header('Content-Type: application/octet-stream');
  267. header('Content-Length: ' . filesize($fileName));
  268. header('Content-Disposition: attachment; filename=' . basename($fileName));
  269. return readfile($fileName);
  270. } else {
  271. throw new AdminException(400736);
  272. }
  273. }
  274. public function import($start)
  275. {
  276. //还原数据
  277. $db = self::connect();
  278. if ($this->config['compress']) {
  279. $gz = gzopen($this->file[1], 'r');
  280. $size = 0;
  281. } else {
  282. $size = filesize($this->file[1]);
  283. $gz = fopen($this->file[1], 'r');
  284. }
  285. $sql = '';
  286. if ($start) {
  287. $this->config['compress'] ? gzseek($gz, $start) : fseek($gz, $start);
  288. }
  289. for ($i = 0; $i < 1000; $i++) {
  290. $sql .= $this->config['compress'] ? gzgets($gz) : fgets($gz);
  291. if (preg_match('/.*;$/', trim($sql))) {
  292. if (false !== $db->execute($sql)) {
  293. $start += strlen($sql);
  294. } else {
  295. return false;
  296. }
  297. $sql = '';
  298. } elseif ($this->config['compress'] ? gzeof($gz) : feof($gz)) {
  299. return 0;
  300. }
  301. }
  302. return array($start, $size);
  303. }
  304. /**
  305. * 写入初始数据
  306. *
  307. * @return boolean true - 写入成功,false - 写入失败
  308. */
  309. public function Backup_Init()
  310. {
  311. $sql = "-- -----------------------------\n";
  312. $sql .= "-- Think MySQL Data Transfer \n";
  313. $sql .= "-- \n";
  314. $sql .= "-- Host : " . $this->dbconfig['hostname'] . "\n";
  315. $sql .= "-- Port : " . $this->dbconfig['hostport'] . "\n";
  316. $sql .= "-- Database : " . $this->dbconfig['database'] . "\n";
  317. $sql .= "-- \n";
  318. $sql .= "-- Part : #{$this->file['part']}\n";
  319. $sql .= "-- Date : " . date("Y-m-d H:i:s") . "\n";
  320. $sql .= "-- -----------------------------\n\n";
  321. $sql .= "SET FOREIGN_KEY_CHECKS = 0;\n\n";
  322. return $this->write($sql);
  323. }
  324. /**
  325. * 备份表结构
  326. *
  327. * @param string $table
  328. * @param int $start
  329. * @return bool|int
  330. * @throws \think\db\exception\BindParamException
  331. * @throws \think\exception\PDOException
  332. */
  333. public function backup(string $table, int $start, $sql = '')
  334. {
  335. $db = self::connect();
  336. // 备份表结构
  337. if (0 == $start) {
  338. $result = $db->query("SHOW CREATE TABLE `{$table}`");
  339. $sql .= "\n";
  340. $sql .= "-- -----------------------------\n";
  341. $sql .= "-- Table structure for `{$table}`\n";
  342. $sql .= "-- -----------------------------\n";
  343. $sql .= "DROP TABLE IF EXISTS `{$table}`;\n";
  344. $sql .= trim($result[0]['Create Table']) . ";\n\n";
  345. }
  346. //数据总数
  347. $result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`");
  348. $count = $result['0']['count'];
  349. //备份表数据
  350. if ($count) {
  351. //写入数据注释
  352. if (0 == $start) {
  353. $sql .= "-- -----------------------------\n";
  354. $sql .= "-- Records of `{$table}`\n";
  355. $sql .= "-- -----------------------------\n";
  356. }
  357. //备份数据记录
  358. $result = $db->query("SELECT * FROM `{$table}` LIMIT :MIN, 1000", ['MIN' => intval($start)]);
  359. foreach ($result as $row) {
  360. $row = array_map('addslashes', $row);
  361. $sql .= "INSERT INTO `{$table}` VALUES ('" . str_replace(array("\r", "\n"), array('\\r', '\\n'), implode("', '", $row)) . "');\n";
  362. }
  363. if (false === $this->write($sql)) {
  364. return false;
  365. }
  366. //还有更多数据
  367. if ($count > $start + 1000) {
  368. //return array($start + 1000, $count);
  369. return $this->backup($table, $start + 1000);
  370. }
  371. }
  372. //备份下一表
  373. return 0;
  374. }
  375. /**
  376. * 优化表
  377. *
  378. * @param array|string $tables
  379. * @throws \think\db\exception\BindParamException
  380. * @throws \think\exception\PDOException
  381. */
  382. public function optimize($tables)
  383. {
  384. if ($tables) {
  385. $db = self::connect();
  386. if (is_array($tables)) {
  387. $tables = implode('`,`', $tables);
  388. $list = $db->query("OPTIMIZE TABLE `{$tables}`");
  389. } else {
  390. $list = $db->query("OPTIMIZE TABLE {$tables}");
  391. }
  392. if (!$list) {
  393. throw new AdminException(400737);
  394. }
  395. return $list;
  396. } else {
  397. throw new AdminException(400738);
  398. }
  399. }
  400. /**
  401. * 修复表
  402. *
  403. * @param string|null $tables
  404. * @return array
  405. * @throws \think\db\exception\BindParamException
  406. * @throws \think\exception\PDOException
  407. */
  408. public function repair(?string $tables = null)
  409. {
  410. if ($tables) {
  411. $db = self::connect();
  412. if (is_array($tables)) {
  413. $tables = implode('`,`', $tables);
  414. $list = $db->query("REPAIR TABLE `{$tables}`");
  415. } else {
  416. $list = $db->query("REPAIR TABLE {$tables}");
  417. }
  418. if ($list) {
  419. return $list;
  420. } else {
  421. throw new AdminException(400737);
  422. }
  423. } else {
  424. throw new AdminException(400738);
  425. }
  426. }
  427. /**
  428. * 写入SQL语句
  429. *
  430. * @param string $sql 要写入的SQL语句
  431. * @return boolean true - 写入成功,false - 写入失败!
  432. */
  433. private function write(string $sql)
  434. {
  435. $size = strlen($sql);
  436. //由于压缩原因,无法计算出压缩后的长度,这里假设压缩率为50%,
  437. //一般情况压缩率都会高于50%;
  438. $size = $this->config['compress'] ? $size / 2 : $size;
  439. $this->open($size);
  440. return $this->config['compress'] ? @gzwrite($this->fp, $sql) : @fwrite($this->fp, $sql);
  441. }
  442. /**
  443. * 打开一个卷,用于写入数据
  444. *
  445. * @param integer $size 写入数据的大小
  446. */
  447. private function open(int $size)
  448. {
  449. if ($this->fp) {
  450. $this->size += $size;
  451. if ($this->size > $this->config['part']) {
  452. $this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp);
  453. $this->fp = null;
  454. $this->file['part']++;
  455. session('backup_file', $this->file);
  456. $this->Backup_Init();
  457. }
  458. } else {
  459. $backuppath = $this->config['path'];
  460. $filename = "{$backuppath}{$this->file['name']}-{$this->file['part']}.sql";
  461. if ($this->config['compress']) {
  462. $filename = "{$filename}.gz";
  463. $this->fp = @gzopen($filename, "a{$this->config['level']}");
  464. } else {
  465. $this->fp = @fopen($filename, 'a');
  466. }
  467. $this->size = filesize($filename) + $size;
  468. }
  469. }
  470. /**
  471. * 检查目录是否可写
  472. *
  473. * @param string $path
  474. * @return bool
  475. */
  476. protected function checkPath(string $path)
  477. {
  478. if (is_dir($path)) {
  479. return true;
  480. }
  481. if (mkdir($path, 0755, true)) {
  482. return true;
  483. } else {
  484. return false;
  485. }
  486. }
  487. /**
  488. * 析构方法,用于关闭文件资源
  489. */
  490. public function __destruct()
  491. {
  492. $this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp);
  493. }
  494. }