Ejemplo n.º 1
0
def list_sections(message):
    """
    Print defined message and defined backup sections on command line.

    :param message:
        message to be displayed on command line
    :type settings:
        String

    :rtype:
        None

    """
    print()
    print(message)
    print()

    # loop over defined sections
    for section in settings.sections():
        print(' * ' + section)

    print()
Ejemplo n.º 2
0
def parse_command_line(settings):
    """
    Parse command line.

    :param settings:
        backup settings and application information
    :type settings:
        lalikan.settings

    :returns:
        selected backup section (**None** for all sections) and
        whether a backup should be forced
    :rtype:
        tuple(section, force_backup)

    """
    # initialise command line parser
    parser = argparse.ArgumentParser(
        description=settings.get_description(),
        formatter_class=argparse.RawDescriptionHelpFormatter)

    # argument: show version information
    parser.add_argument(
        '--version',
        action='version',
        version=settings.get_name_and_version())

    # argument: show copyright and licence information
    parser.add_argument(
        '--licence',
        action='store_true',
        dest='licence',
        default=False,
        help='show copyright and licence information and exit')

    # argument: list all backup sections
    parser.add_argument(
        '-l', '--list',
        action='store_true',
        dest='list_sections',
        default=False,
        help='list all defined backup sections and exit')

    # argument: create backup for section only
    parser.add_argument(
        '-s', '--section',
        action='store',
        dest='section',
        metavar='SECTION',
        default=None,
        help='create backup for SECTION (otherwise, backups for all '
             'sections will be created)')

    # argument: force backup
    parser.add_argument(
        '--force',
        action='store_true',
        dest='force_backup',
        default=False,
        help='force backup')

    # parse command line
    args = parser.parse_args()

    # show copyright and licence information
    if args.licence:
        # get application name and version
        name_and_version = settings.get_name_and_version()

        print()
        print(name_and_version)
        print('=' * len(name_and_version))

        print(settings.get_description())
        print()

        print(settings.get_copyrights())
        print()

        print(settings.get_license(True))
        print()

        exit(0)

    # list defined sections and exit
    if args.list_sections:
        list_sections('Backup sections:')
        exit(0)

    # user asked to create a specific backup, ...
    if args.section is not None:
        # ..., but the specified backup section does not exist
        if args.section not in settings.sections():
            # print error message, list defined sections and exit
            message = 'Could not find section "{}".  '.format(args.section)
            message += 'Please use one of these sections:'

            list_sections(message)
            exit(1)

    return (args.section, args.force_backup)
Ejemplo n.º 3
0
        print()

        exit(1)

    # load Lalikan settings
    settings = lalikan.settings.Settings('/etc/lalikan')

    # parse command line
    section, force_backup = parse_command_line(settings)

    # print application name and version
    print_header()

    # create backup for all sections
    if section is None:
        sections = settings.sections()
    # create backup for specified section only
    else:
        sections = [section]

    # keep track of backup errors
    errors_occurred = False

    # loop over specified backup sections
    for n in range(len(sections)):
        try:
            # create backup for section
            lalikan.runner.BackupRunner(settings, sections[n], force_backup)
        except OSError as err:
            # print error message
            print(err)