示例#1
0
def get_s3_backup(instance, date, backup_type):
    """ Find xbstream file for an instance on s3 on a given day

    Args:
    instance - A hostaddr object for the desired instance
    date - Desired date of restore file
    backup_type - xbstream or mysqldump

    Returns:
    A list of s3 keys
    """
    backup_keys = list()
    prefixes = set()
    try:
        replica_set = instance.get_zk_replica_set()[0]
    except:
        log.debug('Instance {} is not in zk'.format(instance))
        replica_set = None

    if replica_set:
        prefixes.add(BACKUP_SEARCH_PREFIX.format(
                         retention_policy=environment_specific.get_backup_retention_policy(instance),
                         backup_type=backup_type,
                         replica_set=replica_set,
                         hostname=instance.hostname,
                         port=instance.port,
                         timestamp=date))

    prefixes.add(BACKUP_SEARCH_INITIAL_PREFIX.format(
                     backup_type=backup_type,
                     hostname=instance.hostname,
                     port=instance.port,
                     timestamp=date))

    conn = boto.connect_s3()
    for bucket in environment_specific.BACKUP_BUCKET_DOWNLOAD_MAP[host_utils.get_iam_role()]:
        bucket_conn = conn.get_bucket(bucket, validate=False)
        for prefix in prefixes:
            log.info('Looking for backup with prefix '
                     's3://{bucket}/{prefix}'.format(bucket=bucket,
                                                     prefix=prefix))
            bucket_items = bucket_conn.list(prefix=prefix)
            for key in bucket_items:
                if (key.size <= MINIMUM_VALID_BACKUP_SIZE_BYTES):
                    continue

                backup_keys.append(key)

    if not backup_keys:
        msg = ''.join([NO_BACKUP, instance.__str__()])
        raise Exception(msg)
    return backup_keys
示例#2
0
def create_backup_file_name(instance, timestamp, initial_build, backup_type):
    """ Figure out where to put a backup in s3

    Args:
    instance - A hostaddr instance
    timestamp - A timestamp which will be used to create the backup filename
    initial_build - Boolean, if this is being created right after the server
                    was built
    backup_type - xtrabackup or mysqldump

    Returns:
    A string of the path to the finished backup
    """
    timestamp_formatted = time.strftime('%Y-%m-%d-%H:%M:%S', timestamp)
    if backup_type in (BACKUP_TYPE_LOGICAL, BACKUP_TYPE_PARTIAL_LOGICAL):
        extension = BACKUP_TYPE_LOGICAL_EXTENSION
    elif backup_type == BACKUP_TYPE_XBSTREAM:
        extension = BACKUP_TYPE_XBSTREAM_EXTENSION
    else:
        raise Exception('Unsupported backup type {}'.format(backup_type))

    try:
        replica_set = instance.guess_zk_replica_set()
    except:
        log.info('Looks like the db is not in zk, going to initial build '
                 'naming conventions for a backup')
        initial_build = True

    if initial_build:
        return BACKUP_FILE_INITIAL.format(
            backup_type=backup_type,
            hostname=instance.hostname,
            port=instance.port,
            timestamp=timestamp_formatted,
            extension=extension)
    else:
        return BACKUP_FILE.format(
             retention_policy=environment_specific.get_backup_retention_policy(
                    instance),
             backup_type=backup_type,
             replica_set=replica_set,
             hostname=instance.hostname,
             port=instance.port,
             timestamp=timestamp_formatted,
             extension=extension)
示例#3
0
def create_backup_file_name(instance, timestamp, initial_build, backup_type):
    """ Figure out where to put a backup in s3

    Args:
    instance - A hostaddr instance
    timestamp - A timestamp which will be used to create the backup filename
    initial_build - Boolean, if this is being created right after the server
                    was built
    backup_type - xtrabackup or mysqldump

    Returns:
    A string of the path to the finished backup
    """
    timestamp_formatted = time.strftime('%Y-%m-%d-%H:%M:%S', timestamp)
    if backup_type == BACKUP_TYPE_LOGICAL:
        extension = BACKUP_TYPE_LOGICAL_EXTENSION
    elif backup_type == BACKUP_TYPE_XBSTREAM:
        extension = BACKUP_TYPE_XBSTREAM_EXTENSION
    else:
        raise Exception('Unsupported backup type {}'.format(backup_type))

    if initial_build:
        return BACKUP_FILE_INITIAL.format(
            backup_type=backup_type,
            hostname=instance.hostname,
            port=instance.port,
            timestamp=timestamp_formatted,
            extension=extension)
    else:
        return BACKUP_FILE.format(
             retention_policy=environment_specific.get_backup_retention_policy(instance),
             backup_type=backup_type,
             replica_set=instance.get_zk_replica_set()[0],
             hostname=instance.hostname,
             port=instance.port,
             timestamp=timestamp_formatted,
             extension=extension)