diff --git a/rotate_backups_s3/__init__.py b/rotate_backups_s3/__init__.py index be4aad4..14ce5e8 100644 --- a/rotate_backups_s3/__init__.py +++ b/rotate_backups_s3/__init__.py @@ -117,7 +117,7 @@ def __init__(self, rotation_scheme, aws_access_key_id, aws_secret_access_key, include_list=include_list, exclude_list=exclude_list, dry_run=dry_run, config_file=config_file) - def rotate_backups(self, bucketname): + def rotate_backups(self, bucketname, prefix): """ Rotate the backups in a bucket according to a flexible rotation scheme. @@ -126,7 +126,7 @@ def rotate_backups(self, bucketname): bucket = self.conn.get_bucket(bucketname) # Collect the backups in the given directory. - sorted_backups = self.collect_backups(bucketname) + sorted_backups = self.collect_backups(bucketname, prefix) if not sorted_backups: logger.info("No backups found in %s.", bucketname) return @@ -157,7 +157,7 @@ def rotate_backups(self, bucketname): if len(backups_to_preserve) == len(sorted_backups): logger.info("Nothing to do! (all backups preserved)") - def collect_backups(self, bucketname): + def collect_backups(self, bucketname, prefix): """ Collect the backups in the given s3 bucket. @@ -168,9 +168,10 @@ def collect_backups(self, bucketname): backups = [] bucket = self.conn.get_bucket(bucketname) - logger.info("Scanning bucket for backups: %s", bucketname) - - for entry in natsort([key.name for key in bucket.list()]): + + logger.info("Scanning for backups: s3://%s/%s", bucketname, prefix) + + for entry in natsort([key.name for key in bucket.list(prefix)]): # Check for a time stamp in the directory entry's name. match = TIMESTAMP_PATTERN.search(entry) if match: diff --git a/rotate_backups_s3/cli.py b/rotate_backups_s3/cli.py index e6083eb..fae5e67 100644 --- a/rotate_backups_s3/cli.py +++ b/rotate_backups_s3/cli.py @@ -5,7 +5,7 @@ # URL: https://github.com/xolox/python-rotate-backups """ -Usage: rotate-backups-s3 [OPTIONS] DIRECTORY.. +Usage: rotate-backups-s3 [OPTIONS] [s3://]bucket[/prefix].. Easy rotation of backups in an AWS S3 bucket based. To use this program you specify a rotation scheme via (a combination of) the --hourly, @@ -169,7 +169,7 @@ def main(): # If no arguments are given but the system has a configuration file # then the backups in the configured directories are rotated. if not arguments: - arguments.extend(bucket for bucket, _, _ in load_config_file(config_file)) + arguments.extend(s3path for s3path, _, _ in load_config_file(config_file)) # Show the usage message when no directories are given nor configured. if not arguments: usage(__doc__) @@ -178,7 +178,15 @@ def main(): logger.error("%s", e) sys.exit(1) # Rotate the backups in the given or configured directories. - for bucket in arguments: + for s3path in arguments: + prefix = '' + bucket = s3path[5:] if s3path.startswith('s3://') else s3path + + pos = bucket.find('/') + if pos != -1: + prefix = bucket[pos:].strip('/') + bucket = bucket[:pos] + S3RotateBackups( rotation_scheme=rotation_scheme, aws_access_key_id=aws_access_key_id, @@ -186,7 +194,7 @@ def main(): include_list=include_list, exclude_list=exclude_list, dry_run=dry_run, - ).rotate_backups(bucket) + ).rotate_backups(bucket, prefix) if __name__ == "__main__": main() \ No newline at end of file