人人商城

database.ctrl.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. set_time_limit(0);
  8. load()->func('file');
  9. load()->model('cloud');
  10. load()->func('db');
  11. load()->model('system');
  12. $dos = array('backup', 'restore', 'trim', 'optimize', 'run');
  13. $do = in_array($do, $dos) ? $do : 'backup';
  14. if ($do == 'backup') {
  15. $_W['page']['title'] = '备份 - 数据库 - 常用系统工具 - 系统管理';
  16. if ($_GPC['status']) {
  17. if (empty($_W['setting']['copyright']['status'])) {
  18. itoast('为了保证备份数据完整请关闭站点后再进行此操作', url('system/site'), 'error');
  19. }
  20. $sql = "SHOW TABLE STATUS LIKE '{$_W['config']['db']['tablepre']}%'";
  21. $tables = pdo_fetchall($sql);
  22. if (empty($tables)) {
  23. itoast('数据已经备份完成', url('system/database/'), 'success');
  24. }
  25. $series = max(1, intval($_GPC['series']));
  26. if (!empty($_GPC['volume_suffix'])) {
  27. $volume_suffix = $_GPC['volume_suffix'];
  28. } else {
  29. $volume_suffix = random(10);
  30. }
  31. if (!empty($_GPC['folder_suffix'])) {
  32. $folder_suffix = $_GPC['folder_suffix'];
  33. } else {
  34. $folder_suffix = TIMESTAMP . '_' . random(8);
  35. }
  36. $bakdir = IA_ROOT . '/data/backup/' . $folder_suffix;
  37. if (trim($_GPC['start'])) {
  38. $result = mkdirs($bakdir);
  39. }
  40. $size = 300;
  41. $volumn = 1024 * 1024 * 2;
  42. $dump = '';
  43. if (empty($_GPC['last_table'])) {
  44. $last_table ='';
  45. $catch = true;
  46. } else {
  47. $last_table = $_GPC['last_table'];
  48. $catch = false;
  49. }
  50. foreach ($tables as $table) {
  51. $table = array_shift($table);
  52. if (!empty($last_table) && $table == $last_table) {
  53. $catch = true;
  54. }
  55. if (!$catch) {
  56. continue;
  57. }
  58. if (!empty($dump)) {
  59. $dump .= "\n\n";
  60. }
  61. if ($table != $last_table) {
  62. $row = db_table_schemas($table);
  63. $dump .= $row;
  64. }
  65. $index = 0;
  66. if (!empty($_GPC['index'])) {
  67. $index = $_GPC['index'];
  68. $_GPC['index'] = 0;
  69. }
  70. while (true) {
  71. $start = $index * $size;
  72. $result = db_table_insert_sql($table, $start, $size);
  73. if (!empty($result)) {
  74. $dump .= $result['data'];
  75. if (strlen($dump) > $volumn) {
  76. $bakfile = $bakdir . "/volume-{$volume_suffix}-{$series}.sql";
  77. $dump .= "\n\n";
  78. file_put_contents($bakfile, $dump);
  79. $series++;
  80. $index++;
  81. $current = array(
  82. 'last_table' => $table,
  83. 'index' => $index,
  84. 'series' => $series,
  85. 'volume_suffix'=>$volume_suffix,
  86. 'folder_suffix'=>$folder_suffix,
  87. 'status'=>1
  88. );
  89. $current_series = $series-1;
  90. message('正在导出数据, 请不要关闭浏览器, 当前第 ' . $current_series . ' 卷.', url('system/database/backup/',$current), 'info');
  91. }
  92. }
  93. if (empty($result) || count($result['result']) < $size) {
  94. break;
  95. }
  96. $index++;
  97. }
  98. }
  99. $bakfile = $bakdir . "/volume-{$volume_suffix}-{$series}.sql";
  100. $dump .= "\n\n----WeEngine MySQL Dump End";
  101. file_put_contents($bakfile, $dump);
  102. itoast('数据已经备份完成', url('system/database/'), 'success');
  103. }
  104. }
  105. if($do == 'restore') {
  106. $_W['page']['title'] = '还原 - 数据库 - 常用系统工具 - 系统管理';
  107. $reduction = system_database_backup();
  108. if (!empty($_GPC['restore_dirname'])) {
  109. $restore_dirname = $_GPC['restore_dirname'];
  110. $restore_dirname_list = array_keys($reduction);
  111. if (!in_array($restore_dirname, $restore_dirname_list)) {
  112. itoast('非法访问', '','error');
  113. exit;
  114. }
  115. $volume_list = $reduction[$restore_dirname]['volume_list'];
  116. if (empty($_GPC['restore_volume_name'])) {
  117. $restore_volume_name = $volume_list[0];
  118. } else {
  119. $restore_volume_name = $_GPC['restore_volume_name'];
  120. }
  121. $restore_volume_sizes = max(1, intval($_GPC['restore_volume_sizes']));
  122. if ($reduction[$restore_dirname]['volume'] < $restore_volume_sizes) {
  123. itoast('成功恢复数据备份. 可能还需要你更新缓存.', url('system/database/restore'), 'success');
  124. exit;
  125. }
  126. $volume_sizes = $restore_volume_sizes;
  127. system_database_volume_restore($restore_volume_name);
  128. $next_restore_volume_name = system_database_volume_next($restore_volume_name);
  129. $restore_volume_sizes ++;
  130. $restore = array (
  131. 'restore_volume_name' => $next_restore_volume_name,
  132. 'restore_volume_sizes' => $restore_volume_sizes,
  133. 'restore_dirname' => $restore_dirname
  134. );
  135. message('正在恢复数据备份, 请不要关闭浏览器, 当前第 ' . $volume_sizes . ' 卷.', url('system/database/restore',$restore), 'success');
  136. }
  137. if ($_GPC['delete_dirname']) {
  138. $delete_dirname = $_GPC['delete_dirname'];
  139. if(!empty($reduction[$delete_dirname]) && system_database_backup_delete($delete_dirname)) {
  140. itoast('删除备份成功.', url('system/database/restore'), 'success');
  141. }
  142. }
  143. }
  144. if ($do == 'trim') {
  145. if ($_W['ispost']) {
  146. $type = $_GPC['type'];
  147. $data = $_GPC['data'];
  148. $table = $_GPC['table'];
  149. if ($type == 'field') {
  150. $sql = "ALTER TABLE `$table` DROP `$data`";
  151. if (false !== pdo_query($sql, $params)) {
  152. exit('success');
  153. }
  154. } elseif ($type == 'index') {
  155. $sql = "ALTER TABLE `$table` DROP INDEX `$data`";
  156. if (false !== pdo_query($sql, $params)) {
  157. exit('success');
  158. }
  159. }
  160. exit();
  161. }
  162. $r = cloud_prepare();
  163. if(is_error($r)) {
  164. itoast($r['message'], url('cloud/profile'), 'error');
  165. }
  166. $upgrade = cloud_schema();
  167. $schemas = $upgrade['schemas'];
  168. if (!empty($schemas)) {
  169. foreach ($schemas as $key=>$value) {
  170. $tablename = substr($value['tablename'], 4);
  171. $struct = db_table_schema(pdo(), $tablename);
  172. if (!empty($struct)) {
  173. $temp = db_schema_compare($schemas[$key],$struct);
  174. if (!empty($temp['fields']['less'])) {
  175. $diff[$tablename]['name'] = $value['tablename'];
  176. foreach ($temp['fields']['less'] as $key=>$value) {
  177. $diff[$tablename]['fields'][] = $value;
  178. }
  179. }
  180. if (!empty($temp['indexes']['less'])) {
  181. $diff[$tablename]['name'] = $value['tablename'];
  182. foreach ($temp['indexes']['less'] as $key=>$value) {
  183. $diff[$tablename]['indexes'][] = $value;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. if ($do == 'optimize') {
  191. $_W['page']['title'] = '优化 - 数据库 - 常用系统工具 - 系统管理';
  192. $optimize_table = array();
  193. $sql = "SHOW TABLE STATUS LIKE '{$_W['config']['db']['tablepre']}%'";
  194. $tables = pdo_fetchall($sql);
  195. foreach ($tables as $tableinfo) {
  196. if ($tableinfo['Engine'] == 'InnoDB') {
  197. continue;
  198. }
  199. if (!empty($tableinfo) && !empty($tableinfo['Data_free'])) {
  200. $row = array(
  201. 'title' => $tableinfo['Name'],
  202. 'type' => $tableinfo['Engine'],
  203. 'rows' => $tableinfo['Rows'],
  204. 'data' => sizecount($tableinfo['Data_length']),
  205. 'index' => sizecount($tableinfo['Index_length']),
  206. 'free' => sizecount($tableinfo['Data_free'])
  207. );
  208. $optimize_table[$row['title']] = $row;
  209. }
  210. }
  211. if (checksubmit()) {
  212. foreach ($_GPC['select'] as $tablename) {
  213. if (!empty($optimize_table[$tablename])) {
  214. $sql = "OPTIMIZE TABLE {$tablename}";
  215. pdo_fetch($sql);
  216. }
  217. }
  218. itoast('数据表优化成功.', 'refresh', 'success');
  219. }
  220. }
  221. if ($do == 'run') {
  222. $_W['page']['title'] = '运行SQL - 数据库 - 常用系统工具 - 系统管理';
  223. if (!DEVELOPMENT) {
  224. // itoast('请先开启开发模式后再使用此功能', referer(), 'info');
  225. }
  226. if (checksubmit()) {
  227. $sql = $_POST['sql'];
  228. pdo_run($sql);
  229. itoast('查询执行成功.', 'refresh', 'success');
  230. }
  231. }
  232. template('system/database');