SystemDatabackup.php 7.0 KB

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