Пример #1
0
def main(args=None, app=None):
    '''Main function of qvm-backup-restore'''
    # pylint: disable=too-many-return-statements
    args = parser.parse_args(args, app=app)

    appvm = None
    if args.appvm:
        try:
            appvm = args.app.domains[args.appvm]
        except KeyError:
            parser.error('no such domain: {!r}'.format(args.appvm))

    if args.location_is_service and not args.appvm:
        parser.error('--location-is-service option requires -d')

    if args.paranoid_mode:
        args.dom0_home = False
        args.app.log.info("Starting restore process in a DisposableVM...")
        args.app.log.info("When operation completes, close its window "
                          "manually.")
        restore_in_dispvm = RestoreInDisposableVM(args.app, args)
        try:
            backup_log = restore_in_dispvm.run()
            if args.auto_close:
                print_backup_log(backup_log)
        except qubesadmin.exc.BackupRestoreError as e:
            if e.backup_log is not None:
                print_backup_log(e.backup_log)
            parser.error_runtime(str(e))
            return 1
        except qubesadmin.exc.QubesException as e:
            parser.error_runtime(str(e))
            return 1
        return

    if args.pass_file is not None:
        pass_f = open(args.pass_file) if args.pass_file != "-" else sys.stdin
        passphrase = pass_f.readline().rstrip()
        if pass_f is not sys.stdin:
            pass_f.close()
    else:
        passphrase = getpass.getpass("Please enter the passphrase to verify "
                                     "and (if encrypted) decrypt the backup: ")

    args.app.log.info("Checking backup content...")

    try:
        backup = BackupRestore(args.app,
                               args.backup_location,
                               appvm,
                               passphrase,
                               location_is_service=args.location_is_service,
                               force_compression_filter=args.compression)
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))
        # unreachable - error_runtime will raise SystemExit
        return 1

    backup.options.use_default_template = args.ignore_missing
    backup.options.use_default_netvm = args.ignore_missing
    backup.options.rename_conflicting = args.rename_conflicting
    backup.options.dom0_home = args.dom0_home
    backup.options.ignore_username_mismatch = args.ignore_username_mismatch
    backup.options.ignore_size_limit = args.ignore_size_limit
    backup.options.exclude = args.exclude
    backup.options.verify_only = args.verify_only

    restore_info = None
    try:
        restore_info = backup.get_restore_info()
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))

    if args.vms:
        # use original name here, not renamed
        backup.options.exclude += [
            vm_info.vm.name for vm_info in restore_info.values()
            if vm_info.vm.name not in args.vms
        ]
        restore_info = backup.restore_info_verify(restore_info)

    print(backup.get_restore_summary(restore_info))

    try:
        handle_broken(args.app, args, restore_info)
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))

    if args.pass_file is None:
        if input("Do you want to proceed? [y/N] ").upper() != "Y":
            sys.exit(0)

    try:
        backup.restore_do(restore_info)
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))
def main(args=None, app=None):
    '''Main function of qvm-backup-restore'''
    # pylint: disable=too-many-return-statements
    args = parser.parse_args(args, app=app)

    appvm = None
    if args.appvm:
        try:
            appvm = args.app.domains[args.appvm]
        except KeyError:
            parser.error('no such domain: {!r}'.format(args.appvm))

    if args.pass_file is not None:
        pass_f = open(args.pass_file) if args.pass_file != "-" else sys.stdin
        passphrase = pass_f.readline().rstrip()
        if pass_f is not sys.stdin:
            pass_f.close()
    else:
        passphrase = getpass.getpass("Please enter the passphrase to verify "
                                     "and (if encrypted) decrypt the backup: ")

    args.app.log.info("Checking backup content...")

    try:
        backup = BackupRestore(args.app, args.backup_location, appvm,
                               passphrase)
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))
        # unreachable - error_runtime will raise SystemExit
        return 1

    if args.ignore_missing:
        backup.options.use_default_template = True
        backup.options.use_default_netvm = True
    if args.replace_template:
        backup.options.replace_template = args.replace_template
    if args.rename_conflicting:
        backup.options.rename_conflicting = True
    if not args.dom0_home:
        backup.options.dom0_home = False
    if args.ignore_username_mismatch:
        backup.options.ignore_username_mismatch = True
    if args.ignore_size_limit:
        backup.options.ignore_size_limit = True
    if args.exclude:
        backup.options.exclude = args.exclude
    if args.verify_only:
        backup.options.verify_only = True

    restore_info = None
    try:
        restore_info = backup.get_restore_info()
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))

    if args.vms:
        # use original name here, not renamed
        backup.options.exclude += [
            vm_info.vm.name for vm_info in restore_info.values()
            if vm_info.vm.name not in args.vms
        ]
        restore_info = backup.restore_info_verify(restore_info)

    print(backup.get_restore_summary(restore_info))

    try:
        handle_broken(args.app, args, restore_info)
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))

    if args.pass_file is None:
        if input("Do you want to proceed? [y/N] ").upper() != "Y":
            exit(0)

    try:
        backup.restore_do(restore_info)
    except qubesadmin.exc.QubesException as e:
        parser.error_runtime(str(e))