#!/usr/bin/python3 from flask import Flask, render_template import json from datetime import datetime, date, timedelta import re import os.path, time app = Flask(__name__) @app.route('/') def index(): try: with open('/tmp/backupcopies.json') as h: backupcopies = json.load(h) except: backupcopies = {} try: with open('/tmp/forget.txt') as g: data = json.load(g) forget_list = [short_id for rec in data if rec.get('remove') for short_id in [r['short_id'] for r in rec['remove']]] except: forget_list = [] with open('/tmp/snapshots.json') as f: data = json.load(f) for item in data: if item['short_id'] in forget_list: item['mopped'] = True else: item['mopped'] = '' today = date.today().strftime("%d.%m.%Y") tp = time.localtime(os.path.getmtime('/tmp/snapshots.json')) # convert struct_time to string with strftime, the result looks like "2022-03-30 12:23:55" tmp_restic_json_date = time.strftime('%d.%m.%Y %H:%M:%S', tp) try: tp = time.localtime(os.path.getmtime('/tmp/forget.txt')) # convert struct_time to string with strftime, the result looks like "2022-03-30 12:23:55" tmp_restic_forget_date = time.strftime('forget.txt vom %d.%m.%Y %H:%M:%S', tp) except: tmp_restic_forget_date = '' return render_template( 'table.html', data=data, today=today, tmp_restic_json_date=tmp_restic_json_date, tmp_restic_forget_date=tmp_restic_forget_date, lastbackup=backupcopies['__summary'], ) if __name__ == '__main__': app.run(port=5001, host='0.0.0.0', FLASK_ENV='development') # -------------------------------------------------- helper routines --- def parse_date(date_string): formats = ( '%Y-%m-%dT%H:%M:%S%z', '%Y-%m-%dT%H:%M:%S.%f%z', '%Y-%m-%dT%H:%M:%SZ' ) if len(date_string) > 26: tz = 'Z' m = re.search(r"(\+.*?)$", date_string) if m: tz = m[0] date_string = date_string[0:19] + tz #'Z+0000' for fmt in formats: try: return datetime.strptime(date_string, fmt) except ValueError: raise ValueError('no valid date format found for %s' % date_string) # https://stackoverflow.com/questions/6036082/call-a-python-function-from-jinja2 # colors from @app.context_processor def utility_processor(): def time_class(rtime): date_obj = parse_date(rtime) diff = date.today() - date_obj.date() if diff <= timedelta(days=0): return 'daily1' elif diff <= timedelta(days=1): return 'daily2' elif diff <= timedelta(days=7): return 'daily3' elif diff <= timedelta(days=15): return 'weekly' elif diff <= timedelta(days=30): return 'monthly' elif diff <= timedelta(days=200): return 'yearly1' elif diff <= timedelta(days=400): return 'yearly2' else: return 'yearly3' def clean_date(rtime): date_obj = parse_date(rtime) return date_obj.strftime('%d.%m.%Y') def clean_datetime(rtime): date_obj = parse_date(rtime) return date_obj.strftime('%d.%m.%Y %H:%M') def forget_pattern(mopped): if mopped: return 'forget' else: return '' def gigabytes(bytes): return int(int(bytes)/1024/1024) return dict( time_class=time_class, clean_date=clean_date, clean_datetime=clean_datetime, forget_pattern=forget_pattern, gigabytes=gigabytes)