diff --git a/tests/test_backup.py b/tests/test_backup.py index 2a34b4e40d21b2d273b31aa85c09c88c359ca3ad..6374abafa286a306963e0f0de1a128b3c2ca01be 100755 --- a/tests/test_backup.py +++ b/tests/test_backup.py @@ -11,7 +11,7 @@ import os import socket import subprocess import sys - +import glob GREEN = '\033[92m' RED = '\033[91m' @@ -19,12 +19,15 @@ DEF = '\033[0m' MAX_AGE_H = 48 + def print_red(string): print(RED + string + DEF) + def print_green(string): print(GREEN + string + DEF) + def test_ssh(ip): cmd = 'ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no %s ls /tmp' % ip print('Connecting to MediaVault: %s' % cmd) @@ -64,7 +67,7 @@ def test_backup_space(server=None, path="/backup"): status, out = subprocess.getstatusoutput(cmd) if status == 0: dev, total, used, free, used_perc, mount = out.strip().split() - used_perc = int(used_perc.replace('%','')) + used_perc = int(used_perc.replace('%', '')) if used_perc > 80: print('There is less than 20% of available space for backups') return False @@ -75,6 +78,24 @@ def test_backup_space(server=None, path="/backup"): print('Failed to check backup space') return False + +def check_backup_is_incremental(path): + dirs = os.listdir(path) + dirs.sort() + + for d in dirs: + num_folders = 0 + if os.path.isdir(d): + media = glob.glob(os.path.join(path, d, "*/msinstance/media/resources/")) + for m in media: + num_folders += len(os.listdir(m)) + print('%s: %s' % (d, num_folders)) + if num_folders == 0: + print('Folder %s is empty, this indicates non-incremental backups (we are expecting links)' % d) + return False + return True + + def check_local_backup(path): all_ok = True backup_folder = os.path.dirname(path) @@ -87,11 +108,13 @@ def check_local_backup(path): d = datetime.strptime(date, '%Y-%m-%d-%H%M%S') now = datetime.now() diff_seconds = (now - d).total_seconds() - if diff_seconds > MAX_AGE_H*3600: - print_red('Backup %s is older than %sh (%ih)' % (backup_folder, MAX_AGE_H, diff_seconds/3600)) - all_ok = False + if diff_seconds > MAX_AGE_H * 3600: + print_red('Backup %s is older than %sh (%ih)' % (backup_folder, MAX_AGE_H, diff_seconds / 3600)) + all_ok = False else: print_green('Backup %s is fine' % backup_folder) + if not check_backup_is_incremental(backup_folder): + all_ok = False elif os.path.exists(os.path.join(backup_folder, 'backup.inprogress')): print_red('Initial backup %s still running' % backup_folder) all_ok = False