dbrestore.class.inc.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // Copyright (C) 2014-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. 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. /**
  84. * @deprecated Use RestoreFromCompressedBackup instead
  85. * @param $sZipFile
  86. * @param string $sEnvironment
  87. */
  88. public function RestoreFromZip($sZipFile, $sEnvironment = 'production')
  89. {
  90. $this->RestoreFromCompressedBackup($sZipFile, $sEnvironment);
  91. }
  92. /**
  93. * @param $sFile A file with the extension .zip or .tar.gz
  94. * @param string $sEnvironment Target environment
  95. */
  96. public function RestoreFromCompressedBackup($sFile, $sEnvironment = 'production')
  97. {
  98. $this->LogInfo("Starting restore of ".basename($sFile));
  99. $sNormalizedFile = strtolower(basename($sFile));
  100. if (substr($sNormalizedFile, -4) == '.zip')
  101. {
  102. $this->LogInfo('zip file detected');
  103. $oArchive = new ZipArchiveEx();
  104. $res = $oArchive->open($sFile);
  105. }
  106. elseif (substr($sNormalizedFile, -7) == '.tar.gz')
  107. {
  108. $this->LogInfo('tar.gz file detected');
  109. $oArchive = new TarGzArchive($sFile);
  110. }
  111. else
  112. {
  113. throw new Exception('Unsupported format for a backup file: '.$sFile);
  114. }
  115. // Load the database
  116. //
  117. $sDataDir = tempnam(SetupUtils::GetTmpDir(), 'itop-');
  118. unlink($sDataDir); // I need a directory, not a file...
  119. SetupUtils::builddir($sDataDir); // Here is the directory
  120. $oArchive->extractFileTo($sDataDir, 'itop-dump.sql');
  121. $sDataFile = $sDataDir.'/itop-dump.sql';
  122. $this->LoadDatabase($sDataFile);
  123. unlink($sDataFile);
  124. // Update the code
  125. //
  126. $sDeltaFile = APPROOT.'data/'.$sEnvironment.'.delta.xml';
  127. if ($oArchive->hasFile('delta.xml') !== false)
  128. {
  129. // Extract and rename delta.xml => <env>.delta.xml;
  130. file_put_contents($sDeltaFile, $oArchive->getFromName('delta.xml'));
  131. }
  132. else
  133. {
  134. @unlink($sDeltaFile);
  135. }
  136. if (is_dir(APPROOT.'data/production-modules/'))
  137. {
  138. SetupUtils::rrmdir(APPROOT.'data/production-modules/');
  139. }
  140. if ($oArchive->hasDir('production-modules/') !== false)
  141. {
  142. $oArchive->extractDirTo(APPROOT.'data/', 'production-modules/');
  143. }
  144. $sConfigFile = APPROOT.'conf/'.$sEnvironment.'/config-itop.php';
  145. @chmod($sConfigFile, 0770); // Allow overwriting the file
  146. $oArchive->extractFileTo(APPROOT.'conf/'.$sEnvironment, 'config-itop.php');
  147. @chmod($sConfigFile, 0444); // Read-only
  148. $oEnvironment = new RunTimeEnvironment($sEnvironment);
  149. $oEnvironment->CompileFrom($sEnvironment);
  150. }
  151. }