api.backups.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * Converts bytes into human-readable values like Kb, Mb, Gb...
  17. *
  18. * @param int $fs
  19. * @param string $traffsize
  20. *
  21. * @return string
  22. */
  23. function wr_convertSize($fs, $traffsize = 'float') {
  24. if ($traffsize == 'float') {
  25. if ($fs >= (1073741824 * 1024))
  26. $fs = round($fs / (1073741824 * 1024) * 100) / 100 . ' ' . __('Tb');
  27. elseif ($fs >= 1073741824)
  28. $fs = round($fs / 1073741824 * 100) / 100 . ' ' . __('Gb');
  29. elseif ($fs >= 1048576)
  30. $fs = round($fs / 1048576 * 100) / 100 . ' ' . __('Mb');
  31. elseif ($fs >= 1024)
  32. $fs = round($fs / 1024 * 100) / 100 . ' ' . __('Kb');
  33. else
  34. $fs = $fs . ' ' . __('b');
  35. return ($fs);
  36. }
  37. if ($traffsize == 'b') {
  38. return ($fs);
  39. }
  40. if ($traffsize == 'Kb') {
  41. $fs = round($fs / 1024 * 100) / 100 . ' ' . __('Kb');
  42. return ($fs);
  43. }
  44. if ($traffsize == 'Mb') {
  45. $fs = round($fs / 1048576 * 100) / 100 . ' ' . __('Mb');
  46. return ($fs);
  47. }
  48. if ($traffsize == 'Gb') {
  49. $fs = round($fs / 1073741824 * 100) / 100 . ' ' . __('Gb');
  50. return ($fs);
  51. }
  52. if ($traffsize == 'Tb') {
  53. $fs = round($fs / (1073741824 * 1024) * 100) / 100 . ' ' . __('Tb');
  54. return ($fs);
  55. }
  56. }
  57. /**
  58. * Dumps database to file and returns filename
  59. *
  60. * @param bool $silent
  61. *
  62. * @return string
  63. */
  64. function zb_BackupDatabase($silent = false) {
  65. global $ubillingConfig;
  66. $backname = '';
  67. $backupProcess = new StarDust('BACKUPDB');
  68. if ($backupProcess->notRunning()) {
  69. $backupProcess->start();
  70. $alterConf = $ubillingConfig->getAlter();
  71. $binPathsConf = $ubillingConfig->getBinpaths();
  72. $mysqlConf = rcms_parse_ini_file(CONFIG_PATH . 'mysql.ini');
  73. $backname = DATA_PATH . 'backups/sql/wolfrecorder-' . date("Y-m-d_H_i_s", time()) . '.sql';
  74. $command = $binPathsConf['MYSQLDUMP_PATH'] . ' --host ' . $mysqlConf['server'] . ' -u ' . $mysqlConf['username'] . ' -p' . $mysqlConf['password'] . ' ' . $mysqlConf['db'] . ' > ' . $backname;
  75. shell_exec($command);
  76. if (!$silent) {
  77. show_success(__('Backup saved') . ': ' . $backname);
  78. }
  79. log_register('BACKUP CREATE `' . $backname . '`');
  80. $backupProcess->stop();
  81. } else {
  82. log_register('BACKUP ALREADY RUNNING SKIPPED');
  83. }
  84. return ($backname);
  85. }
  86. /**
  87. * Shows database cleanup form
  88. *
  89. * @return string
  90. */
  91. function web_DBCleanupForm() {
  92. $cleanupData = $oldLogs + $oldDetailstat;
  93. $result = '';
  94. $totalRows = 0;
  95. $totalSize = 0;
  96. $totalCount = 0;
  97. $cells = wf_TableCell(__('Table name'));
  98. $cells .= wf_TableCell(__('Rows'));
  99. $cells .= wf_TableCell(__('Size'));
  100. $cells .= wf_TableCell(__('Actions'));
  101. $rows = wf_TableRow($cells, 'row1');
  102. if (!empty($cleanupData)) {
  103. foreach ($cleanupData as $io => $each) {
  104. $cells = wf_TableCell($each['name']);
  105. $cells .= wf_TableCell($each['rows']);
  106. $cells .= wf_TableCell(wr_convertSize($each['size']), '', '', 'sorttable_customkey="' . $each['size'] . '"');
  107. $actlink = wf_JSAlert("?module=backups&tableclean=" . $each['name'], web_delete_icon(), 'Are you serious');
  108. $cells .= wf_TableCell($actlink);
  109. $rows .= wf_TableRow($cells, 'row5');
  110. $totalRows = $totalRows + $each['rows'];
  111. $totalSize = $totalSize + $each['size'];
  112. $totalCount = $totalCount + 1;
  113. }
  114. }
  115. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  116. $result .= wf_tag('b') . __('Total') . ': ' . $totalCount . ' / ' . $totalRows . ' / ' . wr_convertSize($totalSize) . wf_tag('b', true);
  117. return ($result);
  118. }
  119. /**
  120. * This function perform removing of files and directories
  121. *
  122. * @param string $file
  123. * @param bool $recursive
  124. *
  125. * @return bool
  126. */
  127. function rcms_delete_files($file, $recursive = false) {
  128. if ($recursive && is_dir($file)) {
  129. $els = rcms_scandir($file, '', '', true);
  130. foreach ($els as $el) {
  131. if ($el != '.' && $el != '..') {
  132. rcms_delete_files($file . '/' . $el, true);
  133. }
  134. }
  135. }
  136. if (is_dir($file)) {
  137. return rmdir($file);
  138. } else {
  139. return unlink($file);
  140. }
  141. }
  142. /**
  143. * Cleanups backups directory dumps older than X days encoded in filename.
  144. *
  145. * @param int $maxAge
  146. *
  147. * @return void
  148. */
  149. function zb_BackupsRotate($maxAge) {
  150. $maxAge = vf($maxAge, 3);
  151. if ($maxAge) {
  152. if (is_numeric($maxAge)) {
  153. $curTimeStamp = curdate();
  154. $curTimeStamp = strtotime($curTimeStamp);
  155. $cleanupTimeStamp = $curTimeStamp - ($maxAge * 86400); // Option is in days
  156. $backupsDirectory = DATA_PATH . 'backups/sql/';
  157. $backupsPrefix = 'wolfrecorder-';
  158. $backupsExtension = '.sql';
  159. $allBackups = rcms_scandir($backupsDirectory, '*' . $backupsExtension);
  160. if (!empty($allBackups)) {
  161. foreach ($allBackups as $io => $eachDump) {
  162. //trying to extract date from filename
  163. $cleanName = $eachDump;
  164. $cleanName = str_replace($backupsPrefix, '', $cleanName);
  165. $cleanName = str_replace($backupsExtension, '', $cleanName);
  166. if (ispos($cleanName, '_')) {
  167. $explode = explode('_', $cleanName);
  168. $cleanName = $explode[0];
  169. if (zb_checkDate($cleanName)) {
  170. $dumpTimeStamp = strtotime($cleanName);
  171. if ($dumpTimeStamp < $cleanupTimeStamp) {
  172. $rotateBackupPath = $backupsDirectory . $eachDump;
  173. rcms_delete_files($rotateBackupPath);
  174. log_register('BACKUP ROTATE `' . $rotateBackupPath . '`');
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }