Ejemplo n.º 1
0
def get_active_schedule(min_time=None, max_time=None, cur=None):
    sql = ("select * from xxx")
    result = session.execute(sql)
    session.commit()
    items = []
    for item in result:
        next_run_time = item[9]
        if not next_run_time:
            items.append(item)
        elif next_run_time <= cur:
            items.append(item)
    return items
Ejemplo n.º 2
0
def get_host(instance_name, db_type, db_role=None):
    if db_type == 'DBT:MG':
        sql = ("select host from ccm_host where instance_name = {}".format(
            instance_name))
    elif db_type == 'DBT:P' or db_type == 'DBT:MY':
        sql = ("select host from ccm_host where"
               " instance_name = {} and db_role = {}".format(instance_name,
                                                             db_role))
    else:
        raise ParamError('error')
    result = session.execute(sql).fetchall()
    session.commit()
    host = ''
    if result:
        host = result[0][0]
    return host
Ejemplo n.º 3
0
def get_one_schedule(entity_name):
    sql = ("select * from xxx where entity_name = {}".format(entity_name))
    result = session.execute(sql).fetchall()
    session.commit()
    items = []
    for item in result:
        if item[5]:
            last_bkfile_mtime = datetime_to_datetime_str(item[5])
        if item[6]:
            next_run_time = datetime_to_datetime_str(item[6])
        schedule = dict(id_st_bk_dblog_schedule=item[0],
                        id_st_backup_policy=item[1],
                        entity_name=item[2], logbk_interval_min=item[3],
                        schedule_status=item[4],
                        last_bkfile_mtime=last_bkfile_mtime,
                        next_run_time=next_run_time
                        )
        items.append(schedule)
    return items
Ejemplo n.º 4
0
def filter_schedule_and_host(items):
    new_items = []
    tmp_instance_name = set()
    for item in items:
        db_type, entity_name = item[2], item[3]
        instance_name, instance_id = item[4], item[0]
        db_role = item[6]

        if instance_name in tmp_instance_name:
            continue

        if db_type == 'DBT:P':
            sql = (
                "select * in xxx1 where entity like '%{}%'".format(entity_name))
        elif db_type == 'DBT:MY':
            sql = (
                "select * in xxx2 where entity like '%{}%'".format(entity_name))
        elif db_type == 'DBT:MG':
            sql = (
                "select * in xxx3 where entity like '%{}%'".format(entity_name))
        else:
            continue

        result = session.execute(sql).fetchall()
        session.commit()

        if not result:
            continue

        try:
            host = get_host(instance_name, db_type, db_role)
        except Exception:
            continue

        if len(result[0]) >= 2:
            if result[0][1] == instance_id:
                new_items.append(list(item) + [host])
                tmp_instance_name.add(instance_name)
        else:
            if instance_name == result[0][0]:
                new_items.append(list(item) + [host])
    return new_items
Ejemplo n.º 5
0
def task_backup_log(files, port, db_type, instance_name, timestamp,
                    mysql_info=None, host=None, schedule_id=None,
                    task_name=None):
    if not files.get('files'):
        logger.error('not files error')
        return None
    try:
        info = get_base_info(instance_name, db_type, timestamp, port,
                             mysql_info)

    except Exception:
        logger.error('get base info error')
        return None

    start = files.get('files')[0].get('file')
    end = files.get('files')[-1].get('file')
    backup_host = host or files.get('host')
    source_path, target_path = info['source_path'], info['target_path']
    user, identity = info.get('user'), info.get('identity')
    record_items = []
    cur_time = get_datetime()

    for item in files.get('file'):
        file_name, size_mb = item.get('file'), int(item.get('size_mb'))
        file_mtime = time_str_to_datetime(item.get('modify_time'))
        source_file = os.path.join(source_path, file_name)
        target_file = target_path
        data = dict(backup_status='execute',
                    backup_start_time=cur_time, task_name=task_name,
                    size_mb=size_mb, file_create_time=file_mtime,
                    source_file=source_file, target_file=target_file)
        if schedule_id:
            data = dict(id_st_dblog_schedule=schedule_id,
                        backup_start_time=cur_time, backup_status='execute',
                        size_mb=size_mb, file_create_time=file_mtime,
                        source_file=source_file, target_file=target_file)
        record_items.append(LogBackupRecord ** data)
    session.add_all(record_items)

    session.commit()
    record_ids = [record.id_st_bk_dblog_set for record in record_items]
    if schedule_id:
        last_bkfile_mtime = files.get('files')[-1].get('modify_time')
        update_last_bkfile_mtime(schedule_id, last_bkfile_mtime)
    logger.info('backup start')

    @retry(3, 4)
    def _backup(source_path, target_path, user, identity, start, end, host):
        fs = FsClient(host)
        fs.backup_log(source_path, target_path, user, identity, start, end)

    try:
        _backup(source_path, target_path, user, identity, start, end,
                backup_host)
    except Exception:
        logger.error('backup error')
        cur_time = get_datetime()
        for record_id in record_ids:
            data = dict(backup_status='fail', backup_end_time=cur_time)
            update_record(record_id, data)
        session.commit()
        return None

    cur_time = get_datetime()
    for record_id in record_ids:
        data = dict(backup_status='success', backup_end_time=cur_time)
        update_record(record_id, data)
    session.commit()
Ejemplo n.º 6
0
def get_all_schedule():
    sql = ("select * from xxx")
    result = session.execute(sql)
    session.commit()
    return result
Ejemplo n.º 7
0
def insert_schedule(params):
    new_policy = LogSchedule(**params)
    session.add(new_policy)
    session.commit()
    return new_policy.id_st_bk_dblog_schedule
Ejemplo n.º 8
0
def update_interval(id_db_backup_policy, logbk_interval_min):
    sql = ("update xxx set logbk_interval_min = "
           "{0} where id_db_backup_policy = {1}".format(logbk_interval_min,
                                                        id_db_backup_policy))
    session.execute(sql)
    session.commit()
Ejemplo n.º 9
0
def update_schedule(params):
    id_st_bk_dblog_schedule = params.pop('id_st_bk_dblog_schedule')
    session.query(LogSchedule).filter(
        LogSchedule.id_st_bk_dblog_schedule == id_st_bk_dblog_schedule
    ).update(params)
    session.commit()