api.backups.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Returns database backup creation form
  4. *
  5. * @return string
  6. */
  7. function web_BackupForm() {
  8. $backupinputs = __('This will create a backup copy of all tables in the database') . wf_tag('br');
  9. $backupinputs .= wf_HiddenInput('createbackup', 'true');
  10. $backupinputs .= wf_CheckInput('imready', 'I`m ready', true, false);
  11. $backupinputs .= wf_Submit('Create');
  12. $form = wf_Form('', 'POST', $backupinputs, 'glamour');
  13. return($form);
  14. }
  15. /**
  16. * Renders list of available database backup dumps
  17. *
  18. * @return string
  19. */
  20. function web_AvailableDBBackupsList() {
  21. $backupsPath = DATA_PATH . 'backups/sql/';
  22. $availbacks = rcms_scandir($backupsPath);
  23. $messages = new UbillingMessageHelper();
  24. $result = $messages->getStyledMessage(__('No existing DB backups here'), 'warning');
  25. if (!empty($availbacks)) {
  26. $cells = wf_TableCell(__('Creation date'));
  27. $cells .= wf_TableCell(__('Size'));
  28. $cells .= wf_TableCell(__('Filename'));
  29. $cells .= wf_TableCell(__('Actions'));
  30. $rows = wf_TableRow($cells, 'row1');
  31. foreach ($availbacks as $eachDump) {
  32. if (is_file($backupsPath . $eachDump)) {
  33. $fileDate = filectime($backupsPath . $eachDump);
  34. $fileDate = date("Y-m-d H:i:s", $fileDate);
  35. $fileSize = filesize($backupsPath . $eachDump);
  36. $fileSize = stg_convert_size($fileSize);
  37. $encodedDumpPath = base64_encode($backupsPath . $eachDump);
  38. $downloadLink = wf_Link('?module=backups&download=' . $encodedDumpPath, $eachDump, false, '');
  39. $actLinks = wf_JSAlert('?module=backups&deletedump=' . $encodedDumpPath, web_delete_icon(), __('Removing this may lead to irreparable results')) . ' ';
  40. $actLinks .= wf_Link('?module=backups&download=' . $encodedDumpPath, wf_img('skins/icon_download.png', __('Download')), false, '');
  41. $actLinks .= wf_JSAlert('?module=backups&restore=true&restoredump=' . $encodedDumpPath, wf_img('skins/icon_restoredb.png', __('Restore DB')), __('Are you serious'));
  42. $cells = wf_TableCell($fileDate);
  43. $cells .= wf_TableCell($fileSize);
  44. $cells .= wf_TableCell($downloadLink);
  45. $cells .= wf_TableCell($actLinks);
  46. $rows .= wf_TableRow($cells, 'row5');
  47. }
  48. }
  49. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  50. }
  51. return ($result);
  52. }
  53. /**
  54. * Renders list of important configs with some download controls
  55. *
  56. * @return string
  57. */
  58. function web_ConfigsUbillingList() {
  59. $downloadable = array(
  60. 'config/billing.ini',
  61. 'config/mysql.ini',
  62. 'config/alter.ini',
  63. 'config/ymaps.ini',
  64. 'config/photostorage.ini',
  65. 'config/config.ini',
  66. 'config/dhcp/global.template',
  67. 'config/dhcp/subnets.template',
  68. 'config/dhcp/option82.template',
  69. 'config/dhcp/option82_vpu.template',
  70. 'userstats/config/mysql.ini',
  71. 'userstats/config/userstats.ini',
  72. 'userstats/config/tariffmatrix.ini'
  73. );
  74. if (!empty($downloadable)) {
  75. $cells = wf_TableCell(__('Date'));
  76. $cells .= wf_TableCell(__('Size'));
  77. $cells .= wf_TableCell(__('Filename'));
  78. $rows = wf_TableRow($cells, 'row1');
  79. foreach ($downloadable as $eachConfig) {
  80. if (file_exists($eachConfig)) {
  81. $fileDate = filectime($eachConfig);
  82. $fileDate = date("Y-m-d H:i:s", $fileDate);
  83. $fileSize = filesize($eachConfig);
  84. $fileSize = stg_convert_size($fileSize);
  85. $downloadLink = wf_Link('?module=backups&download=' . base64_encode($eachConfig), $eachConfig, false, '');
  86. $cells = wf_TableCell($fileDate);
  87. $cells .= wf_TableCell($fileSize);
  88. $cells .= wf_TableCell($downloadLink);
  89. $rows .= wf_TableRow($cells, 'row5');
  90. } else {
  91. $cells = wf_TableCell('');
  92. $cells .= wf_TableCell('');
  93. $cells .= wf_TableCell($eachConfig);
  94. $rows .= wf_TableRow($cells, 'row5');
  95. }
  96. }
  97. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  98. }
  99. return ($result);
  100. }
  101. /**
  102. * Shows database cleanup form
  103. *
  104. * @return string
  105. */
  106. function web_DBCleanupForm() {
  107. $oldLogs = zb_DBCleanupGetLogs();
  108. $oldDetailstat = zb_DBCleanupGetDetailstat();
  109. $cleanupData = $oldLogs + $oldDetailstat;
  110. $result = '';
  111. $totalRows = 0;
  112. $totalSize = 0;
  113. $totalCount = 0;
  114. $cells = wf_TableCell(__('Table name'));
  115. $cells .= wf_TableCell(__('Rows'));
  116. $cells .= wf_TableCell(__('Size'));
  117. $cells .= wf_TableCell(__('Actions'));
  118. $rows = wf_TableRow($cells, 'row1');
  119. if (!empty($cleanupData)) {
  120. foreach ($cleanupData as $io => $each) {
  121. $cells = wf_TableCell($each['name']);
  122. $cells .= wf_TableCell($each['rows']);
  123. $cells .= wf_TableCell(stg_convert_size($each['size']), '', '', 'sorttable_customkey="' . $each['size'] . '"');
  124. $actlink = wf_JSAlert("?module=backups&tableclean=" . $each['name'], web_delete_icon(), 'Are you serious');
  125. $cells .= wf_TableCell($actlink);
  126. $rows .= wf_TableRow($cells, 'row5');
  127. $totalRows = $totalRows + $each['rows'];
  128. $totalSize = $totalSize + $each['size'];
  129. $totalCount = $totalCount + 1;
  130. }
  131. }
  132. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  133. $result .= wf_tag('b') . __('Total') . ': ' . $totalCount . ' / ' . $totalRows . ' / ' . stg_convert_size($totalSize) . wf_tag('b', true);
  134. return ($result);
  135. }
  136. /**
  137. * Dumps database to file and returns filename
  138. *
  139. * @param bool $silent
  140. *
  141. * @return string
  142. */
  143. function zb_BackupDatabase($silent = false) {
  144. global $ubillingConfig;
  145. $backname = '';
  146. $backupProcess = new StarDust('BACKUPDB');
  147. if ($backupProcess->notRunning()) {
  148. $backupProcess->start();
  149. $alterConf = $ubillingConfig->getAlter();
  150. $mysqlConf = rcms_parse_ini_file(CONFIG_PATH . 'mysql.ini');
  151. $backname = DATA_PATH . 'backups/sql/ubilling-' . date("Y-m-d_H_i_s", time()) . '.sql';
  152. $command = $alterConf['MYSQLDUMP_PATH'] . ' --host ' . $mysqlConf['server'] . ' -u ' . $mysqlConf['username'] . ' -p' . $mysqlConf['password'] . ' ' . $mysqlConf['db'] . ' > ' . $backname;
  153. shell_exec($command);
  154. if (!$silent) {
  155. show_success(__('Backup saved') . ': ' . $backname);
  156. }
  157. log_register('BACKUP CREATE `' . $backname . '`');
  158. $backupProcess->stop();
  159. } else {
  160. log_register('BACKUP ALREADY RUNNING SKIPPED');
  161. }
  162. return ($backname);
  163. }
  164. /**
  165. * Destroy or flush table in database
  166. *
  167. * @param $tablename string table name
  168. * @return void
  169. */
  170. function zb_DBTableCleanup($tablename) {
  171. $tablename = vf($tablename);
  172. $method = 'DROP';
  173. if (!empty($tablename)) {
  174. $query = $method . " TABLE `" . $tablename . "`";
  175. nr_query($query);
  176. log_register("DBCLEANUP `" . $tablename . "`");
  177. }
  178. }
  179. /**
  180. * Auto Cleans all deprecated data
  181. *
  182. * @return string count of cleaned tables
  183. */
  184. function zb_DBCleanupAutoClean() {
  185. $oldLogs = zb_DBCleanupGetLogs();
  186. $oldDstat = zb_DBCleanupGetDetailstat();
  187. $allClean = $oldLogs + $oldDstat;
  188. $counter = 0;
  189. if (!empty($allClean)) {
  190. foreach ($allClean as $io => $each) {
  191. zb_DBTableCleanup($each['name']);
  192. $counter++;
  193. }
  194. }
  195. return ($counter);
  196. }
  197. /**
  198. * Gets list of old stargazer log_ tables exept current month
  199. *
  200. * @return array
  201. */
  202. function zb_DBCleanupGetLogs() {
  203. $logs_query = "SHOW TABLE STATUS WHERE `Name` LIKE 'logs_%'";
  204. $allogs = simple_queryall($logs_query);
  205. $oldlogs = array();
  206. $skiplog = 'logs_' . date("m") . '_' . date("Y");
  207. if (!empty($allogs)) {
  208. foreach ($allogs as $io => $each) {
  209. $filtered = array_values($each);
  210. $oldlogs[$filtered[0]]['name'] = $each['Name'];
  211. $oldlogs[$filtered[0]]['rows'] = $each['Rows'];
  212. $oldlogs[$filtered[0]]['size'] = $each['Data_length'];
  213. }
  214. }
  215. if (!empty($oldlogs)) {
  216. unset($oldlogs[$skiplog]);
  217. }
  218. return ($oldlogs);
  219. }
  220. /**
  221. * Gets list of old stargazer detailstat_ tables exept current month
  222. *
  223. * @return array
  224. */
  225. function zb_DBCleanupGetDetailstat() {
  226. $detail_query = "SHOW TABLE STATUS WHERE `Name` LIKE 'detailstat_%'";
  227. $all = simple_queryall($detail_query);
  228. $old = array();
  229. $skip = 'detailstat_' . date("m") . '_' . date("Y");
  230. if (!empty($all)) {
  231. foreach ($all as $io => $each) {
  232. $filtered = array_values($each);
  233. $old[$filtered[0]]['name'] = $each['Name'];
  234. $old[$filtered[0]]['rows'] = $each['Rows'];
  235. $old[$filtered[0]]['size'] = $each['Data_length'];
  236. }
  237. }
  238. if (!empty($old)) {
  239. unset($old[$skip]);
  240. }
  241. return ($old);
  242. }