Skip to content
Snippets Groups Projects
test_backup.py 1.99 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2017, Florent Thiery
'''
Criticality: Medium
Stéphane Diemer's avatar
Stéphane Diemer committed
Checks that the server backups are not older than a day.
'''
import os
import sys
import subprocess
import imp
from datetime import datetime
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):
    path = '/backup/%s/current' % client
    cmd = 'ssh -o StrictHostKeyChecking=no %s ls -l %s | grep current' % (server, path)
Florent Thiery's avatar
Florent Thiery committed
    status, out = subprocess.getstatusoutput(cmd)
        date = out.strip().split(' ')[-2]
        pdate = datetime.strptime(date, '%Y-%m-%d')
        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')
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()
Stéphane Diemer's avatar
Stéphane Diemer committed
    BURP_SERVER = conf.get('BURP_SERVER')
    BURP_CLIENT_NAME = conf.get('BURP_CLIENT_NAME', 'localhost')
    if BURP_SERVER:
        if not test_ssh(BURP_SERVER):
            print('Failed to ssh into backup server')
            sys.exit(1)
        else:
            if not test_last_backup_is_recent(BURP_SERVER, BURP_CLIENT_NAME):
                sys.exit(1)
            else:
                sys.exit(0)
Florent Thiery's avatar
Florent Thiery committed
    print('No BURP_SERVER defined in config, untestable')