SystemDatabackup.php 7.2 KB

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