SystemDatabackup.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\adminapi\controller\v1\system;
  3. use app\adminapi\controller\AuthController;
  4. use app\models\system\SystemConfig;
  5. use \crmeb\services\MysqlBackupService as Backup;
  6. use crmeb\services\UtilService;
  7. use think\facade\Env;
  8. use think\facade\Session;
  9. use think\facade\Db;
  10. /**
  11. * 数据备份
  12. * Class SystemDatabackup
  13. * @package app\admin\controller\system
  14. *
  15. */
  16. class SystemDatabackup extends AuthController
  17. {
  18. /**
  19. * @var Backup
  20. */
  21. protected $DB;
  22. public function initialize()
  23. {
  24. parent::initialize();
  25. $config = array(
  26. //数据库备份卷大小
  27. 'compress' => 1,
  28. //数据库备份文件是否启用压缩 0不压缩 1 压缩
  29. 'level' => 5,
  30. );
  31. $this->DB = new Backup($config);
  32. }
  33. /**
  34. * 获取数据库表
  35. */
  36. public function index()
  37. {
  38. $db = $this->DB;
  39. $list = $db->dataList();
  40. $count = count($list);
  41. return $this->success(compact('count', 'list'));
  42. }
  43. /**
  44. * 查看表结构 详情
  45. */
  46. public function read()
  47. {
  48. $database = Env::get("database.database");
  49. $tablename = request()->param('tablename', '', 'htmlspecialchars');
  50. $res = Db::query("select * from information_schema.columns where table_name = '" . $tablename . "' and table_schema = '" . $database . "'");
  51. $count = count($res);
  52. foreach ($res AS $key => $f) {
  53. $res[$key]['EXTRA'] = ($f['EXTRA'] == 'auto_increment' ? '是' : ' ');
  54. }
  55. return $this->success(['count' => $count, 'list' => $res]);
  56. }
  57. /**
  58. * 优化表
  59. */
  60. public function optimize()
  61. {
  62. $tables = $this->request->param('tables');
  63. $db = $this->DB;
  64. $res = $db->optimize($tables);
  65. return $this->success($res ? '优化成功' : '优化失败');
  66. }
  67. /**
  68. * 修复表
  69. */
  70. public function repair()
  71. {
  72. $tables = $this->request->param('tables');
  73. $db = $this->DB;
  74. $res = $db->repair($tables);
  75. return $this->success($res ? '修复成功' : '修复失败');
  76. }
  77. /**
  78. * 备份表
  79. */
  80. public function backup()
  81. {
  82. $tables = $this->request->param('tables');
  83. $tables = explode(',', $tables);
  84. $db = $this->DB;
  85. $data = '';
  86. foreach ($tables as $t) {
  87. $res = $db->backup($t, 0);
  88. if ($res == false && $res != 0) {
  89. $data .= $t . '|';
  90. }
  91. }
  92. return $this->success($data ? '备份失败' . $data : '备份成功');
  93. }
  94. /**
  95. * 获取备份记录表
  96. */
  97. public function fileList()
  98. {
  99. $db = $this->DB;
  100. $files = $db->fileList();
  101. $data = [];
  102. foreach ($files as $key => $t) {
  103. $data[$key]['filename'] = $t['filename'];
  104. $data[$key]['part'] = $t['part'];
  105. $data[$key]['size'] = $t['size'] . 'B';
  106. $data[$key]['compress'] = $t['compress'];
  107. $data[$key]['backtime'] = $key;
  108. $data[$key]['time'] = $t['time'];
  109. }
  110. krsort($data);//根据时间降序
  111. return $this->success(['count' => count($data), 'list' => array_values($data)]);
  112. }
  113. /**
  114. * 删除备份记录表
  115. */
  116. public function delFile()
  117. {
  118. $filename = intval(request()->post('filename'));
  119. $files = $this->DB->delFile($filename);
  120. return $this->success('删除成功');
  121. }
  122. /**
  123. * 导入备份记录表
  124. */
  125. public function import()
  126. {
  127. [$part, $start, $time] = UtilService::postMore([
  128. [['part', 'd'], 0],
  129. [['start', 'd'], 0],
  130. [['time', 'd'], 0],
  131. ], null, true);
  132. $db = $this->DB;
  133. if (is_numeric($time) && !$start) {
  134. $list = $db->getFile('timeverif', $time);
  135. if (is_array($list)) {
  136. session::set('backup_list', $list);
  137. return $this->success('初始化完成!', array('part' => 1, 'start' => 0));
  138. } else {
  139. return $this->fail('备份文件可能已经损坏,请检查!');
  140. }
  141. } else if (is_numeric($part) && is_numeric($start) && $part && $start) {
  142. $list = session::get('backup_list');
  143. $start = $db->setFile($list)->import($start);
  144. if (false === $start) {
  145. return $this->fail('还原数据出错!');
  146. } elseif (0 === $start) {
  147. if (isset($list[++$part])) {
  148. $data = array('part' => $part, 'start' => 0);
  149. return $this->success("正在还原...#{$part}", $data);
  150. } else {
  151. session::delete('backup_list');
  152. return $this->success('还原完成!');
  153. }
  154. } else {
  155. $data = array('part' => $part, 'start' => $start[0]);
  156. if ($start[1]) {
  157. $rate = floor(100 * ($start[0] / $start[1]));
  158. return $this->success("正在还原...#{$part}({$rate}%)", $data);
  159. } else {
  160. $data['gz'] = 1;
  161. return $this->success("正在还原...#{$part}", $data);
  162. }
  163. return $this->success("正在还原...#{$part}");
  164. }
  165. } else {
  166. return $this->fail('参数错误!');
  167. }
  168. }
  169. /**
  170. * 下载备份记录表
  171. */
  172. public function downloadFile()
  173. {
  174. $time = intval(request()->param('time'));
  175. $this->DB->downloadFile($time);
  176. }
  177. }