bundlewrap/bundles/backup-server/files/check_backup_for_node

44 lines
1.1 KiB
Python

#!/usr/bin/env python3
from datetime import datetime
from json import load
from sys import argv, exit
from time import time
NODE = argv[1]
ONE_BACKUP_EVERY_HOURS = int(argv[2])
NOW = int(time())
HOUR_SECONDS = 60 * 60
snaps = set()
try:
with open(f'/etc/backup-server/config.json', 'r') as f:
server_settings = load(f)
with open(f'/etc/backup-server/backups.json', 'r') as f:
backups = load(f)
if NODE not in backups:
print('No backups found!')
exit(2)
delta = NOW - backups[NODE]
print('Last backup was on {} UTC'.format(
datetime.fromtimestamp(backups[NODE]).strftime('%Y-%m-%d %H:%M:%S'),
))
# One day without backups is still okay. There may be fluctuations
# because of transfer speed, amount of data, changes in backup
# schedule etc.
if delta > ((HOUR_SECONDS * (ONE_BACKUP_EVERY_HOURS + 1)) + (HOUR_SECONDS*24)):
exit(2)
elif delta > (HOUR_SECONDS * (ONE_BACKUP_EVERY_HOURS + 1)):
exit(1)
else:
exit(0)
except Exception as e:
print(repr(e))
exit(3)