Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions rotate_backups_s3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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:
Expand Down
16 changes: 12 additions & 4 deletions rotate_backups_s3/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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__)
Expand All @@ -178,15 +178,23 @@ 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,
aws_secret_access_key=aws_secret_access_key,
include_list=include_list,
exclude_list=exclude_list,
dry_run=dry_run,
).rotate_backups(bucket)
).rotate_backups(bucket, prefix)

if __name__ == "__main__":
main()