Database.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\controller\Base;
  4. use my\Backup;
  5. use think\Request;
  6. use think\Db;
  7. class Database extends Base{
  8. /**
  9. *获取数据库表信息
  10. *
  11. * @return \think\response\View
  12. */
  13. public function index(){
  14. $backup = new Backup();
  15. $list = $backup->dataList();
  16. $this -> view -> assign(['list'=>$list]);
  17. return $this -> view -> fetch('index');
  18. }
  19. /**
  20. *表的优化
  21. *
  22. * @return \
  23. */
  24. public function optimize(Request $request){
  25. if($request->isPost()){
  26. $table = input('post.tablename');
  27. if(!empty($table)){
  28. if(substr_count($table,',')>=1){
  29. $table = explode(",",$table);
  30. }
  31. $backup = new Backup();
  32. $res = $backup->optimize($table);
  33. if($res[0]['Msg_text'] === "Table does not support optimize, doing recreate + analyze instead"){
  34. if($res[1]['Msg_text']==="OK"){
  35. return json(['code'=>1,'msg'=>'优化成功!']);
  36. }
  37. }elseif($res[0]['Msg_text']==="OK"){
  38. return json(['code'=>1,'msg'=>'优化成功!']);
  39. }
  40. return json(['code'=>0,'msg'=>'优化失败!']);
  41. }
  42. }else{
  43. return json(['code'=>0,'msg'=>'非法请求!']);
  44. }
  45. }
  46. /**
  47. *表的修复
  48. *
  49. * @return \
  50. */
  51. public function repair(Request $request){
  52. if($request->isPost()){
  53. $table = input('post.tablename');
  54. if(!empty($table)){
  55. if(substr_count($table,',')>=1){
  56. $table = explode(",",$table);
  57. }
  58. $backup = new Backup();
  59. $res = $backup->repair($table);
  60. if($res[0]['Msg_text']==="OK"){
  61. return json(['code'=>1,'msg'=>'修复成功!']);
  62. }else{
  63. return json(['code'=>0,'msg'=>'修复失败!']);
  64. }
  65. }
  66. }else{
  67. return json(['code'=>0,'msg'=>'非法请求!']);
  68. }
  69. }
  70. /**
  71. *表的分析
  72. *
  73. * @return \
  74. */
  75. public function analyze(Request $request){
  76. if($request->isPost()){
  77. $table = input('post.tablename');
  78. if(!empty($table)){
  79. if(substr_count($table,',')>=1){
  80. $table = explode(",",$table);
  81. }
  82. $backup = new Backup();
  83. $res = $backup->analyze($table);
  84. if($res[0]['Msg_text']==="OK"){
  85. return json(['code'=>1,'msg'=>'分析成功!']);
  86. }else{
  87. return json(['code'=>0,'msg'=>'分析失败!']);
  88. }
  89. }
  90. }else{
  91. return json(['code'=>0,'msg'=>'非法请求!']);
  92. }
  93. }
  94. /**
  95. *表的备份-获取备份列表
  96. *
  97. * @return \
  98. */
  99. public function backuplst(){
  100. //获取表名
  101. $table = input('tb');
  102. //实例化
  103. $config = [
  104. 'path' => './public/data/'.$table.'/',
  105. ];
  106. $backup = new Backup($config);
  107. $list = $backup->fileList();
  108. $this -> view -> assign(['list'=>$list,'table'=>$table]);
  109. return $this -> view -> fetch('backuplst');
  110. }
  111. /**
  112. *表的备份-备份操作
  113. *
  114. * @return \
  115. */
  116. public function dbbackup(Request $request){
  117. if($request->isPost()){
  118. $table = input('tb');
  119. $config = [
  120. 'path' => './public/data/'.$table.'/',
  121. 'compress' => 0,
  122. ];
  123. $backup = new Backup($config);
  124. $file = ['name'=>date('Ymd-His'),'part'=>1];
  125. if($table == 'database'){
  126. $database = \think\facade\Config::get('database.database');
  127. $sql = "show tables";
  128. $tables = Db::query($sql);
  129. foreach($tables as $v){
  130. $start = $backup->setFile($file)->backup($v['Tables_in_'.$database],0);
  131. }
  132. }else{
  133. $start = $backup->setFile($file)->backup($table,0);
  134. }
  135. if($start == 0){
  136. return json(['code'=>1,'msg'=>'备份成功!']);
  137. }else{
  138. return json(['code'=>0,'msg'=>'备份失败!']);
  139. }
  140. }
  141. return json(['code'=>500,'msg'=>'非法请求!']);
  142. }
  143. /**
  144. *表的还原-单表还原
  145. *
  146. * @return \
  147. */
  148. public function restore(Request $request){
  149. if($request->isPost()){
  150. $table = input('post.tb');
  151. $name = input('post.name');
  152. $config = [
  153. 'path' => './public/data/'.$table.'/',
  154. 'compress' => 1,
  155. ];
  156. $backup = new Backup($config);
  157. $start = 0;
  158. $start = $backup->setFile(['name' => $name])->import($start);
  159. if($start == 0){
  160. return json(['code'=>1,'msg'=>'还原成功!']);
  161. }else{
  162. return json(['code'=>0,'msg'=>'还原失败!']);
  163. }
  164. }
  165. return json(['code'=>500,'msg'=>'非法请求!']);
  166. }
  167. /**
  168. *表的备份-单表删除
  169. *
  170. * @return \
  171. */
  172. public function sqldel(Request $request){
  173. if($request->isPost()){
  174. $table = input('post.tb');
  175. $name = input('post.name');
  176. $config = [
  177. 'path' => './public/data/'.$table.'/',
  178. ];
  179. $backup = new Backup($config);
  180. $res = $backup->delFile($name);
  181. return $res;
  182. }
  183. return json(['code'=>500,'msg'=>'非法请求!']);
  184. }
  185. }