Skip to content

Instantly share code, notes, and snippets.

@JonasPf
Created March 22, 2016 13:51
Show Gist options
  • Save JonasPf/2eb195a2f2f7a4806c71 to your computer and use it in GitHub Desktop.
Save JonasPf/2eb195a2f2f7a4806c71 to your computer and use it in GitHub Desktop.
List openstack freezer backups with datetime and size
import subprocess
import datetime
import re
import sys
backups = subprocess.check_output(['swift', 'list', sys.argv[1]]).splitlines()
width = None
total = 0
def size_fmt(bytes):
if bytes > 1024 * 1024 * 1024:
return "{} GiB".format(bytes / 1024 / 1024 / 1024)
elif bytes > 1024 * 1024:
return "{} MiB".format(bytes / 1024 / 1024)
elif bytes > 1024:
return "{} KiB".format(bytes / 1024)
else:
return "{} Bytes".format(bytes)
for backup in backups:
if width is None:
width = len(backup) + 5
if backup.startswith('tar'):
continue
else:
info = subprocess.check_output(['swift', 'stat', 'freezer_deployer_backup', backup])
result = re.search('Content Length: (.*)$', info, re.MULTILINE)
size = int(result.group(1))
result = re.search('^.*_(.*)_.*$', backup)
date = datetime.datetime.fromtimestamp(int(result.group(1)))
print date.strftime('%x %X') + " " + backup.ljust(width) + " " + size_fmt(size)
total += size
print "-" * (20 + width + 1 + len(size_fmt(size)))
print "Total:".ljust(width + 20) + " " + size_fmt(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment