4
0

backup-functions.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. trait BackupFunctions
  3. {
  4. public function fileArray($files)
  5. {
  6. foreach ($files as $file) {
  7. if (file_exists($file)) {
  8. $list[] = $file;
  9. }
  10. }
  11. if (!empty($list)) {
  12. return $list;
  13. }
  14. }
  15. public function deleteBackup($filename)
  16. {
  17. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  18. $path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  19. $filename = $path . $filename;
  20. if ($ext == 'zip') {
  21. if (file_exists($filename)) {
  22. $this->setLoggerChannel('Backup')->info('Deleted Backup [' . pathinfo($filename, PATHINFO_BASENAME) . ']');
  23. $this->setAPIResponse(null, pathinfo($filename, PATHINFO_BASENAME) . ' has been deleted', null);
  24. return (unlink($filename));
  25. } else {
  26. $this->setAPIResponse('error', 'File does not exist', 404);
  27. return false;
  28. }
  29. } else {
  30. $this->setAPIResponse('error', pathinfo($filename, PATHINFO_BASENAME) . ' is not approved to be deleted', 409);
  31. return false;
  32. }
  33. }
  34. public function downloadBackup($filename)
  35. {
  36. $path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  37. $filename = $path . $filename;
  38. if (file_exists($filename)) {
  39. header('Content-Type: application/zip');
  40. header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
  41. header('Content-Length: ' . filesize($filename));
  42. flush();
  43. readfile($filename);
  44. exit();
  45. } else {
  46. $this->setAPIResponse('error', 'File does not exist', 404);
  47. return false;
  48. }
  49. }
  50. public function backupOrganizr($type = 'config')
  51. {
  52. $directory = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  53. @mkdir($directory, 0770, true);
  54. switch ($type) {
  55. case 'config':
  56. break;
  57. case 'full':
  58. break;
  59. default:
  60. }
  61. $this->setLoggerChannel('Backup')->notice('Backing up Organizr');
  62. $zipname = $directory . 'backup[' . date('Y-m-d_H-i') . ' - ' . $this->random_ascii_string(2) . '][' . $this->version . '].zip';
  63. $zip = new ZipArchive;
  64. $zip->open($zipname, ZipArchive::CREATE);
  65. if ($this->config['driver'] == 'sqlite3') {
  66. $zip->addFile($this->config['dbLocation'] . $this->config['dbName'], basename($this->config['dbLocation'] . $this->config['dbName']));
  67. }
  68. $rootPath = $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR;
  69. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
  70. foreach ($files as $name => $file) {
  71. // Skip directories (they would be added automatically)
  72. if (!$file->isDir()) {
  73. if (stripos($name, 'data' . DIRECTORY_SEPARATOR . 'cache') == false) {
  74. // Get real and relative path for current file
  75. $filePath = $file->getRealPath();
  76. $relativePath = substr($filePath, strlen($rootPath));
  77. // Add current file to archive
  78. $zip->addFile($filePath, $relativePath);
  79. }
  80. }
  81. }
  82. $zip->close();
  83. $this->setLoggerChannel('Backup')->notice('Backup process finished');
  84. $this->setAPIResponse('success', 'Backup has been created', 200);
  85. $this->deleteBackupsLimit();
  86. return true;
  87. }
  88. public function deleteBackupsLimit()
  89. {
  90. $backups = $this->getBackups();
  91. if ($backups) {
  92. $list = array_reverse($backups['files']);
  93. $killCount = count($list) - $this->config['keepBackupsCountCron'];
  94. if ($killCount >= 1) {
  95. foreach ($list as $count => $backup) {
  96. $count++;
  97. if ($count <= $killCount) {
  98. $this->log('Cron')->notice('Deleting organizr backup file as it is over limit', ['file' => $backup['name']]);
  99. $this->deleteBackup($backup['name']);
  100. }
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. public function getBackups()
  107. {
  108. $path = $this->config['dbLocation'] . 'backups' . DIRECTORY_SEPARATOR;
  109. @mkdir($path, 0770, true);
  110. $files = array_diff(scandir($path), array('.', '..'));
  111. $fileList = [];
  112. $totalFiles = 0;
  113. $totalFileSize = 0;
  114. foreach ($files as $file) {
  115. $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  116. if (file_exists($path . $file) && $ext == 'zip') {
  117. $size = filesize($path . $file);
  118. $totalFileSize = $totalFileSize + $size;
  119. $totalFiles = $totalFiles + 1;
  120. try {
  121. $fileList['files'][] = [
  122. 'name' => $file,
  123. 'size' => $this->human_filesize($size, 0),
  124. 'date' => gmdate("Y-m-d\TH:i:s\Z", (filemtime($path . $file)))
  125. ];
  126. } catch (Exception $e) {
  127. $this->setAPIResponse('error', 'Backup list failed', 409, $e->getMessage());
  128. return false;
  129. }
  130. }
  131. }
  132. $fileList['total_files'] = $totalFiles;
  133. $fileList['total_size'] = $this->human_filesize($totalFileSize, 2);
  134. $fileList['files'] = $totalFiles > 0 ? array_reverse($fileList['files']) : null;
  135. $this->setAPIResponse('success', null, 200, array_reverse($fileList));
  136. return array_reverse($fileList);
  137. }
  138. }