backup.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Copyright (C) 2014
  17. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. include "root.php";
  22. //define the backup class
  23. if (!class_exists('backup')) {
  24. class backup {
  25. public $result;
  26. public $domain_uuid;
  27. public function command($type, $file) {
  28. if ($type == "backup") {
  29. $path = ($_SESSION['server']['backup']['path'] != '') ? $_SESSION['server']['backup']['path'] : '/tmp';
  30. $file = str_replace(array("/", "\\"),'',$file); //remove slashes to prevent changing the directory with the file name
  31. $format = substr($file,-3);
  32. if (strlen($file) == 3) {
  33. $file = 'backup_'.date('Ymd_His').'.'.$format;
  34. }
  35. if (count($_SESSION['backup']['path']) > 0) {
  36. //determine compression method
  37. switch ($format) {
  38. case "rar" : $cmd = 'rar a -ow -r '; break;
  39. case "zip" : $cmd = 'zip -r '; break;
  40. case "tbz" : $cmd = 'tar -jvcf '; break;
  41. default : $cmd = 'tar -zvcf ';
  42. }
  43. $cmd .= $path.'/'.$file.' ';
  44. foreach ($_SESSION['backup']['path'] as $value) {
  45. $cmd .= $value.' ';
  46. }
  47. return $cmd;
  48. }
  49. else {
  50. return false;
  51. }
  52. }
  53. if ($type == "restore") {
  54. $path = ($_SESSION['server']['backup']['path'] != '') ? $_SESSION['server']['backup']['path'] : '/tmp';
  55. $file = str_replace(array("/", "\\"),'',$file); //remove slashes to prevent changing the directory with the file name
  56. $format = substr($file,-3);
  57. if (count($_SESSION['backup']['path']) > 0) {
  58. switch ($format) {
  59. case "rar" : $cmd = 'rar x -ow -o+ '.$path.'/'.$file.' /'; break;
  60. case "zip" : $cmd = 'umask 755; unzip -o -qq -X -K '.$path.'/'.$file.' -d /'; break;
  61. case "tbz" : $cmd = 'tar -xvpjf '.$path.'/'.$file.' -C /'; break;
  62. case "tgz" : $cmd = 'tar -xvpzf '.$path.'/'.$file.' -C /'; break;
  63. default: $valid_format = false;
  64. }
  65. return $cmd;
  66. }
  67. else {
  68. return false;
  69. }
  70. }
  71. }
  72. public function backup($type, $format) {
  73. $cmd = $this->command("backup", $format);
  74. exec($cmd);
  75. return $cmd;
  76. }
  77. public function restore($file) {
  78. $path = ($_SESSION['server']['backup']['path'] != '') ? $_SESSION['server']['backup']['path'] : '/tmp';
  79. $format = substr($file,-3);
  80. switch ($format) {
  81. case "rar" : break;
  82. case "zip" : break;
  83. case "tbz" : break;
  84. case "tgz" : break;
  85. default: return false;
  86. }
  87. $cmd = $this->command("restore", $file);
  88. exec($cmd);
  89. return $cmd;
  90. }
  91. public function download($file) {
  92. $path = ($_SESSION['server']['backup']['path'] != '') ? $_SESSION['server']['backup']['path'] : '/tmp';
  93. session_cache_limiter('public');
  94. $file = str_replace(array("/", "\\"),'',$file); //remove slashes to prevent changing the directory with the file name
  95. if (file_exists($path."/".$file)) {
  96. $fd = fopen($path."/".$file, 'rb');
  97. header("Content-Type: application/octet-stream");
  98. header("Content-Transfer-Encoding: binary");
  99. header("Content-Description: File Transfer");
  100. header('Content-Disposition: attachment; filename='.$file);
  101. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  102. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  103. header("Content-Length: ".filesize($path."/".$file));
  104. header("Pragma: no-cache");
  105. header("Expires: 0");
  106. ob_clean();
  107. fpassthru($fd);
  108. exit;
  109. }
  110. }
  111. }
  112. }
  113. ?>