MysqlBackupService.php 16 KB

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