Skip to content
Snippets Groups Projects
test_backup.py 4.25 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2017, Florent Thiery
'''
Stéphane Diemer's avatar
Stéphane Diemer committed
Criticality: Normal
Stéphane Diemer's avatar
Stéphane Diemer committed
Checks that the server backups are not older than a day.
from datetime import datetime
import imp
Florent Thiery's avatar
Florent Thiery committed
GREEN = '\033[92m'
RED = '\033[91m'
DEF = '\033[0m'

Florent Thiery's avatar
Florent Thiery committed
def test_ssh(ip):
    cmd = 'ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no %s ls /tmp' % ip
    print('Connecting to MediaVault: %s' % cmd)
    try:
        subprocess.check_output(cmd, shell=True, timeout=2)
        print('%sLogged in successfully%s' % (GREEN, DEF))
    except subprocess.CalledProcessError:
        print('%sFailed to login using SSH, run ssh-copy-id %s %s' % (RED, ip, DEF))
        return False
    return True
Stéphane Diemer's avatar
Stéphane Diemer committed

def test_last_backup_is_recent(server):
    client = socket.gethostname()
Florent Thiery's avatar
Florent Thiery committed
    path = '/backup/%s/home/latest' % client
    cmd = 'ssh -o StrictHostKeyChecking=no %s ls -l %s | grep latest' % (server, path)
Florent Thiery's avatar
Florent Thiery committed
    status, out = subprocess.getstatusoutput(cmd)
        date = out.strip().split(' ')[-1]
        pdate = datetime.strptime(date, '%Y-%m-%d-%H%M%S')
        if (datetime.now() - pdate).days > 2:
            print('Backup is older than 2 days')
            return False
        else:
            print('There is a backup that is less than 2 days old, this is fine')
        out = out or 'No output.'
        print('SSH access is not working (code: %s):\n%s' % (status, out))
Stéphane Diemer's avatar
Stéphane Diemer committed

def test_backup_space(server):
    cmd = 'ssh -o StrictHostKeyChecking=no %s df -h /backup | tail -n 1' % (server)
    status, out = subprocess.getstatusoutput(cmd)
    if status == 0:
        dev, total, used, free, used_perc, mount = out.strip().split()
        used_perc = int(used_perc.replace('%',''))
        if used_perc > 80:
            print('There is less than 20% of available space for backups')
            return False
        else:
            print('There is %s%% of free space' % (100 - used_perc))
            return True
    else:
        print('Failed to check backup space')
        return False


def check_local_backup(path, descend=True):
    all_ok = True
    for d in os.listdir(path):
        subd = os.path.join(path, d)
        if os.path.isdir(subd):
            latest = os.path.join(subd, 'latest')
Florent Thiery's avatar
Florent Thiery committed
            print('Checking %s' % latest)
            if os.path.exists(latest):
                mtime = os.path.getmtime(latest)
                d = datetime.fromtimestamp(mtime)
                now = datetime.now()
                diff_seconds = (now - d).total_seconds()
                if diff_seconds > 25*3600:
                    print('Backup %s is older than a day' % subd)
                    all_ok = False 
                else:
                    print('Backup %s is fine' % subd)
        else:
            if descend:
                for dd in os.listdir(subd):
                    subsubd = os.path.join(subd, dd)
                    if os.path.isdir(subsubd):
                        all_ok = min(all_ok, check_local_backup(subsubd, descend=False))
    return all_ok


def check_local_backups(paths):
    all_ok = True
    blacklist = ('lost+found')
    folders = paths.split(',')
    for f in folders:
        if f not in blacklist:
            print('Checking top folder %s' % f)
            all_ok = min(check_local_backup(f), all_ok)
    return all_ok


Stéphane Diemer's avatar
Stéphane Diemer committed
os.chdir(os.path.dirname(__file__))
if os.path.isfile('../utils.py'):
    es_utils = imp.load_source('es_utils', '../utils.py')
    conf = es_utils.load_conf()
    BACKUP_SERVER = conf.get('BACKUP_SERVER')
    LOCAL_BACKUP_FOLDERS = conf.get('LOCAL_BACKUP_FOLDERS')
    if BACKUP_SERVER:
        if not test_ssh(BACKUP_SERVER):
            print('Failed to ssh into backup server')
            sys.exit(1)
        else:
            if not test_last_backup_is_recent(BACKUP_SERVER):
                sys.exit(1)
            else:
                if not test_backup_space(BACKUP_SERVER):
    elif LOCAL_BACKUP_FOLDERS:
        sys.exit(check_local_backups(LOCAL_BACKUP_FOLDERS))
        print('No BACKUP_SERVER defined in config, untestable')
    print('Unable to load config, untestable')