Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * @Route("/client/{idClient}/job/{idJob}", requirements={"idClient" = "\d+", "idJob" = "\d+"}, defaults={"idJob" = "-1"}, name="saveJob")
  * @Method("POST")
  * @Template()
  */
 public function saveJobAction(Request $request, $idClient, $idJob)
 {
     $t = $this->get('translator');
     if ("-1" === $idJob) {
         $job = new Job();
         $client = $this->getDoctrine()->getRepository('BinovoElkarBackupBundle:Client')->find($idClient);
         if (null == $client) {
             throw $this->createNotFoundException($t->trans('Unable to find Client entity:', array(), 'BinovoElkarBackup') . $idClient);
         }
         $job->setClient($client);
     } else {
         $repository = $this->getDoctrine()->getRepository('BinovoElkarBackupBundle:Job');
         $job = $repository->find($idJob);
     }
     $storedOwner = $job->getOwner();
     $form = $this->createForm(new JobType(), $job, array('translator' => $t));
     $form->bind($request);
     if ($form->isValid()) {
         $job = $form->getData();
         if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
             // only allow chown to admin
             $job->setOwner($storedOwner);
         }
         if ($job->getOwner() == null) {
             $job->setOwner($this->get('security.context')->getToken()->getUser());
         }
         try {
             $em = $this->getDoctrine()->getManager();
             $em->persist($job);
             $this->info('Save client %clientid%, job %jobid%', array('%clientid%' => $job->getClient()->getId(), '%jobid%' => $job->getId()), array('link' => $this->generateJobRoute($job->getId(), $job->getClient()->getId())));
             $em->flush();
         } catch (Exception $e) {
             $this->get('session')->getFlashBag()->add('job', $t->trans('Unable to save your changes: %extrainfo%', array('%extrainfo%' => $e->getMessage()), 'BinovoElkarBackup'));
         }
         return $this->redirect($this->generateUrl('showClients'));
     } else {
         return $this->render('BinovoElkarBackupBundle:Default:job.html.twig', array('form' => $form->createView()));
     }
 }
Exemplo n.º 3
0
 protected function _getJobTahoePath(Job $job)
 {
     $idClient = $job->getClient()->getId();
     $idJob = $job->getId();
     return 'elkarbackup:Backups/' . sprintf('%04d', $idClient) . '/' . sprintf('%04d', $idJob) . '/';
 }