dbrestore.class.inc.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. class DBRestore extends DBBackup
  19. {
  20. protected function LogInfo($sMsg)
  21. {
  22. //IssueLog::Info('non juste info: '.$sMsg);
  23. }
  24. protected function LogError($sMsg)
  25. {
  26. IssueLog::Error($sMsg);
  27. }
  28. protected function LoadDatabase($sDataFile)
  29. {
  30. $this->LogInfo("Loading data onto $this->sDBHost/$this->sDBName(suffix:'$this->sDBSubName')");
  31. // Just to check the connection to the DB (more accurate than getting the retcode of mysql)
  32. $oMysqli = $this->DBConnect();
  33. $sHost = self::EscapeShellArg($this->sDBHost);
  34. $sUser = self::EscapeShellArg($this->sDBUser);
  35. $sPwd = self::EscapeShellArg($this->sDBPwd);
  36. $sDBName = self::EscapeShellArg($this->sDBName);
  37. if (empty($this->sMySQLBinDir))
  38. {
  39. $sMySQLExe = 'mysql';
  40. }
  41. else
  42. {
  43. $sMySQLExe = '"'.$this->sMySQLBinDir.'/mysql"';
  44. }
  45. if (is_null($this->iDBPort))
  46. {
  47. $sPortOption = '';
  48. }
  49. else
  50. {
  51. $sPortOption = '--port='.$this->iDBPort.' ';
  52. }
  53. $sDataFileEscaped = self::EscapeShellArg($sDataFile);
  54. $sCommand = "$sMySQLExe --default-character-set=utf8 --host=$sHost $sPortOption --user=$sUser --password=$sPwd $sDBName <$sDataFileEscaped 2>&1";
  55. $sCommandDisplay = "$sMySQLExe --default-character-set=utf8 --host=$sHost $sPortOption --user=xxxx --password=xxxx $sDBName <$sDataFileEscaped 2>&1";
  56. // Now run the command for real
  57. $this->LogInfo("Executing command: $sCommandDisplay");
  58. $aOutput = array();
  59. $iRetCode = 0;
  60. exec($sCommand, $aOutput, $iRetCode);
  61. foreach($aOutput as $sLine)
  62. {
  63. $this->LogInfo("mysql said: $sLine");
  64. }
  65. if ($iRetCode != 0)
  66. {
  67. $this->LogError("Failed to execute: $sCommandDisplay. The command returned:$iRetCode");
  68. foreach($aOutput as $sLine)
  69. {
  70. $this->LogError("mysql said: $sLine");
  71. }
  72. if (count($aOutput) == 1)
  73. {
  74. $sMoreInfo = trim($aOutput[0]);
  75. }
  76. else
  77. {
  78. $sMoreInfo = "Check the log file '".realpath(APPROOT.'/log/error.log')."' for more information.";
  79. }
  80. throw new BackupException("Failed to execute mysql: ".$sMoreInfo);
  81. }
  82. }
  83. public function RestoreFromZip($sZipFile, $sEnvironment = 'production')
  84. {
  85. $this->LogInfo("Starting restore of ".basename($sZipFile));
  86. $oZip = new ZipArchive();
  87. $res = $oZip->open($sZipFile);
  88. // Load the database
  89. //
  90. $sDataDir = tempnam(SetupUtils::GetTmpDir(), 'itop-');
  91. unlink($sDataDir); // I need a directory, not a file...
  92. SetupUtils::builddir($sDataDir); // Here is the directory
  93. $oZip->extractTo($sDataDir, 'itop-dump.sql');
  94. $sDataFile = $sDataDir.'/itop-dump.sql';
  95. $this->LoadDatabase($sDataFile);
  96. unlink($sDataFile);
  97. // Update the code
  98. //
  99. $sDeltaFile = APPROOT.'data/'.$sEnvironment.'.delta.xml';
  100. if ($oZip->locateName('delta.xml') !== false)
  101. {
  102. // Extract and rename delta.xml => <env>.delta.xml;
  103. file_put_contents($sDeltaFile, $oZip->getFromName('delta.xml'));
  104. }
  105. else
  106. {
  107. @unlink($sDeltaFile);
  108. }
  109. $sConfigFile = APPROOT.'conf/'.$sEnvironment.'/config-itop.php';
  110. @chmod($sConfigFile, 0770); // Allow overwriting the file
  111. $oZip->extractTo(APPROOT.'conf/'.$sEnvironment, 'config-itop.php');
  112. @chmod($sConfigFile, 0444); // Read-only
  113. $oEnvironment = new RunTimeEnvironment($sEnvironment);
  114. $oEnvironment->CompileFrom($sEnvironment);
  115. }
  116. }