Skip to content

Instantly share code, notes, and snippets.

@dario61081
Created February 26, 2024 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dario61081/79385f5343073219f3df64dc40cf77af to your computer and use it in GitHub Desktop.
Save dario61081/79385f5343073219f3df64dc40cf77af to your computer and use it in GitHub Desktop.
backup utils
class BackupApi(MethodView):
def get(self):
def filesize_humanize(size):
if size < 1024:
return f'{size} bytes'
elif size < 1024 * 1024:
return f'{size / 1024:.2f} KB'
elif size < 1024 * 1024 * 1024:
return f'{size / 1024 / 1024:.2f} MB'
elif size < 1024 * 1024 * 1024 * 1024:
return f'{size / 1024 / 1024 / 1024:.2f} GB'
else:
return f'{size / 1024 / 1024 / 1024 / 1024:.2f} TB'
def get_backup_info(item):
filename = os.path.basename(item)
return {
'file': filename,
'size': filesize_humanize(os.path.getsize(item)),
'date': datetime.fromtimestamp(os.path.getmtime(item)).strftime('%Y-%m-%d %H:%M:%S')
}
source = session.get('source', '')
patternfile = os.path.join(current_app.root_path, 'backups', source, '*.tgz')
lista = glob.glob(patternfile)
print(lista)
lista.sort()
print(lista)
lista = map(get_backup_info, lista)
lista = [dict(item) for item in lista]
return jsonify(lista)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment