SystemDatabackup.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\system;
  12. use app\admin\controller\AuthController;
  13. use service\FormBuilder as Form;
  14. use think\Request;
  15. use service\JsonService as Json;
  16. use \tp5er\Backup;
  17. use think\Session;
  18. use think\Db;
  19. /**
  20. * 文件校验控制器
  21. * Class SystemDatabackup
  22. * @package app\admin\controller\system
  23. *
  24. */
  25. class SystemDatabackup extends AuthController
  26. {
  27. protected $DB;
  28. public function _initialize()
  29. {
  30. $config = array(
  31. 'path' => '.' . PUBILC_PATH . 'backup/data/',
  32. //数据库备份路径
  33. 'part' => 20971520,
  34. //数据库备份卷大小
  35. 'compress' => 1,
  36. //数据库备份文件是否启用压缩 0不压缩 1 压缩
  37. 'level' => 5,
  38. );
  39. $this->DB = new Backup($config);
  40. }
  41. /**
  42. * 数据类表列表
  43. */
  44. public function index()
  45. {
  46. return $this->fetch();
  47. }
  48. /**
  49. * 获取数据库表
  50. * @param Request|null $request
  51. */
  52. public function tablelist(Request $request = null)
  53. {
  54. $db = $this->DB;
  55. return Json::result(0, 'sucess', $db->dataList(), count($db->dataList()));
  56. }
  57. /**
  58. * 查看表结构
  59. * @param Request|null $request
  60. */
  61. public function seetable(Request $request = null)
  62. {
  63. $database = config("database.database");
  64. $tablename = $request->param('tablename');
  65. $res = Db::query("select * from information_schema.columns where table_name = '" . $tablename . "' and table_schema = '" . $database . "'");
  66. $html = '';
  67. $html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
  68. $html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th><th>允许非空</th><th>自动递增</th><th>备注</th></tr>';
  69. $html .= '';
  70. foreach ($res AS $f) {
  71. $html .= '<td class="c1">' . $f['COLUMN_NAME'] . '</td>';
  72. $html .= '<td class="c2">' . $f['COLUMN_TYPE'] . '</td>';
  73. $html .= '<td class="c3">' . $f['COLUMN_DEFAULT'] . '</td>';
  74. $html .= '<td class="c4">' . $f['IS_NULLABLE'] . '</td>';
  75. $html .= '<td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? '是' : ' ') . '</td>';
  76. $html .= '<td class="c6">' . $f['COLUMN_COMMENT'] . '</td>';
  77. $html .= '</tr>';
  78. }
  79. $html .= '</tbody></table></p>';
  80. $html .= '<p style="text-align:left;margin:20px auto;">总共:' . count($res) . '个字段</p>';
  81. $html .= '</body></html>';
  82. echo '<style>
  83. body,td,th {font-family:"宋体"; font-size:12px;}
  84. table,h1,p{width:960px;margin:0px auto;}
  85. table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
  86. table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
  87. table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:12px; border:1px solid #CCC;padding-left:5px;}
  88. table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;padding-left:5px;}
  89. .c1{ width: 150px;}
  90. .c2{ width: 150px;}
  91. .c3{ width: 80px;}
  92. .c4{ width: 100px;}
  93. .c5{ width: 100px;}
  94. .c6{ width: 300px;}
  95. </style>';
  96. echo $html;
  97. }
  98. /**
  99. * 优化表
  100. * @param Request|null $request
  101. */
  102. public function optimize(Request $request = null)
  103. {
  104. $tables = $request->post('tables/a');
  105. $db = $this->DB;
  106. $res = $db->optimize($tables);
  107. return Json::successful($res ? '优化成功' : '优化失败');
  108. }
  109. /**修复表
  110. * @param Request|null $request
  111. */
  112. public function repair(Request $request = null)
  113. {
  114. $tables = $request->post('tables/a');
  115. $db = $this->DB;
  116. $res = $db->repair($tables);
  117. return Json::successful($res ? '修复成功' : '修复失败');
  118. }
  119. /**备份表
  120. * @param Request|null $request
  121. */
  122. public function backup(Request $request = null)
  123. {
  124. $tables = $request->post('tables/a');
  125. $db = $this->DB;
  126. $data = '';
  127. foreach ($tables as $t) {
  128. $res = $db->backup($t, 0);
  129. if ($res == false && $res != 0) {
  130. $data .= $t . '|';
  131. }
  132. }
  133. return Json::successful($data ? '备份失败' . $data : '备份成功');
  134. }
  135. /**获取备份记录表
  136. */
  137. public function fileList()
  138. {
  139. $db = $this->DB;
  140. $files = $db->fileList();
  141. $data = [];
  142. foreach ($files as $key => $t) {
  143. $data[$key]['filename'] = $t['filename'];
  144. $data[$key]['part'] = $t['part'];
  145. $data[$key]['size'] = $t['size'] . 'B';
  146. $data[$key]['compress'] = $t['compress'];
  147. $data[$key]['backtime'] = $key;
  148. $data[$key]['time'] = $t['time'];
  149. }
  150. krsort($data);//根据时间降序
  151. return Json::result(0, 'sucess', $data, count($data));
  152. }
  153. /**删除备份记录表
  154. * @param Request|null $request
  155. */
  156. public function delFile(Request $request = null)
  157. {
  158. $feilname = intval($request->post('feilname'));
  159. $files = $this->DB->delFile($feilname);
  160. return Json::result(0, 'sucess');
  161. }
  162. /**倒入备份记录表
  163. * @param Request|null $request
  164. */
  165. public function import(Request $request = null)
  166. {
  167. $part = $request->post('part') != '' ? intval($request->post('part')) : null;
  168. $start = $request->post('start') != '' ? intval($request->post('start')) : null;
  169. $time = intval($request->post('time'));
  170. $db = $this->DB;
  171. if (is_numeric($time) && is_null($part) && is_null($start)) {
  172. $list = $db->getFile('timeverif', $time);
  173. if (is_array($list)) {
  174. session::set('backup_list', $list);
  175. $this->success('初始化完成!', '', array('part' => 1, 'start' => 0));
  176. } else {
  177. $this->error('备份文件可能已经损坏,请检查!');
  178. }
  179. } else if (is_numeric($part) && is_numeric($start)) {
  180. $list = session::get('backup_list');
  181. $start = $db->setFile($list)->import($start);
  182. if (false === $start) {
  183. $this->error('还原数据出错!');
  184. } elseif (0 === $start) {
  185. if (isset($list[++$part])) {
  186. $data = array('part' => $part, 'start' => 0);
  187. $this->success("正在还原...#{$part}", '', $data);
  188. } else {
  189. session::delete('backup_list');
  190. $this->success('还原完成!');
  191. }
  192. } else {
  193. $data = array('part' => $part, 'start' => $start[0]);
  194. if ($start[1]) {
  195. $rate = floor(100 * ($start[0] / $start[1]));
  196. $this->success("正在还原...#{$part}({$rate}%)", '', $data);
  197. } else {
  198. $data['gz'] = 1;
  199. $this->success("正在还原...#{$part}", '', $data);
  200. }
  201. $this->success("正在还原...#{$part}", '');
  202. }
  203. } else {
  204. $this->error('参数错误!');
  205. }
  206. // return Json::result(0,'sucess',$data,count($data));
  207. }
  208. /**下载备份记录表
  209. * @param Request|null $request
  210. */
  211. public function downloadFile(Request $request = null)
  212. {
  213. $time = intval($request->param('feilname'));
  214. $this->DB->downloadFile($time);
  215. }
  216. }