diff --git a/management/backup.py b/management/backup.py index 71fa859e..a14c872b 100755 --- a/management/backup.py +++ b/management/backup.py @@ -11,12 +11,17 @@ import os, os.path, shutil, glob, re, datetime import dateutil.parser, dateutil.relativedelta, dateutil.tz import rtyaml +import logging from utils import exclusive_process, load_environment, shell, wait_for_service # Root folder backup_root = os.path.join(load_environment()["STORAGE_ROOT"], 'backup') +# Setup logging +log_path = os.path.join(backup_root, 'backup.log') +logging.basicConfig(filename=log_path, format='%(levelname)s at %(asctime)s: %(message)s') + # Default settings # min_age_in_days is the minimum amount of days a backup will be kept before # it is eligble to be removed. Backups might be kept much longer if there's no @@ -236,6 +241,9 @@ def perform_backup(full_backup): "--allow-source-mismatch" ], get_env()) + logging.info("Backup successful") + except Exception as e: + logging.warn("Backup failed: {0}".format(e)) finally: # Start services again. shell('check_call', ["/usr/sbin/service", "dovecot", "start"]) @@ -247,29 +255,35 @@ def perform_backup(full_backup): # Remove old backups. This deletes all backup data no longer needed # from more than 3 days ago. - shell('check_call', [ - "/usr/bin/duplicity", - "remove-older-than", - "%dD" % config["min_age_in_days"], - "--archive-dir", backup_cache_dir, - "--force", - config["target"] - ], - get_env()) + try: + shell('check_call', [ + "/usr/bin/duplicity", + "remove-older-than", + "%dD" % config["min_age_in_days"], + "--archive-dir", backup_cache_dir, + "--force", + config["target"] + ], + get_env()) + except Exception as e: + logging.warn("Removal of old backups failed: {0}".format(e)) # From duplicity's manual: # "This should only be necessary after a duplicity session fails or is # aborted prematurely." # That may be unlikely here but we may as well ensure we tidy up if # that does happen - it might just have been a poorly timed reboot. - shell('check_call', [ - "/usr/bin/duplicity", - "cleanup", - "--archive-dir", backup_cache_dir, - "--force", - config["target"] - ], - get_env()) + try: + shell('check_call', [ + "/usr/bin/duplicity", + "cleanup", + "--archive-dir", backup_cache_dir, + "--force", + config["target"] + ], + get_env()) + except Exception as e: + logging.warn("Cleanup of backups failed: {0}".format(e)) # Change ownership of backups to the user-data user, so that the after-bcakup # script can access them. @@ -336,6 +350,16 @@ def get_backup_config(): merged_config.update(config) return config + +def get_backup_log(): + try: + fileHandle = open(log_path, 'r') + log = fileHandle.read() + fileHandle.close() + except: + log = "" + + return log def write_backup_config(newconfig): with open(os.path.join(backup_root, 'custom.yaml'), "w") as f: diff --git a/management/daemon.py b/management/daemon.py index 4b63f71b..b011823a 100755 --- a/management/daemon.py +++ b/management/daemon.py @@ -4,7 +4,7 @@ import os, os.path, re, json from functools import wraps -from flask import Flask, request, render_template, abort, Response, send_from_directory +from flask import Flask, request, render_template, abort, Response, send_from_directory, jsonify import auth, utils from mailconfig import get_mail_users, get_mail_users_ex, get_admins, add_mail_user, set_mail_password, remove_mail_user @@ -399,14 +399,14 @@ def do_updates(): @app.route('/system/backup/status') @authorized_personnel_only def backup_status(): - from backup import backup_status - return json_response(backup_status(env)) + from backup import backup_status, get_backup_log + return jsonify(backups=backup_status(env), log=get_backup_log()) @app.route('/system/backup/config', methods=["GET"]) @authorized_personnel_only def backup_get_custom(): from backup import get_backup_config - return json_response(get_backup_config()) + return jsonify(get_backup_config()) @app.route('/system/backup/config', methods=["POST"]) @authorized_personnel_only diff --git a/management/templates/system-backup.html b/management/templates/system-backup.html index 615ba980..24fa5f57 100644 --- a/management/templates/system-backup.html +++ b/management/templates/system-backup.html @@ -68,6 +68,11 @@ + +

Backup logs

+

+
+