Ejemplo n.º 1
0
def wiz_execute(wiz):
    #    Connect
    if wiz.fields["storagetype"].value == _("Local Folder"):
        store = FolderStore("store", 0, False, wiz.fields["folderpath"].value)
    elif wiz.fields["storagetype"].value == _("FTP Server"):
        server = wiz.fields["ftpserver"].value
        login = wiz.fields["ftplogin"].value
        password = wiz.fields["ftppassword"].value
        root = wiz.fields["ftproot"].value
        sftp = wiz.fields["sftp"].value
        store = FTPStore("store", 0, False, server, root, login, password, sftp)
    elif wiz.fields["storagetype"].value == _("DropBox"):
        login = wiz.fields["dblogin"].value
        password = wiz.fields["dbpassword"].value
        root = wiz.fields["dbroot"].value
        store = DropBoxStore("store", 0, False, root, login, password)
    elif wiz.fields["storagetype"].value == _("Server Share"):
        mountcmd = wiz.fields["mountcmd"].value
        umountcmd = wiz.fields["umountcmd"].value
        shareroot = wiz.fields["shareroot"].value
        store = ShareStore("store", 0, False, shareroot, mountcmd, umountcmd)
    else:
        raise Exception("Internal error: bad store type")
    log.debug("Store = ", store)
    
    #    In case we need to revert our config later
    orig_config = Config.get_config()
    try:
        store.connect()

        folders = []
        #    List all backups
        backups = store.list(".")
        for backup in backups:
            #    Make sure its not a system folder
            if backup[0] != "_":
                try:
                    runs = store.list(backup + "/")
                    for run in runs:
                        folders.append((backup, run))
                except:
                    pass

        log.debug("Folders", folders)
        if len(folders) == 0:
            raise Exception(_("There are no backup runs in this store"))
        #    Sort them based on the path name
        folders.sort(key=lambda item: item[1], reverse=True)
        #    Now the first item is the one to use (as it is the most recent).
        backupname, backuprun = folders[0]
        config = os.path.join(backupname, backuprun, const.ConfigName)
        configenc = config + const.EncryptionSuffix
        #    Check if the config exists.
        if store.exists(config):
            src = config
            encrypted = False
        elif store.exists(configenc):
            src = configenc
            encrypted = True
        else:
            raise Exception(_("The backup runs are missing or corrupt (no config files)"))

        if not encrypted:
            store.get(src, const.ConfigDir+os.sep)
        else:
            #    Fetch the file.
            enc_file = const.ConfigFile + const.EncryptionSuffix
            clear_file = const.ConfigFile
            store.get(src, const.ConfigDir+os.sep)

            #    ENCRYPTED
            bad = True  #    keep going until we get a good password
            while bad:
                password = GetPassword(wiz, backupname, backuprun)
                if password == None:
                    #    User is quitting
                    return

                #    Lets check the file is good.
                try:
                    log.debug("Decrypting", enc_file, clear_file, password)
                    ret = cryptor.decrypt_file(password, enc_file, clear_file)
                    log.debug("Return from decrypt", ret)
                    if ret != 0:
                        raise Exception(_("Failed encryption"))
                    bad = False
                    if os.path.exists(enc_file):
                        os.remove(enc_file)
                    #    Looks like this password is a good one. 
                except:
                    log.info("Invalid backup password")
                    dlg.Warn(wiz, _("Invalid password. Please enter the correct backup password."))
                    os.remove(clear_file)
                    #    Revert the old config
                    orig_config.save()


        dlg.Info(wiz, _("Your configuration has been restored.\nClick OK to restart the UI..."), _("Restore"))

        python = sys.executable
        log.debug("Starting:", const.UIProgram)
        subprocess.Popen(const.UIProgram)
        app.quit()

    except Exception as e:
        dlg.Warn(wiz, str(e), _("Configuration restore failed"))
    finally:
        store.disconnect()