/** * Runs a client level pre or post script. * * @param string $type Either "pre" or "post". * * @param int $status Status of previos command. * * @param Client $client Client entity * * @param Job $job Job entity. Null if running at the client level. * * @param Script $script Script entity * * @param string $stats Stats for script environment vars (not stored in DB) * * @return boolean true on success, false on error. * */ protected function runScript($type, $status, $client, $job, $script, $stats) { if ($script === null) { return true; } if (null == $job) { $entity = $client; $context = array('link' => $this->generateClientRoute($client->getId())); $errScriptError = 'Client "%entityid%" %scripttype% script "%scriptname%" execution failed. Diagnostic information follows: %output%'; $errScriptMissing = 'Client "%entityid%" %scripttype% script "%scriptname%" present but file "%scriptfile%" missing.'; $errScriptOk = 'Client "%entityid%" %scripttype% script "%scriptname%" execution succeeded. Output follows: %output%'; $level = 'CLIENT'; // Empty vars (only available under JOB level) $job_name = ''; $owner_email = ''; $recipient_list = ''; $job_total_size = 0; $job_run_size = 0; $job_starttime = 0; $job_endtime = 0; if ($type == 'post') { $client_endtime = $stats['ELKARBACKUP_CLIENT_ENDTIME']; $client_starttime = $stats['ELKARBACKUP_CLIENT_STARTTIME']; } else { $client_endtime = 0; $client_starttime = 0; } } else { $entity = $job; $context = array('link' => $this->generateJobRoute($job->getId(), $client->getId())); $errScriptError = 'Job "%entityid%" %scripttype% script "%scriptname%" execution failed. Diagnostic information follows: %output%'; $errScriptMissing = 'Job "%entityid%" %scripttype% script "%scriptname%" present but file "%scriptfile%" missing.'; $errScriptOk = 'Job "%entityid%" %scripttype% script "%scriptname%" execution succeeded. Output follows: %output%'; $level = 'JOB'; $job_name = $job->getName(); $owner_email = $job->getOwner()->getEmail(); $recipient_list = $job->getNotificationsEmail(); $job_total_size = $job->getDiskUsage(); $client_starttime = 0; $client_endtime = 0; if ($type == 'post') { $job_run_size = $stats['ELKARBACKUP_JOB_RUN_SIZE']; $job_starttime = $stats['ELKARBACKUP_JOB_STARTTIME']; $job_endtime = $stats['ELKARBACKUP_JOB_ENDTIME']; } else { $job_run_size = 0; $job_starttime = 0; $job_endtime = 0; } } $scriptName = $script->getName(); $scriptFile = $script->getScriptPath(); if (!file_exists($scriptFile)) { $this->err($errScriptMissing, array('%entityid%' => $entity->getId(), '%scriptfile%' => $scriptFile, '%scriptname%' => $scriptName, '%scripttype%' => $type), $context); return false; } $commandOutput = array(); $command = sprintf('env ELKARBACKUP_LEVEL="%s" ELKARBACKUP_EVENT="%s" ELKARBACKUP_URL="%s" ELKARBACKUP_ID="%s" ELKARBACKUP_PATH="%s" ELKARBACKUP_STATUS="%s" ELKARBACKUP_CLIENT_NAME="%s" ELKARBACKUP_JOB_NAME="%s" ELKARBACKUP_OWNER_EMAIL="%s" ELKARBACKUP_RECIPIENT_LIST="%s" ELKARBACKUP_CLIENT_TOTAL_SIZE="%s" ELKARBACKUP_JOB_TOTAL_SIZE="%s" ELKARBACKUP_JOB_RUN_SIZE="%s" ELKARBACKUP_CLIENT_STARTTIME="%s" ELKARBACKUP_CLIENT_ENDTIME="%s" ELKARBACKUP_JOB_STARTTIME="%s" ELKARBACKUP_JOB_ENDTIME="%s" sudo "%s" 2>&1', $level, 'pre' == $type ? 'PRE' : 'POST', $entity->getUrl(), $entity->getId(), $entity->getSnapshotRoot(), $status, $client->getName(), $job_name, $owner_email, $recipient_list, $client->getDiskUsage(), $job_total_size, $job_run_size, $client_starttime, $client_endtime, $job_starttime, $job_endtime, $scriptFile); exec($command, $commandOutput, $status); if (0 != $status) { $this->err($errScriptError, array('%entityid%' => $entity->getId(), '%output%' => "\n" . implode("\n", $commandOutput), '%scriptname%' => $scriptName, '%scripttype%' => $type), $context); return false; } $this->info($errScriptOk, array('%entityid%' => $entity->getId(), '%output%' => "\n" . implode("\n", $commandOutput), '%scriptname%' => $scriptName, '%scripttype%' => $type), $context); return true; }