ajax.backup.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // Copyright (C) 2013-2017 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-2017 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->IsLocked())
  71. {
  72. $sFile = utils::ReadParam('file', '', false, 'raw_data');
  73. $sToken = str_replace(' ', '', (string)microtime());
  74. $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
  75. file_put_contents($sTokenFile, $sFile);
  76. $oPage->add_ready_script(
  77. <<<EOF
  78. $("#restore_token").val('$sToken');
  79. EOF
  80. );
  81. }
  82. else
  83. {
  84. $oPage->p(Dict::S('bkp-restore-running'));
  85. }
  86. $oPage->output();
  87. break;
  88. case 'restore_exec':
  89. require_once(APPROOT."setup/runtimeenv.class.inc.php");
  90. require_once(APPROOT.'/application/utils.inc.php');
  91. require_once(APPROOT.'/setup/backup.class.inc.php');
  92. require_once(dirname(__FILE__).'/dbrestore.class.inc.php');
  93. IssueLog::Enable(APPROOT.'log/error.log');
  94. $oPage = new ajax_page("");
  95. $oPage->no_cache();
  96. $oPage->SetContentType('text/html');
  97. if (utils::GetConfig()->Get('demo_mode'))
  98. {
  99. $oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>");
  100. }
  101. else
  102. {
  103. $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
  104. $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
  105. IssueLog::Info("Backup Restore - Acquiring the LOCK 'restore.$sEnvironment'");
  106. $oRestoreMutex->Lock();
  107. IssueLog::Info('Backup Restore - LOCK acquired, executing...');
  108. try
  109. {
  110. set_time_limit(0);
  111. // Get the file and destroy the token (single usage)
  112. $sToken = utils::ReadParam('token', '', false, 'raw_data');
  113. $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
  114. if (!is_file($sTokenFile))
  115. {
  116. throw new Exception("Error: missing token file: '$sTokenFile'");
  117. }
  118. $sFile = file_get_contents($sTokenFile);
  119. unlink($sTokenFile);
  120. $sMySQLBinDir = utils::ReadParam('mysql_bindir', '', false, 'raw_data');
  121. $sDBHost = utils::ReadParam('db_host', '', false, 'raw_data');
  122. $sDBUser = utils::ReadParam('db_user', '', false, 'raw_data');
  123. $sDBPwd = utils::ReadParam('db_pwd', '', false, 'raw_data');
  124. $sDBName = utils::ReadParam('db_name', '', false, 'raw_data');
  125. $sDBSubName = utils::ReadParam('db_subname', '', false, 'raw_data');
  126. $oDBRS = new DBRestore($sDBHost, $sDBUser, $sDBPwd, $sDBName, $sDBSubName);
  127. $oDBRS->SetMySQLBinDir($sMySQLBinDir);
  128. $sBackupDir = APPROOT.'data/backups/';
  129. $sBackupFile = $sBackupDir.$sFile;
  130. $sRes = $oDBRS->RestoreFromCompressedBackup($sBackupFile, $sEnvironment);
  131. IssueLog::Info('Backup Restore - Done, releasing the LOCK');
  132. $oRestoreMutex->Unlock();
  133. }
  134. catch (Exception $e)
  135. {
  136. $oRestoreMutex->Unlock();
  137. $oPage->p('Error: '.$e->getMessage());
  138. }
  139. }
  140. $oPage->output();
  141. break;
  142. case 'download':
  143. require_once(APPROOT.'/application/startup.inc.php');
  144. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  145. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  146. if (utils::GetConfig()->Get('demo_mode'))
  147. {
  148. throw new Exception('iTop is in demonstration mode: the feature is disabled');
  149. }
  150. $sFile = utils::ReadParam('file', '', false, 'raw_data');
  151. $oBackup = new DBBackupScheduled();
  152. $sBackupDir = APPROOT.'data/backups/';
  153. $oBackup->DownloadBackup($sBackupDir.$sFile);
  154. break;
  155. }
  156. }
  157. catch (Exception $e)
  158. {
  159. IssueLog::Error($e->getMessage());
  160. }