예제 #1
0
def perform_backup_cleanup(cfg):
    """
    Performs a cleanup of files and directories which are no longer needed.
    """
    try:
        logging.info("Cleaning up tmp directory.")
        fileutils.clean_tmp()

        total_removed = 0
        for backup in cfg.backups:
            if int(backup.rotationPeriod) != 0:
                archives_to_remove = []
                archive_dates = get_archive_names_and_times(backup.target)
                newest_archive_date = max(archive_dates.items(), key=operator.itemgetter(1))[1]
                oldest_archive_date = newest_archive_date-datetime.timedelta(days=backup.rotationPeriod)

                for archive in archive_dates.keys():
                    if oldest_archive_date > archive_dates[archive]:
                        archives_to_remove.append(archive)
                for archive in archives_to_remove:
                    fileutils.remove_file(archive)

                total_removed += len(archives_to_remove)
                logging.info("Cleaning up old archives at "+backup.target+". Removed a total of "+str(len(archives_to_remove))+" files")

        logging.info("Removed a total of " + str(total_removed) + " old archives.")
    except Exception as ex:
        raise TaskError("Couldn't complete cleanup: " + ex.__str__())
예제 #2
0
def main():
    global status, skipped_backups, inconsistencies_found
    #Processing configuration
    rex_config = None
    try:
        config_file_path = os.path.join(fileutils.get_working_dir(), "resources", "config.xml")
        logging.info("Reading config file: " + config_file_path)
        rex_config = config.readConfig(config_file_path)
    except Exception as ex:
        logging.fatal("Failed to parse configuration file. Reason: " + ex.__str__())

    if rex_config:
        #step 1:performing backups
        if len(rex_config.backups) > 0:
            for backup in rex_config.backups:
                if not is_downtime_period(backup):
                    try:
                        perform_backup(backup)
                        add_message(Status.Success, Tasks.Backup, backup.source)
                    except Exception as ex:
                        add_message(Status.Failed, Tasks.Backup,backup.source,ex.__str__())
                        logging.error("Failed to perform backup: " + ex.__str__())
                        break
                    try:
                        if rex_config.performChecks:
                            perform_backup_check(backup)
                            add_message(Status.Success, Tasks.Check, backup.source)
                    except ArchiveIntegrityError as ex:
                        add_message(Status.Failed, Tasks.Check,backup.source,ex.__str__())
                        logging.error("Backup check found some archive inconsistencies: " + ex.__str__())
                        inconsistencies_found+=len(ex.inconsistencies)
                    except Exception as ex:
                        add_message(Status.Failed, Tasks.Check,backup.source,ex.__str__())
                        logging.error("Failed to perform backup check: " + ex.__str__())
                else:
                    skipped_backups += 1
                    add_message(Status.Skipped, Tasks.Backup, backup.source)
                fileutils.clean_tmp()
            if skipped_backups == len(rex_config.backups):
                status = Status.Skipped

        #step 2:performing cleanup
        try:
            perform_backup_cleanup(rex_config)
            add_message(Status.Success, Tasks.Cleanup, fileutils.get_tmp_dir())
        except Exception as ex:
            add_message(Status.Failed, Tasks.Cleanup, fileutils.get_tmp_dir(), ex.__str__())
            logging.error("Failed to perform backup cleanup: " + ex.__str__())

        #step 3: performing reporting
        try:
            if rex_config.performReporting:
                perform_reporting(messages, rex_config.reporterConfig)
        except Exception as ex:
            logging.error("Failed to perform reporting: " + ex.__str__())