def test_time_no_format(): now_str_from_formatting = formatting.time_now() valid = False if now_str_from_formatting != '': valid = True assert valid
def test_time_valid_format(): now = datetime.datetime.now() now_str = now.strftime('%Y%m%d%H') now_str_from_formatting = formatting.time_now('YYYYMMDDHH') assert now_str == now_str_from_formatting
def do(): errcode = 0 # Check backup dir, if it does not exist, create it if not os.path.exists(cfg.BACKUP_DIR): os.mkdir(cfg.BACKUP_DIR) # Configure commands mysqlcliauth = "--host={host} --port={port} --user={user} --password={passw}" mysqlclidocker = "docker run --network=host -i {image}" mysqlcliauth = mysqlcliauth.format( host=cfg.MYSQL['HOST'], port=cfg.MYSQL['PORT'], user=cfg.MYSQL['USER'], passw=cfg.MYSQL['PASSW'], ) mysqlclicmd = "{program} {options} " + mysqlcliauth mysqldump = mysqlclicmd.format( program="mysqldump", options="--single-transaction --quick --lock-tables=false") mysql = mysqlclicmd.format(program="mysql", options="--silent --skip-column-names") if cfg.MYSQLCLI_USE_DOCKER: log.debug('using docker') mysqlclidocker = mysqlclidocker.format(image=cfg.MYSQLCLI_DOCKER_IMAGE) mysqldump = mysqlclidocker + " " + mysqldump mysql = mysqlclidocker + " " + mysql log.debug('mysqlcliauth: {}'.format(mysqlcliauth)) log.debug('mysqldump: {}'.format(mysqldump)) log.debug('mysql: {}'.format(mysql)) # Get a list of databases log.info('getting a list of databases') cmd = mysql + " --execute='show databases'" log.debug("cmd: {}".format(cmd)) out = gpmos.run(cmd) log.debug('returncode: {}'.format(out['returncode'])) # Was the execution successful? if out['returncode'] > 0: errcode = 1 log.error('error getting list of databases') log.error('stderr: {}'.format(out['stderr'])) return errcode log.debug('stdout: {}'.format(out['stdout'])) databases = out['stdout'].splitlines() log.info('found {} databases'.format(len(databases))) log.info(databases) if len(databases) > 0: for database in databases: if database not in cfg.EXCLUDE and (database in cfg.INCLUDE or cfg.INCLUDE[0] == '*'): ts = formatting.time_now(cfg.BACKUP_FILE_TSFORMAT) backup_file = os.path.join(cfg.BACKUP_DIR, database + "_" + ts + ".sql.gz") log.debug("backup file name {}".format(backup_file)) cmd = mysqldump + " --databases " + database log.debug("cmd: {}".format(cmd)) out = gpmos.run(cmd) if out['returncode'] > 1: log.error('error backing up database {}'.format(database)) log.error('stderr: {}'.format(out['stderr'])) return errcode log.debug('writing to backup file {}'.format(backup_file)) with gzip.open(backup_file, "wb") as f: f.write(out['stdout'].encode('utf-8')) log.info('successfully backed up database {} to {}'.format( database, backup_file)) else: log.info('excluding {}'.format(database)) else: log.error('no databases found') errcode = 1 return errcode
def test_time_invalid_format(): with pytest.raises(Exception): formatting.time_now('invalid_format')
def __init__(self, script=None, tsformat=None, keyword=None, log_level=None, logger_name=None, rotating=False, console=True, log_entry_format_separator=None): if tsformat is None: tsformat = c.log_file_default_tsformat if log_level is None: log_level = c.log_level if log_entry_format_separator is None: log_entry_format_separator = c.log_entry_format_separator # Define logger logger = get_logger(logger_name) logger.setLevel(log_level) # Define log format c.log_entry_format = c.log_entry_format.format( separator=log_entry_format_separator) formatter = logging.Formatter(c.log_entry_format, datefmt=c.log_date_format) # Check if handlers are already added. If not add them # This avoids duplicate messages if not logger.handlers: # Log to Console by default unless otherwise defined to not if console: console = logging.StreamHandler() console.setLevel(log_level) console.setFormatter(formatter) logger.addHandler(console) # If Script specified then also do file based if script is not None: ts = '_' + formatting.time_now(tsformat) # Extract Script ID which is equal to ScriptY for ScriptY.py script_id = os.path.splitext(os.path.basename(script))[0] # Define log dir log_dir = os.path.join( os.path.dirname(os.path.abspath(script)), 'log', script_id) # Check if the folder exists and create if necessary if not os.path.exists(log_dir): os.makedirs(log_dir) # This optional keyword gets appended to the file name # after the timestamp if keyword: # Strip characters that OS won't allow keyword = re.sub('[/:?"|*]', '', keyword) keyword = '_' + keyword else: keyword = '' if rotating: # Difference in the name is that there will be no # timestamp for rotating logs log_file = script_id + keyword + '.log' log_file_path = os.path.join(log_dir, log_file) file = logging.handlers.RotatingFileHandler( log_file_path, maxBytes=c.log_maxbytes, backupCount=c.log_backupcount) else: log_file = script_id + ts + keyword + '.log' log_file_path = os.path.join(log_dir, log_file) file = logging.FileHandler(log_file_path) file.setLevel(log_level) file.setFormatter(formatter) logger.addHandler(file) # Assign path variables self._log_dir = log_dir self._log_file = log_file self._log_file_path = log_file_path # Assign logger values to self and global log object self._logger = logger