Exemplo n.º 1
0
 protected function runJob(Job $job, $runnableRetains)
 {
     $stats[] = array();
     $warnings = False;
     $container = $this->getContainer();
     $backupDir = $container->getParameter('backup_dir');
     $rsnapshot = $container->getParameter('rsnapshot');
     $tmpDir = $container->getParameter('tmp_dir');
     $engine = $container->get('templating');
     $idClient = $job->getClient()->getId();
     $idJob = $job->getId();
     $url = $job->getUrl();
     $retains = $job->getPolicy()->getRetains();
     $includes = array();
     $include = $job->getInclude();
     if ($include) {
         $includes = explode("\n", $include);
         foreach ($includes as &$theInclude) {
             $theInclude = str_replace('\\ ', '?', trim($theInclude));
         }
     }
     $excludes = array();
     $exclude = $job->getExclude();
     if ($exclude) {
         $excludes = explode("\n", $exclude);
         foreach ($excludes as &$theExclude) {
             $theExclude = str_replace('\\ ', '?', trim($theExclude));
         }
     }
     $syncFirst = (int) $job->getPolicy()->getSyncFirst();
     $context = array('link' => $this->generateJobRoute($idJob, $idClient));
     $content = $engine->render('BinovoElkarBackupBundle:Default:rsnapshotconfig.txt.twig', array('cmdPreExec' => '', 'cmdPostExec' => '', 'excludes' => $excludes, 'idClient' => sprintf('%04d', $idClient), 'idJob' => sprintf('%04d', $idJob), 'includes' => $includes, 'backupDir' => $backupDir, 'retains' => $retains, 'tmp' => $tmpDir, 'snapshotRoot' => $job->getSnapshotRoot(), 'syncFirst' => $syncFirst, 'url' => $url, 'useLocalPermissions' => $job->getUseLocalPermissions(), 'sshArgs' => $job->getSshArgs()));
     $confFileName = sprintf("%s/rsnapshot.%s_%s.cfg", $tmpDir, $idClient, $idJob);
     $fd = fopen($confFileName, 'w');
     if (false === $fd) {
         $this->err('Error opening config file %filename%. Aborting backup.', array('%filename%' => $confFileName), $context);
         return false;
     }
     $bytesWriten = fwrite($fd, $content);
     if (false === $bytesWriten) {
         $this->err('Error writing to config file %filename%. Aborting backup.', array('%filename%' => $confFileName), $context);
         return false;
     }
     $ok = fclose($fd);
     if (false === $ok) {
         $this->warn('Error closing config file %filename%.', array('%filename%' => $confFileName), $context);
     }
     if (!is_dir($job->getSnapshotRoot())) {
         $ok = mkdir($job->getSnapshotRoot(), 0777, true);
         if (false === $ok) {
             $this->err('Error creating snapshot root %filename%. Aborting backup.', array('%filename%' => $job->getSnapshotRoot()), $context);
             return false;
         }
     }
     foreach ($runnableRetains as $retain) {
         $status = 0;
         // pre script execution if needed
         $mustRunScripts = !$job->getPolicy()->isRotation($retain);
         if ($mustRunScripts) {
             foreach ($job->getPreScripts() as $script) {
                 if ($this->runScript('pre', 0, $job->getClient(), $job, $script, $stats)) {
                     $this->info('Job "%jobid%" pre script ok.', array('%jobid%' => $job->getId()), $context);
                 } else {
                     $this->err('Job "%jobid%" pre script error.', array('%jobid%' => $job->getId()), $context);
                     $warnings = True;
                 }
             }
         }
         $job_starttime = time();
         // run rsnapshot. sync first if needed
         $commands = array();
         if ($job->getPolicy()->mustSync($retain)) {
             $commands[] = sprintf('"%s" -c "%s" sync 2>&1', $rsnapshot, $confFileName);
         }
         $commands[] = sprintf('"%s" -c "%s" %s 2>&1', $rsnapshot, $confFileName, $retain);
         foreach ($commands as $command) {
             $commandOutput = array();
             $status = 0;
             $this->info('Running %command%', array('%command%' => $command), $context);
             exec($command, $commandOutput, $status);
             if (0 != $status) {
                 $this->err('Command %command% failed. Diagnostic information follows: %output%', array('%command%' => $command, '%output%' => "\n" . implode("\n", $commandOutput)), $context);
                 $ok = false;
                 break;
             } else {
                 $this->info('Command %command% succeeded with output: %output%', array('%command%' => $command, '%output%' => implode("\n", $commandOutput)), $context);
             }
         }
         $job_endtime = time();
         // get disk usage
         $du_before = $job->getDiskUsage();
         $this->info('Client "%clientid%", Job "%jobid%" du begin.', array('%clientid%' => $job->getClient()->getId(), '%jobid%' => $job->getId()), $context);
         $du = (int) shell_exec(sprintf("du -ks '%s' | sed 's/\t.*//'", $job->getSnapshotRoot()));
         $job->setDiskUsage($du);
         $this->info('Client "%clientid%", Job "%jobid%" du end.', array('%clientid%' => $job->getClient()->getId(), '%jobid%' => $job->getId()), $context);
         $job_run_size = $du - $du_before;
         // post script execution if needed
         if ($mustRunScripts) {
             foreach ($job->getPostScripts() as $script) {
                 $stats['ELKARBACKUP_JOB_RUN_SIZE'] = $job_run_size;
                 $stats['ELKARBACKUP_JOB_STARTTIME'] = $job_starttime;
                 $stats['ELKARBACKUP_JOB_ENDTIME'] = $job_endtime;
                 if ($this->runScript('post', $status, $job->getClient(), $job, $script, $stats)) {
                     $this->info('Job "%jobid%" post script ok.', array('%jobid%' => $job->getId()), $context);
                 } else {
                     $this->err('Job "%jobid%" post script error.', array('%jobid%' => $job->getId()), $context);
                     $warnings = True;
                 }
             }
         }
         //tahoe backup
         $tahoe = $container->get('Tahoe');
         $tahoeInstalled = $tahoe->isInstalled();
         $tahoeOn = $container->getParameter('tahoe_active');
         if ($tahoeInstalled && $tahoeOn) {
             $tahoe->enqueueJob($job, $retain);
         }
     }
     if (false === unlink($confFileName)) {
         $this->warn('Error unlinking config file %filename%.', array('%filename%' => $confFileName), $context);
     }
     if (True === $ok) {
         if (True === $warnings) {
             $ok = 2;
         }
     }
     return $ok;
 }