ajax.backup.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // Copyright (C) 2014 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Backup from an interactive session
  20. *
  21. * @copyright Copyright (C) 2013 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
  25. if (!defined('APPROOT')) require_once(__DIR__.'/../../approot.inc.php');
  26. require_once(APPROOT.'/application/application.inc.php');
  27. require_once(APPROOT.'/application/webpage.class.inc.php');
  28. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  29. require_once(APPROOT.'core/mutex.class.inc.php');
  30. try
  31. {
  32. $sOperation = utils::ReadParam('operation', '');
  33. switch($sOperation)
  34. {
  35. case 'backup':
  36. require_once(APPROOT.'/application/startup.inc.php');
  37. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  38. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  39. $oPage = new ajax_page("");
  40. $oPage->no_cache();
  41. $oPage->SetContentType('text/html');
  42. if (utils::GetConfig()->Get('demo_mode'))
  43. {
  44. $oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>");
  45. }
  46. else
  47. {
  48. try
  49. {
  50. set_time_limit(0);
  51. $oBB = new BackupExec(APPROOT.'data/backups/manual/', 0 /*iRetentionCount*/);
  52. $sRes = $oBB->Process(time() + 36000); // 10 hours to complete should be sufficient!
  53. }
  54. catch (Exception $e)
  55. {
  56. $oPage->p('Error: '.$e->getMessage());
  57. }
  58. }
  59. $oPage->output();
  60. break;
  61. case 'restore_get_token':
  62. require_once(APPROOT.'/application/startup.inc.php');
  63. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  64. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  65. $oPage = new ajax_page("");
  66. $oPage->no_cache();
  67. $oPage->SetContentType('text/html');
  68. $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
  69. $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
  70. if ($oRestoreMutex->TryLock())
  71. {
  72. $oRestoreMutex->Unlock();
  73. $sFile = utils::ReadParam('file', '', false, 'raw_data');
  74. $sToken = str_replace(' ', '', (string)microtime());
  75. $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
  76. file_put_contents($sTokenFile, $sFile);
  77. $oPage->add_ready_script(
  78. <<<EOF
  79. $("#restore_token").val('$sToken');
  80. EOF
  81. );
  82. }
  83. else
  84. {
  85. $oPage->p(Dict::S('bkp-restore-running'));
  86. }
  87. $oPage->output();
  88. break;
  89. case 'restore_exec':
  90. require_once(APPROOT."setup/runtimeenv.class.inc.php");
  91. require_once(APPROOT.'/application/utils.inc.php');
  92. require_once(APPROOT.'/setup/backup.class.inc.php');
  93. require_once(dirname(__FILE__).'/dbrestore.class.inc.php');
  94. IssueLog::Enable(APPROOT.'log/error.log');
  95. $oPage = new ajax_page("");
  96. $oPage->no_cache();
  97. $oPage->SetContentType('text/html');
  98. if (utils::GetConfig()->Get('demo_mode'))
  99. {
  100. $oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>");
  101. }
  102. else
  103. {
  104. $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
  105. $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
  106. $oRestoreMutex->Lock();
  107. try
  108. {
  109. set_time_limit(0);
  110. // Get the file and destroy the token (single usage)
  111. $sToken = utils::ReadParam('token', '', false, 'raw_data');
  112. $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
  113. if (!is_file($sTokenFile))
  114. {
  115. throw new Exception("Error: missing token file: '$sTokenFile'");
  116. }
  117. $sFile = file_get_contents($sTokenFile);
  118. unlink($sTokenFile);
  119. $sMySQLBinDir = utils::ReadParam('mysql_bindir', '', false, 'raw_data');
  120. $sDBHost = utils::ReadParam('db_host', '', false, 'raw_data');
  121. $sDBUser = utils::ReadParam('db_user', '', false, 'raw_data');
  122. $sDBPwd = utils::ReadParam('db_pwd', '', false, 'raw_data');
  123. $sDBName = utils::ReadParam('db_name', '', false, 'raw_data');
  124. $sDBSubName = utils::ReadParam('db_subname', '', false, 'raw_data');
  125. $oDBRS = new DBRestore($sDBHost, $sDBUser, $sDBPwd, $sDBName, $sDBSubName);
  126. $oDBRS->SetMySQLBinDir($sMySQLBinDir);
  127. $sBackupDir = APPROOT.'data/backups/';
  128. $sBackupFile = $sBackupDir.$sFile;
  129. $sRes = $oDBRS->RestoreFromZip($sBackupFile, $sEnvironment);
  130. $oRestoreMutex->Unlock();
  131. }
  132. catch (Exception $e)
  133. {
  134. $oRestoreMutex->Unlock();
  135. $oPage->p('Error: '.$e->getMessage());
  136. }
  137. }
  138. $oPage->output();
  139. break;
  140. case 'download':
  141. require_once(APPROOT.'/application/startup.inc.php');
  142. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  143. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  144. if (utils::GetConfig()->Get('demo_mode'))
  145. {
  146. throw new Exception('iTop is in demonstration mode: the feature is disabled');
  147. }
  148. $sFile = utils::ReadParam('file', '', false, 'raw_data');
  149. $oBackup = new DBBackupScheduled();
  150. $sBackupDir = APPROOT.'data/backups/';
  151. $oBackup->DownloadBackup($sBackupDir.$sFile);
  152. break;
  153. }
  154. }
  155. catch (Exception $e)
  156. {
  157. IssueLog::Error($e->getMessage());
  158. }
  159. ?>