routerbackup/backup_check

63 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import os
import datetime
import time
def get_args():
parser = argparse.ArgumentParser(
description='backup_check - check whether backups are current')
parser.add_argument('-H', '--host',
required=True,
action='store',
help='host name, used also as backup filename base ')
parser.add_argument('-o', '--backupdir',
required=True,
action='store',
help='backup directory')
parser.add_argument('-m', '--maxage',
required=False,
action='store',
type=int,
help='maximum backup age in hours, default: 24')
parser.add_argument('-v', '--verbose',
required=False,
action='store_true',
help='be verbose')
args = parser.parse_args()
return args
def main():
args = get_args()
if args.maxage:
maxage = args.maxage
else:
maxage = 24
configpath = f'{args.backupdir}/{args.host}.config'
try:
info = os.stat(configpath)
backuptime = info.st_mtime
except FileNotFoundError:
print(f'Last backup: never (backup file not found: {configpath})')
exit(1)
age = (time.time() - backuptime) / 3600
if age > 24:
agetext = f'{age // 24:.0f}d{age % 24:.0f}h'
else:
agetext = f'{age:.1f}h'
backuptimestamp = datetime.datetime.fromtimestamp(backuptime).strftime('%F %H:%M:%S')
if age > maxage:
print(f'WARNING: {args.host}: last backup {backuptimestamp}, {agetext} ago (>{maxage}h)')
exit(1)
if args.verbose:
print(f'{args.host}: last backup {backuptimestamp}, {agetext} ago')
if __name__ == "__main__":
main()
# vim: set ft=python tabstop=4 shiftwidth=4 expandtab smarttab: