예제 #1
0
def backup_binlogs(run_type, config):  # pylint: disable=too-many-locals
    """Copy MySQL binlog files to the backup destination.

    :param run_type: Run type
    :type run_type: str
    :param config: Tool configuration
    :type config: TwinDBBackupConfig
    """
    if config.mysql is None:
        LOG.debug("No MySQL config, not copying binlogs")
        return

    dst = config.destination()
    status = BinlogStatus(dst=dst)
    mysql_client = MySQLClient(defaults_file=config.mysql.defaults_file)

    # last_copy = status.latest_backup
    LOG.debug("Latest copied binlog %s", status.latest_backup)
    with mysql_client.cursor() as cur:
        cur.execute("FLUSH BINARY LOGS")
        backup_set = binlogs_to_backup(
            cur,
            last_binlog=status.latest_backup.name
            if status.latest_backup else None,
        )
        cur.execute("SELECT @@log_bin_basename")
        row = cur.fetchone()
        if row["@@log_bin_basename"]:
            binlog_dir = osp.dirname(row["@@log_bin_basename"])
        else:
            return

    for binlog_name in backup_set:
        src = BinlogSource(run_type, mysql_client, binlog_name)
        binlog_copy = BinlogCopy(
            src.host,
            binlog_name,
            BinlogParser(osp.join(binlog_dir, binlog_name)).created_at,
        )
        _backup_stream(config, src, dst)
        status.add(binlog_copy)

    try:
        expire_log_days = config.mysql.expire_log_days
    except (configparser.NoSectionError, configparser.NoOptionError):
        expire_log_days = 7

    for copy in status:
        now = int(time.time())
        LOG.debug("Reviewing copy %s. Now: %d", copy, now)

        if copy.created_at < now - expire_log_days * 24 * 3600:
            LOG.debug(
                "Deleting copy that was taken %d seconds ago",
                now - copy.created_at,
            )
            dst.delete(copy.key + ".gz")
            status.remove(copy.key)

    status.save(dst)
예제 #2
0
def test_add_to_empty_status():
    instance = BinlogStatus()

    instance.add(BinlogCopy("host", "foo/bar", 100500))
    assert BinlogCopy("host", "foo/bar", 100500) in instance

    instance.add(BinlogCopy("host", "foo/bar-2", 100501))
    assert BinlogCopy("host", "foo/bar", 100500) in instance
    assert BinlogCopy("host", "foo/bar-2", 100501) in instance
예제 #3
0
파일: backup.py 프로젝트: twindb/backup
def backup_binlogs(run_type, config):  # pylint: disable=too-many-locals
    """Copy MySQL binlog files to the backup destination.

    :param run_type: Run type
    :type run_type: str
    :param config: Tool configuration
    :type config: TwinDBBackupConfig
    """
    if config.mysql is None:
        LOG.debug('No MySQL config, not copying binlogs')
        return

    dst = config.destination()
    status = BinlogStatus(dst=dst)
    mysql_client = MySQLClient(
        defaults_file=config.mysql.defaults_file
    )

    # last_copy = status.latest_backup
    LOG.debug('Latest copied binlog %s', status.latest_backup)
    with mysql_client.cursor() as cur:
        cur.execute("FLUSH BINARY LOGS")
        backup_set = binlogs_to_backup(
            cur,
            last_binlog=status.latest_backup.name
            if status.latest_backup else None
        )
        cur.execute("SELECT @@log_bin_basename")
        row = cur.fetchone()
        binlog_dir = osp.dirname(row['@@log_bin_basename'])

    for binlog_name in backup_set:
        src = BinlogSource(run_type, mysql_client, binlog_name)
        binlog_copy = BinlogCopy(
            src.host,
            binlog_name,
            BinlogParser(
                osp.join(
                    binlog_dir,
                    binlog_name
                )
            ).created_at
        )
        _backup_stream(config, src, dst)
        status.add(binlog_copy)

    try:
        expire_log_days = config.mysql.expire_log_days
    except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
        expire_log_days = 7

    for copy in status:
        now = int(time.time())
        LOG.debug('Reviewing copy %s. Now: %d', copy, now)

        if copy.created_at < now - expire_log_days * 24 * 3600:
            LOG.debug(
                'Deleting copy that was taken %d seconds ago',
                now - copy.created_at
            )
            dst.delete(copy.key + ".gz")
            status.remove(copy.key)

    status.save(dst)