backup.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // Copyright (C) 2014-2017 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
  17. if (!defined('APPROOT')) require_once(__DIR__.'/../../approot.inc.php');
  18. require_once(APPROOT.'application/application.inc.php');
  19. require_once(APPROOT.'application/webpage.class.inc.php');
  20. require_once(APPROOT.'application/csvpage.class.inc.php');
  21. require_once(APPROOT.'application/clipage.class.inc.php');
  22. require_once(APPROOT.'application/ajaxwebpage.class.inc.php');
  23. require_once(APPROOT.'core/log.class.inc.php');
  24. require_once(APPROOT.'application/startup.inc.php');
  25. class MyDBBackup extends DBBackup
  26. {
  27. protected function LogInfo($sMsg)
  28. {
  29. $this->oPage->p($sMsg);
  30. }
  31. protected function LogError($sMsg)
  32. {
  33. $this->oPage->p('Error: '.$sMsg);
  34. ToolsLog::Error($sMsg);
  35. }
  36. protected $oPage;
  37. public function __construct($oPage)
  38. {
  39. $this->oPage = $oPage;
  40. parent::__construct();
  41. }
  42. }
  43. /**
  44. * Checks if a parameter (possibly empty) was specified when calling this page
  45. */
  46. function CheckParam($sParamName)
  47. {
  48. global $argv;
  49. if (isset($_REQUEST[$sParamName])) return true; // HTTP parameter either GET or POST
  50. if (!is_array($argv)) return false;
  51. foreach($argv as $sArg)
  52. {
  53. if ($sArg == '--'.$sParamName) return true; // Empty command line parameter, long unix style
  54. if ($sArg == $sParamName) return true; // Empty command line parameter, Windows style
  55. if ($sArg == '-'.$sParamName) return true; // Empty command line parameter, short unix style
  56. if (preg_match('/^--'.$sParamName.'=(.*)$/', $sArg, $aMatches)) return true; // Command parameter with a value
  57. }
  58. return false;
  59. }
  60. function Usage($oP)
  61. {
  62. $oP->p('Perform a backup of the iTop database by running mysqldump');
  63. $oP->p('Parameters:');
  64. if (utils::IsModeCLI())
  65. {
  66. $oP->p('auth_user: login, must be administrator');
  67. $oP->p('auth_pwd: ...');
  68. }
  69. $oP->p('backup_file [optional]: name of the file to store the backup into. Follows the PHP strftime format spec. The following placeholders are available: __HOST__, __DB__, __SUBNAME__');
  70. $oP->p('simulate [optional]: set to check the name of the file that would be created');
  71. $oP->p('mysql_bindir [optional]: specify the path for mysqldump');
  72. if (utils::IsModeCLI())
  73. {
  74. $oP->p('Example: php -q backup.php --auth_user=admin --auth_pwd=myPassw0rd');
  75. $oP->p('Known limitation: the current directory must be the directory of backup.php');
  76. }
  77. else
  78. {
  79. $oP->p('Example: .../backup.php?backup_file=/tmp/backup.__DB__-__SUBNAME__.%Y-%m');
  80. }
  81. }
  82. function ExitError($oP, $sMessage)
  83. {
  84. ToolsLog::Error($sMessage);
  85. $oP->p($sMessage);
  86. $oP->output();
  87. exit;
  88. }
  89. function ReadMandatoryParam($oP, $sParam)
  90. {
  91. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */, 'raw_data');
  92. if (is_null($sValue))
  93. {
  94. ExitError($oP, "ERROR: Missing argument '$sParam'");
  95. }
  96. return trim($sValue);
  97. }
  98. /////////////////////////////////
  99. // Main program
  100. set_time_limit(0);
  101. if (utils::IsModeCLI())
  102. {
  103. $oP = new CLIPage("iTop - Database Backup");
  104. }
  105. else
  106. {
  107. $oP = new WebPage("iTop - Database Backup");
  108. }
  109. try
  110. {
  111. utils::UseParamFile();
  112. }
  113. catch(Exception $e)
  114. {
  115. ExitError($oP, $e->GetMessage());
  116. }
  117. if (utils::IsModeCLI())
  118. {
  119. $oP->p(date('Y-m-d H:i:s')." - running backup utility");
  120. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  121. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  122. $bDownloadBackup = false;
  123. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  124. {
  125. UserRights::Login($sAuthUser); // Login & set the user's language
  126. }
  127. else
  128. {
  129. ExitError($oP, "Access restricted or wrong credentials ('$sAuthUser')");
  130. }
  131. }
  132. else
  133. {
  134. require_once(APPROOT.'application/loginwebpage.class.inc.php');
  135. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  136. $bDownloadBackup = utils::ReadParam('download', false);
  137. }
  138. if (!UserRights::IsAdministrator())
  139. {
  140. ExitError($oP, "Access restricted to administors");
  141. }
  142. if (CheckParam('?') || CheckParam('h') || CheckParam('help'))
  143. {
  144. Usage($oP);
  145. $oP->output();
  146. exit;
  147. }
  148. $sDefaultBackupFileName = SetupUtils::GetTmpDir().'/'."__DB__-%Y-%m-%d";
  149. $sBackupFile = utils::ReadParam('backup_file', $sDefaultBackupFileName, true, 'raw_data');
  150. // Interpret strftime specifications (like %Y) and database placeholders
  151. $oBackup = new MyDBBackup($oP);
  152. $oBackup->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', ''));
  153. $sBackupFile = $oBackup->MakeName($sBackupFile);
  154. $bSimulate = utils::ReadParam('simulate', false, true);
  155. $res = false;
  156. if ($bSimulate)
  157. {
  158. $oP->p("Simulate: would create file '$sBackupFile'");
  159. }
  160. elseif (MetaModel::GetConfig()->Get('demo_mode'))
  161. {
  162. $oP->p("Sorry, iTop is in demonstration mode: the feature is disabled");
  163. }
  164. else
  165. {
  166. $oBackup->CreateCompressedBackup($sBackupFile);
  167. }
  168. if ($res && $bDownloadBackup)
  169. {
  170. $oBackup->DownloadBackup($sBackupFile);
  171. }
  172. $oP->output();