Example #1
0
 public function Compile($sTargetDir, $oP = null, $bUseSymbolicLinks = false)
 {
     $sFinalTargetDir = $sTargetDir;
     if ($bUseSymbolicLinks) {
         // Skip the creation of a temporary dictionary, not compatible with symbolic links
         $sTempTargetDir = $sFinalTargetDir;
     } else {
         // Create a temporary directory
         // Once the compilation is 100% successful, then move the results into the target directory
         $sTempTargetDir = tempnam(SetupUtils::GetTmpDir(), 'itop-');
         unlink($sTempTargetDir);
         // I need a directory, not a file...
         SetupUtils::builddir($sTempTargetDir);
         // Here is the directory
     }
     try {
         $this->DoCompile($sTempTargetDir, $sFinalTargetDir, $oP = null, $bUseSymbolicLinks);
     } catch (Exception $e) {
         if ($sTempTargetDir != $sFinalTargetDir) {
             // Cleanup the temporary directory
             SetupUtils::rrmdir($sTempTargetDir);
         }
         throw $e;
     }
     if ($sTempTargetDir != $sFinalTargetDir) {
         // Move the results to the target directory
         SetupUtils::movedir($sTempTargetDir, $sFinalTargetDir);
     }
 }
 public function RestoreFromZip($sZipFile, $sEnvironment = 'production')
 {
     $this->LogInfo("Starting restore of " . basename($sZipFile));
     $oZip = new ZipArchiveEx();
     $res = $oZip->open($sZipFile);
     // Load the database
     //
     $sDataDir = tempnam(SetupUtils::GetTmpDir(), 'itop-');
     unlink($sDataDir);
     // I need a directory, not a file...
     SetupUtils::builddir($sDataDir);
     // Here is the directory
     $oZip->extractTo($sDataDir, 'itop-dump.sql');
     $sDataFile = $sDataDir . '/itop-dump.sql';
     $this->LoadDatabase($sDataFile);
     unlink($sDataFile);
     // Update the code
     //
     $sDeltaFile = APPROOT . 'data/' . $sEnvironment . '.delta.xml';
     if ($oZip->locateName('delta.xml') !== false) {
         // Extract and rename delta.xml => <env>.delta.xml;
         file_put_contents($sDeltaFile, $oZip->getFromName('delta.xml'));
     } else {
         @unlink($sDeltaFile);
     }
     if (is_dir(APPROOT . 'data/production-modules/')) {
         SetupUtils::rrmdir(APPROOT . 'data/production-modules/');
     }
     if ($oZip->locateName('production-modules/') !== false) {
         $oZip->extractDirTo(APPROOT . 'data/', 'production-modules/');
     }
     $sConfigFile = APPROOT . 'conf/' . $sEnvironment . '/config-itop.php';
     @chmod($sConfigFile, 0770);
     // Allow overwriting the file
     $oZip->extractTo(APPROOT . 'conf/' . $sEnvironment, 'config-itop.php');
     @chmod($sConfigFile, 0444);
     // Read-only
     $oEnvironment = new RunTimeEnvironment($sEnvironment);
     $oEnvironment->CompileFrom($sEnvironment);
 }
 public function CreateZip($sZipFile, $sSourceConfigFile = null)
 {
     // Note: the file is created by tempnam and might not be writeable by another process (Windows/IIS)
     // (delete it before spawning a process)
     $sDataFile = tempnam(SetupUtils::GetTmpDir(), 'itop-');
     $this->LogInfo("Data file: '{$sDataFile}'");
     $aContents = array();
     $aContents[] = array('source' => $sDataFile, 'dest' => 'itop-dump.sql');
     if (is_null($sSourceConfigFile)) {
         $sSourceConfigFile = MetaModel::GetConfig()->GetLoadedFile();
     }
     if (!empty($sSourceConfigFile)) {
         $aContents[] = array('source' => $sSourceConfigFile, 'dest' => 'config-itop.php');
     }
     $this->DoBackup($sDataFile);
     $sDeltaFile = APPROOT . 'data/' . utils::GetCurrentEnvironment() . '.delta.xml';
     if (file_exists($sDeltaFile)) {
         $aContents[] = array('source' => $sDeltaFile, 'dest' => 'delta.xml');
     }
     $sExtraDir = APPROOT . 'data/' . utils::GetCurrentEnvironment() . '-modules/';
     if (is_dir($sExtraDir)) {
         $aContents[] = array('source' => $sExtraDir, 'dest' => utils::GetCurrentEnvironment() . '-modules/');
     }
     $this->DoZip($aContents, $sZipFile);
     // Windows/IIS: the data file has been created by the spawned process...
     //   trying to delete it will issue a warning, itself stopping the setup abruptely
     @unlink($sDataFile);
 }
Example #4
0
    }
} else {
    require_once APPROOT . 'application/loginwebpage.class.inc.php';
    LoginWebPage::DoLogin();
    // Check user rights and prompt if needed
    $bDownloadBackup = utils::ReadParam('download', false);
}
if (!UserRights::IsAdministrator()) {
    ExitError($oP, "Access restricted to administors");
}
if (CheckParam('?') || CheckParam('h') || CheckParam('help')) {
    Usage($oP);
    $oP->output();
    exit;
}
$sDefaultBackupFileName = SetupUtils::GetTmpDir() . '/' . "__DB__-%Y-%m-%d";
$sBackupFile = utils::ReadParam('backup_file', $sDefaultBackupFileName, true, 'raw_data');
// Interpret strftime specifications (like %Y) and database placeholders
$oBackup = new MyDBBackup($oP);
$oBackup->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', ''));
$sBackupFile = $oBackup->MakeName($sBackupFile);
$sZipArchiveFile = $sBackupFile . '.zip';
$bSimulate = utils::ReadParam('simulate', false, true);
$res = false;
if ($bSimulate) {
    $oP->p("Simulate: would create file '{$sZipArchiveFile}'");
} elseif (MetaModel::GetConfig()->Get('demo_mode')) {
    $oP->p("Sorry, iTop is in demonstration mode: the feature is disabled");
} else {
    $oBackup->CreateZip($sZipArchiveFile);
}