Example #1
0
def test_config(path_to_config):
    try:
        specific_function.get_parsed_string(path_to_config)
    except general_function.MyError as e:
        general_function.print_info("The configuration file '%s' syntax is bad: %s! "
                                    %(path_to_config, e))
    else:
        general_function.print_info("The configuration file '%s' syntax is ok!" %(path_to_config))
    finally:
        sys.exit()
Example #2
0
def do_backup(path_to_config, jobs_name):
    resource_constraint.set_limitations()

    try:
        parsed_string = specific_function.get_parsed_string(path_to_config)
    except general_function.MyError as e:
        general_function.print_info(
            "An error in the parse of the configuration file %s:%s!" %
            (path_to_config, e))
        sys.exit(1)

    (db_jobs_dict, file_jobs_dict,
     external_jobs_dict) = config.get_conf_value(parsed_string)

    general_function.create_files('', config.log_file)

    if not jobs_name in config.all_jobs_name:
        general_function.print_info(
            "Only one of this job's name is allowed: %s" %
            (config.general_str))
        sys.exit(1)

    try:
        config.filelog_fd = open(config.log_file, 'a')
    except OSError:  # e.g. /dev/stdout
        try:
            config.filelog_fd = open(config.log_file, 'w')
        except (OSError, PermissionError, FileNotFoundError) as e:
            messange_info = "Couldn't open file %s:%s!" % (config.log_file, e)
            general_function.print_info(messange_info)
            log_and_mail.send_report(messange_info)
            sys.exit(1)
    except (PermissionError, FileNotFoundError) as e:
        messange_info = "Couldn't open file %s:%s!" % (config.log_file, e)
        general_function.print_info(messange_info)
        log_and_mail.send_report(messange_info)
        sys.exit(1)

    if general_function.is_running_script():
        log_and_mail.writelog('ERROR', "Script already is running!",
                              config.filelog_fd, '')
        config.filelog_fd.close()
        general_function.print_info("Script already is running!")
        sys.exit(1)

    log_and_mail.writelog('INFO', "Starting script.\n", config.filelog_fd)

    if jobs_name == 'all':
        log_and_mail.writelog('INFO', "Starting files block backup.",
                              config.filelog_fd)
        for i in list(file_jobs_dict.keys()):
            current_jobs_name = file_jobs_dict[i]['job']
            execute_job(current_jobs_name, file_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing files block backup.",
                              config.filelog_fd)

        log_and_mail.writelog('INFO', "Starting databases block backup.",
                              config.filelog_fd)
        for i in list(db_jobs_dict.keys()):
            current_jobs_name = db_jobs_dict[i]['job']
            execute_job(current_jobs_name, db_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing databases block backup.\n",
                              config.filelog_fd)

        log_and_mail.writelog('INFO', "Starting external block backup.",
                              config.filelog_fd)
        for i in list(external_jobs_dict.keys()):
            current_jobs_name = external_jobs_dict[i]['job']
            execute_job(current_jobs_name, external_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing external block backup.\n",
                              config.filelog_fd)
    elif jobs_name == 'databases':
        log_and_mail.writelog('INFO', "Starting databases block backup.",
                              config.filelog_fd)
        for i in list(db_jobs_dict.keys()):
            current_jobs_name = db_jobs_dict[i]['job']
            execute_job(current_jobs_name, db_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing databases block backup.\n",
                              config.filelog_fd)
    elif jobs_name == 'files':
        log_and_mail.writelog('INFO', "Starting files block backup.",
                              config.filelog_fd)
        for i in list(file_jobs_dict.keys()):
            current_jobs_name = file_jobs_dict[i]['job']
            execute_job(current_jobs_name, file_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing files block backup.\n",
                              config.filelog_fd)
    elif jobs_name == 'external':
        log_and_mail.writelog('INFO', "Starting external block backup.",
                              config.filelog_fd)
        for i in list(external_jobs_dict.keys()):
            current_jobs_name = external_jobs_dict[i]['job']
            execute_job(current_jobs_name, external_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing external block backup.\n",
                              config.filelog_fd)
    else:
        if jobs_name in list(db_jobs_dict.keys()):
            log_and_mail.writelog('INFO', "Starting databases block backup.",
                                  config.filelog_fd)
            execute_job(jobs_name, db_jobs_dict[jobs_name])
            log_and_mail.writelog('INFO',
                                  "Finishing databases block backup.\n",
                                  config.filelog_fd)
        if jobs_name in list(file_jobs_dict.keys()):
            log_and_mail.writelog('INFO', "Starting files block backup.",
                                  config.filelog_fd)
            execute_job(jobs_name, file_jobs_dict[jobs_name])
            log_and_mail.writelog('INFO', "Finishing files block backup.\n",
                                  config.filelog_fd)
        else:
            log_and_mail.writelog('INFO', "Starting external block backup.",
                                  config.filelog_fd)
            execute_job(jobs_name, external_jobs_dict[jobs_name])
            log_and_mail.writelog('INFO', "Finishing external block backup.\n",
                                  config.filelog_fd)

    log_and_mail.writelog('INFO', "Stopping script.", config.filelog_fd)
    log_and_mail.send_report()
    config.filelog_fd.close()
Example #3
0
def do_backup(path_to_config, jobs_name):
    try:
        parsed_string = specific_function.get_parsed_string(path_to_config)
    except general_function.MyError as e:
        general_function.print_info(
            f"An error in the parse of the configuration file {path_to_config}:{e}!"
        )
        sys.exit(1)

    db_jobs_dict, file_jobs_dict, external_jobs_dict = config.get_conf_value(
        parsed_string)

    resource_constraint.set_limitations()

    general_function.create_files('', config.log_file)

    if jobs_name not in config.all_jobs_name:
        general_function.print_info(
            f"Only one of this job's name is allowed: {config.general_str}")
        sys.exit(1)

    try:
        config.filelog_fd = open(config.log_file, 'a')
    except OSError:  # e.g. /dev/stdout
        try:
            config.filelog_fd = open(config.log_file, 'w')
        except (OSError, PermissionError, FileNotFoundError) as e:
            messange_info = f"Couldn't open file {config.log_file}:{e}!"
            general_function.print_info(messange_info)
            log_and_mail.send_report(messange_info)
            sys.exit(1)
    except (PermissionError, FileNotFoundError) as e:
        messange_info = f"Couldn't open file {config.log_file}:{e}!"
        general_function.print_info(messange_info)
        log_and_mail.send_report(messange_info)
        sys.exit(1)

    try:
        general_function.get_lock()
    except general_function.MyError as ex:
        msg = ex.message
        log_and_mail.writelog('ERROR', f"{msg}", config.filelog_fd, '')
        general_function.print_info(f"{msg}")
        sys.exit(1)

    log_and_mail.writelog('INFO', "Starting script.\n", config.filelog_fd)

    if jobs_name == 'all':
        log_and_mail.writelog('INFO', "Starting files block backup.",
                              config.filelog_fd)
        for i in list(file_jobs_dict.keys()):
            current_jobs_name = file_jobs_dict[i]['job']
            execute_job(current_jobs_name, file_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing files block backup.",
                              config.filelog_fd)

        log_and_mail.writelog('INFO', "Starting databases block backup.",
                              config.filelog_fd)
        for i in list(db_jobs_dict.keys()):
            current_jobs_name = db_jobs_dict[i]['job']
            execute_job(current_jobs_name, db_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing databases block backup.\n",
                              config.filelog_fd)

        log_and_mail.writelog('INFO', "Starting external block backup.",
                              config.filelog_fd)
        for i in list(external_jobs_dict.keys()):
            current_jobs_name = external_jobs_dict[i]['job']
            execute_job(current_jobs_name, external_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing external block backup.\n",
                              config.filelog_fd)
    elif jobs_name == 'databases':
        log_and_mail.writelog('INFO', "Starting databases block backup.",
                              config.filelog_fd)
        for i in list(db_jobs_dict.keys()):
            current_jobs_name = db_jobs_dict[i]['job']
            execute_job(current_jobs_name, db_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing databases block backup.\n",
                              config.filelog_fd)
    elif jobs_name == 'files':
        log_and_mail.writelog('INFO', "Starting files block backup.",
                              config.filelog_fd)
        for i in list(file_jobs_dict.keys()):
            current_jobs_name = file_jobs_dict[i]['job']
            execute_job(current_jobs_name, file_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing files block backup.\n",
                              config.filelog_fd)
    elif jobs_name == 'external':
        log_and_mail.writelog('INFO', "Starting external block backup.",
                              config.filelog_fd)
        for i in list(external_jobs_dict.keys()):
            current_jobs_name = external_jobs_dict[i]['job']
            execute_job(current_jobs_name, external_jobs_dict[i])
        log_and_mail.writelog('INFO', "Finishing external block backup.\n",
                              config.filelog_fd)
    else:
        if jobs_name in list(db_jobs_dict.keys()):
            log_and_mail.writelog('INFO', "Starting databases block backup.",
                                  config.filelog_fd)
            execute_job(jobs_name, db_jobs_dict[jobs_name])
            log_and_mail.writelog('INFO',
                                  "Finishing databases block backup.\n",
                                  config.filelog_fd)
        elif jobs_name in list(file_jobs_dict.keys()):
            log_and_mail.writelog('INFO', "Starting files block backup.",
                                  config.filelog_fd)
            execute_job(jobs_name, file_jobs_dict[jobs_name])
            log_and_mail.writelog('INFO', "Finishing files block backup.\n",
                                  config.filelog_fd)
        else:
            log_and_mail.writelog('INFO', "Starting external block backup.",
                                  config.filelog_fd)
            execute_job(jobs_name, external_jobs_dict[jobs_name])
            log_and_mail.writelog('INFO', "Finishing external block backup.\n",
                                  config.filelog_fd)

    log_and_mail.writelog('INFO', "Stopping script.", config.filelog_fd)