def main(argv=None): """Main block for backup script. @param argv: Command line arguments to script. By default the arguments are obtained automatically from the command line. This variable exists only for testing purposes. @return: Integer return code for process. """ try: (opts, jobs) = parseCmdline(argv) jobmgr = JobManager(opts, jobs) jobmgr.run() except errors.BackupError, e: if e.fatal: level = logging.CRITICAL else: level = logging.ERROR logger.log(level, e.desc) for line in e: logger.log(level, " %s" , line) if e.trace: raise elif e.fatal: return 1
def runJobs(self): """Runs the requested backup jobs. Backup jobs are either explicitly listed on the command line or all active backup jobs in configuration file are run. """ dry_run = self._globalConf.get('dry_run', False) for job_name in self._jobs: self._numJobs += 1 logmgr.setContext(job_name) job_conf = self._jobsConf.get(job_name) if job_conf is not None: active = parse_value(job_conf.get('active', 'yes'), True) if active: job_pre_exec = job_conf.get('job_pre_exec') job_post_exec = job_conf.get('job_post_exec') if job_pre_exec is not None: logger.info("Executing job pre-execution script.") try: execExternalCmd(job_pre_exec.split(), None, dry_run) job_pre_exec_ok = True except errors.ExternalCmdError, e: job_pre_exec_ok = False job_ok = False logger.error("Job pre-execution script failed.") logger.error(e.desc) for line in e: logger.error(" %s" , line) else: job_pre_exec_ok = True if job_pre_exec_ok: try: logger.info("Starting execution of backup job.") job = BackupJob(job_name, self._globalConf, job_conf) job.run() logger.info("Finished execution of backup job.") job_ok = True except errors.BackupError, e: logger.error("Execution of backup job failed.") job_ok = False if e.trace or e.fatal: raise else: if e.fatal: level = logging.CRITICAL else: level = logging.ERROR logger.log(level, e.desc) for line in e: logger.log(level, " %s" , line) if job_post_exec is not None and job_pre_exec_ok: logger.info("Executing job post-execution script.") try: execExternalCmd(job_post_exec.split(), None, dry_run) except errors.ExternalCmdError, e: job_ok = False logger.error("Job pre-execution script failed.") logger.error(e.desc) for line in e: logger.error(" %s" , line) if job_ok: self._numJobsSuccess += 1 else: self._numJobsError += 1