2022-01-05 21:44:55 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from json import load
|
|
|
|
from subprocess import check_output
|
|
|
|
from sys import argv, exit
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
NODE = argv[1]
|
2022-02-12 18:04:15 +00:00
|
|
|
ONE_BACKUP_EVERY_HOURS = argv[2]
|
2022-01-05 21:44:55 +00:00
|
|
|
|
|
|
|
NOW = int(time())
|
|
|
|
DAY_SECONDS = 60 * 60 * 24
|
|
|
|
|
|
|
|
snaps = set()
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(f'/etc/backup-server/config.json', 'r') as f:
|
|
|
|
server_settings = load(f)
|
|
|
|
|
|
|
|
# get all existing snapshots for NODE
|
|
|
|
for line in check_output('LC_ALL=C zfs list -H -t snapshot -o name', shell=True).splitlines():
|
|
|
|
line = line.decode('UTF-8')
|
|
|
|
|
|
|
|
if line.startswith('{}/{}@'.format(server_settings['zfs-base'], NODE)):
|
|
|
|
_, snapname = line.split('@', 1)
|
|
|
|
|
|
|
|
if 'zfs-auto-snap' in snapname:
|
|
|
|
# migration from auto-snapshots, ignore
|
|
|
|
continue
|
|
|
|
|
|
|
|
ts, bucket = snapname.split('-', 1)
|
|
|
|
snaps.add(int(ts))
|
|
|
|
|
|
|
|
if not snaps:
|
|
|
|
print('No backups found!')
|
|
|
|
exit(2)
|
|
|
|
|
|
|
|
last_snap = sorted(snaps)[-1]
|
|
|
|
delta = NOW - last_snap
|
|
|
|
|
2022-01-05 21:56:00 +00:00
|
|
|
print('Last backup was on {} UTC'.format(
|
2022-01-05 21:44:55 +00:00
|
|
|
datetime.fromtimestamp(last_snap).strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
))
|
|
|
|
|
2022-01-09 07:26:08 +00:00
|
|
|
# One day without backups is still okay. There may be fluctuations
|
|
|
|
# because of transfer speed, amount of data, changes in backup
|
|
|
|
# schedule etc.
|
2022-02-12 18:04:15 +00:00
|
|
|
if delta > (DAY_SECONDS * (ONE_BACKUP_EVERY_HOURS + 2)):
|
2022-01-05 21:44:55 +00:00
|
|
|
exit(2)
|
2022-02-12 18:04:15 +00:00
|
|
|
elif delta > (DAY_SECONDS * (ONE_BACKUP_EVERY_HOURS + 1)):
|
2022-01-05 21:44:55 +00:00
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
exit(0)
|
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|
|
|
|
exit(3)
|