def main(config: CONFIGTYPE) -> COMMANDLISTTYPE: """Command builder. :param config: config snippet for this plugin :returns: Commands to create the backup """ command = ['tar', '-vcp'] # parse compress level out of destination filename extension = os.path.splitext(config['destination'])[1] if extension == '.bz2': command.append('-j') elif extension == '.gz': command.append('-z') elif extension == '.xz': command.append('-J') else: pass # add '-f' to command list for storing the tar in a file command.append('-f') # add destination command.append(absolutenormpath(config['destination'])) # add dirs that get into the tarball for i in config['source']: command.append(absolutenormpath(i)) return [command]
def main(config): """Command builder. :param config: config snippet for this plugin :type config: dict :returns: Commands to create the backup :rtype: list """ commands = [] destination = config['destination'] # git init if its not a repo yet if config['mode'] == 'git': cloned_yet = utils.git_cloned_yet(destination) if not cloned_yet: commands.append(['cd', destination, '&&', 'git', 'init']) # slapcat command commands.append([ 'slapcat', '-l', join(utils.absolutenormpath(destination), 'backup.ldif') ]) # commit if git mode is used if config['mode'] == 'git': if not cloned_yet or utils.git_something_to_commit(destination): commands.append([ 'cd', destination, '&&', 'git', 'add', 'backup.ldif', '&&', 'git', 'commit', '-m', '"new export"' ]) return commands
def main(config): """Command builder. :param config: config snippet for this plugin :type config: dict :returns: Commands to create the backup :rtype: list """ commands = [] destination = config['destination'] # git init if its not a repo yet if config['mode'] == 'git': cloned_yet = utils.git_cloned_yet(destination) if not cloned_yet: commands.append(['cd', destination, '&&', 'git', 'init']) # slapcat command commands.append(['slapcat', '-l', join(utils.absolutenormpath(destination), 'backup.ldif')]) # commit if git mode is used if config['mode'] == 'git': if not cloned_yet or utils.git_something_to_commit(destination): commands.append( ['cd', destination, '&&', 'git', 'add', 'backup.ldif', '&&', 'git', 'commit', '-m', '"new export"']) return commands
def main(config: CONFIGTYPE) -> COMMANDLISTTYPE: """Command builder. :param config: config snippet for this plugin :returns: Commands to create the backup """ commands = [] destination = config['destination'] # if commiting every dump to a git repo it has to init first if # its not there if config['mode'] == 'git': cloned_yet = utils.git_cloned_yet(destination) if not cloned_yet: commands.append(['cd', destination, '&&', 'git', 'init']) # mysqldump command commands.append( [ 'mysqldump', '--skip-extended-insert', '--skip-comments', '--user={}'.format(config['username']), '--password={}'.format(config['password']), '--host={}'.format(config['server']), config['database'], '>', path.join( utils.absolutenormpath(destination), '{}.sql'.format(config['database']) ) ] ) # commit if git mode is used if config['mode'] == 'git': # only commit if there is something to be commited if not cloned_yet or utils.git_something_to_commit(destination): commands.append( [ 'cd', destination, '&&', 'git', 'add', '{}.sql'.format(config['database']), '&&', 'git', 'commit', '-m', '"new dump"' ] ) return commands
def main(config): """Command builder. :param config: config snippet for this plugin :type config: dict :returns: Commands to create the backup :rtype: list """ command = ['tar', '-vcp'] # parse compress level out of destination filename extension = os.path.splitext(config['destination'])[1] if extension == '.bz2': command.append('-j') elif extension == '.gz': command.append('-z') elif extension == '.xz': command.append('-J') else: pass # add '-f' to command list for storing the tar in a file command.append('-f') # add destination command.append(absolutenormpath(config['destination'])) # add dirs that get into the tarball for i in config['source']: command.append(absolutenormpath(i)) return [command]
def main(config): """Command builder. :param config: config snippet for this plugin :type config: dict :returns: Commands to create the backup :rtype: list """ commands = [] destination = config['destination'] # if commiting every dump to a git repo it has to init first if # its not there if config['mode'] == 'git': cloned_yet = utils.git_cloned_yet(destination) if not cloned_yet: commands.append(['cd', destination, '&&', 'git', 'init']) # mysqldump command commands.append(['mysqldump', '--skip-extended-insert', '--skip-comments', '--user={}'.format(config['username']), '--password={}'.format(config['password']), '--host={}'.format(config['server']), config['database'], '>', path.join( utils.absolutenormpath(destination), '{}.sql'.format(config['database']))]) # commit if git mode is used if config['mode'] == 'git': # only commit if there is something to be commited if not cloned_yet or utils.git_something_to_commit(destination): commands.append(['cd', destination, '&&', 'git', 'add', '{}.sql'.format(config['database']), '&&', 'git', 'commit', '-m', '"new dump"']) return commands
def test_absolutenormpath(input, expected): utils.absolutenormpath(input) == expected
def run_commands( commands: Dict[str, List[List[str]]], test: bool, log_dir: str, log_keep: int ) -> None: """Running the commands. The actual runner. It will take the commands dictionary and run it one after another. There is also a test key. With this enabled it will only print the commands it would run. :param commands: Commands dictionary :param test: If test the commands only will be printed :param log_dir: Dictionary for logfiles :param log_keep: How many logs to keep from one job """ # in test mode it will print all the commands it would run for # each item in the config if test: print_commands(commands) else: # list to store all return codes of all sub commands all_return_codes = [] # type: List[int] for item in commands.items(): LOG.debug('item: %s', item) name, command_list = item LOG.info('started item %s', name) # collects the return codes of all sub commands return_codes = [] # define logger for stdout logging logger = Logger( utils.absolutenormpath(log_dir), name, log_keep ) # be sure that there is the log dir logger.create_log_dir() # roate logfiles logger.rotate() # run through commands starting_time = datetime.datetime.now() for command_item in command_list: # create process command = ' '.join(command_item) LOG.debug('command: %s', command) proc = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True ) LOG.debug('run command') # write logfile LOG.debug('write to logfile') with logger.logfile() as logfile: for line in proc.stdout: dec_line = line.decode('utf-8', 'replace') LOG.debug(dec_line) logfile.write(dec_line) proc.wait() LOG.debug('done writing logfile') LOG.debug('done with command') # store returncode returncode = proc.returncode LOG.debug('returncode: %s', returncode) return_codes.append(returncode) # get exit code code = return_code(return_codes) # store it in the return code list for all sub commands all_return_codes.append(code) LOG.debug('exitcode: %s', code) LOG.debug('write metadata') with logger.logfile() as logfile: finishing_time = datetime.datetime.now() logfile.write( 'Finished at: {}\n'.format( finishing_time.strftime("%Y-%m-%d %H:%M") ) ) logfile.write( 'Total runtime: {} seconds.\n'.format( (finishing_time - starting_time).total_seconds()) ) logfile.write('Exit code: {}\n'.format(code)) LOG.debug('metadata done') LOG.info('done with item %s', name) # get overall return code and exit with it sys.exit(return_code(all_return_codes))
def run_commands(commands, test, log_dir, log_keep): """Running the commands. The actual runner. It will take the commands dictionary and run it one after another. There is also a test key. With this enabled it will only print the commands it would run. :param commands: Commands dictionary :param test: If test the commands only will be printed :param log_dir: Dictionary for logfiles :param log_keep: How many logs to keep from one job :type commands: dict :type test: bool :type log_dir: str :type log_keep: int """ # in test mode it will print all the commands it would run for # each item in the config if test: print_commands(commands) else: for item in commands.items(): log.debug('item: {}'.format(item)) name, command_list = item log.info('started item {}'.format(name)) # collects the return codes of all sub commands return_codes = [] # define logger for stdout logging logger = Logger(utils.absolutenormpath(log_dir), name, log_keep) # be sure that there is the log dir logger.create_log_dir() # roate logfiles logger.rotate() # run through commands starting_time = datetime.datetime.now() for command in command_list: # create process command = ' '.join(command) log.debug('command: {}'.format(command)) proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) log.debug('done with command') # write logfile log.debug('write to logfile') with logger.logfile() as logfile: for line in proc.stdout: logfile.write(line.decode('utf-8', 'replace')) proc.wait() log.debug('done writing logfile') # store returncode returncode = proc.returncode log.debug('returncode: {}'.format(returncode)) return_codes.append(returncode) # write exit code code = 0 for exitcode in return_codes: if exitcode != 0: code = 1 log.debug('exitcode: {}'.format(code)) log.debug('write metadata') with logger.logfile() as logfile: finishing_time = datetime.datetime.now() logfile.write('Finished at: {}\n'.format( finishing_time.strftime("%Y-%m-%d %H:%M"))) logfile.write('Total runtime: {} seconds.\n'.format( (finishing_time - starting_time).total_seconds())) logfile.write('Exit code: {}\n'.format(code)) log.debug('metadata done') log.info('done with item {}'.format(name))