ajax.backup.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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. try
  43. {
  44. set_time_limit(0);
  45. $oBB = new BackupExec(APPROOT.'data/backups/manual/', 0 /*iRetentionCount*/);
  46. $sRes = $oBB->Process(time() + 36000); // 10 hours to complete should be sufficient!
  47. }
  48. catch (Exception $e)
  49. {
  50. $oPage->p('Error: '.$e->getMessage());
  51. }
  52. $oPage->output();
  53. break;
  54. case 'restore_get_token':
  55. require_once(APPROOT.'/application/startup.inc.php');
  56. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  57. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  58. $oPage = new ajax_page("");
  59. $oPage->no_cache();
  60. $oPage->SetContentType('text/html');
  61. $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
  62. $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
  63. if ($oRestoreMutex->TryLock())
  64. {
  65. $oRestoreMutex->Unlock();
  66. $sFile = utils::ReadParam('file', '', false, 'raw_data');
  67. $sToken = str_replace(' ', '', (string)microtime());
  68. $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
  69. file_put_contents($sTokenFile, $sFile);
  70. $oPage->add_ready_script(
  71. <<<EOF
  72. $("#restore_token").val('$sToken');
  73. EOF
  74. );
  75. }
  76. else
  77. {
  78. $oPage->p(Dict::S('bkp-restore-running'));
  79. }
  80. $oPage->output();
  81. break;
  82. case 'restore_exec':
  83. require_once(APPROOT."setup/runtimeenv.class.inc.php");
  84. require_once(APPROOT.'/application/utils.inc.php');
  85. require_once(APPROOT.'/setup/backup.class.inc.php');
  86. require_once(dirname(__FILE__).'/dbrestore.class.inc.php');
  87. IssueLog::Enable(APPROOT.'log/error.log');
  88. $oPage = new ajax_page("");
  89. $oPage->no_cache();
  90. $oPage->SetContentType('text/html');
  91. $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data');
  92. $oRestoreMutex = new iTopMutex('restore.'.$sEnvironment);
  93. $oRestoreMutex->Lock();
  94. try
  95. {
  96. set_time_limit(0);
  97. // Get the file and destroy the token (single usage)
  98. $sToken = utils::ReadParam('token', '', false, 'raw_data');
  99. $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
  100. $sFile = file_get_contents($sTokenFile);
  101. unlink($sTokenFile);
  102. $sMySQLBinDir = utils::ReadParam('mysql_bindir', '', false, 'raw_data');
  103. $sDBHost = utils::ReadParam('db_host', '', false, 'raw_data');
  104. $sDBUser = utils::ReadParam('db_user', '', false, 'raw_data');
  105. $sDBPwd = utils::ReadParam('db_pwd', '', false, 'raw_data');
  106. $sDBName = utils::ReadParam('db_name', '', false, 'raw_data');
  107. $sDBSubName = utils::ReadParam('db_subname', '', false, 'raw_data');
  108. $oDBRS = new DBRestore($sDBHost, $sDBUser, $sDBPwd, $sDBName, $sDBSubName);
  109. $oDBRS->SetMySQLBinDir($sMySQLBinDir);
  110. $sBackupDir = APPROOT.'data/backups/';
  111. $sBackupFile = $sBackupDir.$sFile;
  112. $sRes = $oDBRS->RestoreFromZip($sBackupFile, $sEnvironment);
  113. $oRestoreMutex->Unlock();
  114. }
  115. catch (Exception $e)
  116. {
  117. $oRestoreMutex->Unlock();
  118. $oPage->p('Error: '.$e->getMessage());
  119. }
  120. $oPage->output();
  121. break;
  122. case 'download':
  123. require_once(APPROOT.'/application/startup.inc.php');
  124. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  125. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  126. $sFile = utils::ReadParam('file', '', false, 'raw_data');
  127. $oBackup = new DBBackupScheduled();
  128. $sBackupDir = APPROOT.'data/backups/';
  129. $oBackup->DownloadBackup($sBackupDir.$sFile);
  130. break;
  131. }
  132. }
  133. catch (Exception $e)
  134. {
  135. IssueLog::Error($e->getMessage());
  136. }
  137. ?>