restic2html.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/python3
  2. from flask import Flask, render_template
  3. import json
  4. from datetime import datetime, date, timedelta
  5. import re
  6. import os.path, time
  7. app = Flask(__name__)
  8. @app.route('/')
  9. def index():
  10. try:
  11. with open('/tmp/backupcopies.json') as h:
  12. backupcopies = json.load(h)
  13. except:
  14. backupcopies = {}
  15. try:
  16. with open('/tmp/forget.txt') as g:
  17. data = json.load(g)
  18. forget_list = [short_id for rec in data if rec.get('remove') for short_id in [r['short_id'] for r in rec['remove']]]
  19. except:
  20. forget_list = []
  21. with open('/tmp/snapshots.json') as f:
  22. data = json.load(f)
  23. for item in data:
  24. if item['short_id'] in forget_list:
  25. item['mopped'] = True
  26. else:
  27. item['mopped'] = ''
  28. today = date.today().strftime("%d.%m.%Y")
  29. tp = time.localtime(os.path.getmtime('/tmp/snapshots.json'))
  30. # convert struct_time to string with strftime, the result looks like "2022-03-30 12:23:55"
  31. tmp_restic_json_date = time.strftime('%d.%m.%Y %H:%M:%S', tp)
  32. try:
  33. tp = time.localtime(os.path.getmtime('/tmp/forget.txt'))
  34. # convert struct_time to string with strftime, the result looks like "2022-03-30 12:23:55"
  35. tmp_restic_forget_date = time.strftime('forget.txt vom %d.%m.%Y %H:%M:%S', tp)
  36. except:
  37. tmp_restic_forget_date = ''
  38. return render_template( 'table.html', data=data, today=today,
  39. tmp_restic_json_date=tmp_restic_json_date,
  40. tmp_restic_forget_date=tmp_restic_forget_date,
  41. lastbackup=backupcopies['__summary'],
  42. )
  43. if __name__ == '__main__':
  44. app.run(port=5001, host='0.0.0.0', FLASK_ENV='development')
  45. # -------------------------------------------------- helper routines ---
  46. def parse_date(date_string):
  47. formats = (
  48. '%Y-%m-%dT%H:%M:%S%z',
  49. '%Y-%m-%dT%H:%M:%S.%f%z',
  50. '%Y-%m-%dT%H:%M:%SZ'
  51. )
  52. if len(date_string) > 26:
  53. tz = 'Z'
  54. m = re.search(r"(\+.*?)$", date_string)
  55. if m:
  56. tz = m[0]
  57. date_string = date_string[0:19] + tz #'Z+0000'
  58. for fmt in formats:
  59. try:
  60. return datetime.strptime(date_string, fmt)
  61. except ValueError:
  62. raise ValueError('no valid date format found for %s' % date_string)
  63. # https://stackoverflow.com/questions/6036082/call-a-python-function-from-jinja2
  64. # colors from
  65. @app.context_processor
  66. def utility_processor():
  67. def time_class(rtime):
  68. date_obj = parse_date(rtime)
  69. diff = date.today() - date_obj.date()
  70. if diff <= timedelta(days=0):
  71. return 'daily1'
  72. elif diff <= timedelta(days=1):
  73. return 'daily2'
  74. elif diff <= timedelta(days=7):
  75. return 'daily3'
  76. elif diff <= timedelta(days=15):
  77. return 'weekly'
  78. elif diff <= timedelta(days=30):
  79. return 'monthly'
  80. elif diff <= timedelta(days=200):
  81. return 'yearly1'
  82. elif diff <= timedelta(days=400):
  83. return 'yearly2'
  84. else:
  85. return 'yearly3'
  86. def clean_date(rtime):
  87. date_obj = parse_date(rtime)
  88. return date_obj.strftime('%d.%m.%Y')
  89. def clean_datetime(rtime):
  90. date_obj = parse_date(rtime)
  91. return date_obj.strftime('%d.%m.%Y %H:%M')
  92. def forget_pattern(mopped):
  93. if mopped:
  94. return 'forget'
  95. else:
  96. return ''
  97. def gigabytes(bytes):
  98. return int(int(bytes)/1024/1024)
  99. return dict( time_class=time_class, clean_date=clean_date,
  100. clean_datetime=clean_datetime,
  101. forget_pattern=forget_pattern,
  102. gigabytes=gigabytes)