コード例 #1
0
ファイル: admin_gui.py プロジェクト: wiz21b/koi
    def set_backup_directory(self):
        dialog = QFileDialog(self)
        dialog.setFileMode(QFileDialog.Directory)
        dialog.setOption(QFileDialog.ShowDirsOnly, True)
        dialog.setWindowTitle("Please select a backup directory")

        if configuration.get("Backup", "backup_directory"):
            dialog.setDirectory(configuration.get("Backup",
                                                  "backup_directory"))

        if dialog.exec_():
            mainlog.debug(dialog.selectedFiles())
            directory = dialog.selectedFiles()[0]

            self._log("Testing the backup directory")
            try:
                f = open(os.path.join(directory, "test_file"), "w")
                f.write("TestBackup")
                f.close()
            except Exception as ex:
                box = QMessageBox(
                    QMessageBox.Warning,
                    "Unable to write into the backup directory",
                    u"I can't write in the backup directory you selected. Have I the necessary permissions  on that directory ({})? The error was : {}"
                    .format(directory, str(ex)))
                box.exec_()
                return

            self.backup_directory_edit.setText(directory)

            configuration.set("Backup", "backup_directory", directory)

            self._log("Saving the backup directory in the configuration")
            configuration.save()

        dialog.close()
コード例 #2
0
ファイル: admin_gui.py プロジェクト: wiz21b/koi
    def restore_backup(self):

        self._clear_log()
        self._log("Restore procedure started")

        url = self.url_edit.text()
        psql_path = configuration.get("Commands", "psql")

        if not psql_path:
            self._log_error(
                "The Commands/psql path is not set in the server.cfg")
            self._log("Please fix the configuration file (on the right)")
            return

        if not configuration.get("Commands", "pg_restore"):
            self._log_error(
                "The Commands/pg_restore path is not set in the server.cfg")
            self._log("Please fix the configuration file (on the right)")
            return

        if not configuration.get("Backup", "backup_directory"):

            self._log(
                "The Backup/backup_directory path is not set in the server.cfg"
            )
            self._log("I'm setting it myself.")

            configuration.set("Backup", "backup_directory", get_data_dir())
            configuration.set("DocumentsDatabase", "documents_root",
                              os.path.join(get_data_dir(), "documents"))
            configuration.save()
            self.edit_config.load_configuration()

        login_clt, password_clt, dummy, dummy, dummy = self._extract_db_params_from_url(
            configuration.get("Database", "url"))
        login_adm, password_adm, dbname, host, port = self._extract_db_params_from_url(
            configuration.get("Database", "admin_url"))

        self._log("{} / {}".format(login_adm, password_adm))

        full_path_backup = None
        d = ""
        if configuration.get("Backup", "backup_directory"):
            d = configuration.get("Backup", "backup_directory")

        if platform.system() == "Windows":

            if configuration.get("Backup", "backup_directory"):
                d = configuration.get("Backup", "backup_directory")

            # Using the static method gives a more native FileDialog.
            # with support for network
            backup_file = QFileDialog.getOpenFileName(
                self, _("Please select a backup file"), d,
                "{} database backup (*.pgbackup)".format(
                    configuration.get("Globals", "name")))[0]

            if not backup_file:
                self._log("Restore aborted")
                return

            full_path_backup = backup_file
            if not os.path.isdir(full_path_backup):
                self._log(
                    "{} is not a directory, so I'll go up a level".format(
                        full_path_backup))
                full_path_backup = os.path.dirname(full_path_backup)

                if not os.path.isdir(full_path_backup):
                    self._log_error(
                        "{} is not a directory either. Aborting restore.".
                        format(full_path_backup))
                    return

        elif platform.system() == "Linux":

            d = AskWindowsShare(None)
            d.exec_()
            if d.result() == QDialog.Accepted:

                # //192.168.0.6/postgresqlbackup

                script_path = "/tmp/horse_mount.sh"
                script = open(script_path, "w")
                script.write("""#!/bin/bash
echo "Creating transfer directory"
mkdir /tmp/backup_win
echo "Unmounting previous transfer directory (can fail)"
umount /tmp/backup_win
echo "Mouting the backup directory"
mount -t cifs -ousername={},password={} {} /tmp/backup_win
                """.format(d.user.text().strip(),
                           d.password.text().strip(),
                           d.address.text().strip()))
                script.close()

                import stat
                os.chmod(script_path,
                         stat.S_IEXEC | stat.S_IWRITE | stat.S_IREAD)

                cmd = [
                    'gksudo', '--sudo-mode', '--message',
                    'Allow Koi to connect to the backup server.', script_path
                ]

                # gksudo seems to like to have the DISPLAY set. So I basically copy
                # it from the calling environment.

                ret, dummy, dummy = self._run_shell(
                    cmd, {'DISPLAY': os.environ.get('DISPLAY')})

                if ret > 0:
                    self._log_error(
                        "The mount operation failed. Please review the parameters you've given."
                    )
                    self._log_error(
                        "Network address : {}, windows user name : {}".format(
                            d.address.text() or "?",
                            d.user.text() or "?"))
                    return

                full_path_backup = "/tmp/backup_win"
            else:
                dialog = QFileDialog(self)
                dialog.setFileMode(QFileDialog.Directory)
                dialog.setNameFilters(['Koi database backup (*.pgbackup)'])
                dialog.setWindowTitle("Please select a backup file")
                if configuration.get("Backup", "backup_directory"):
                    dialog.setDirectory(
                        configuration.get("Backup", "backup_directory"))
                if dialog.exec_():
                    full_path_backup = dialog.selectedFiles()[0]
                else:
                    self._log_error(
                        "Without proper source directory, I can't continue !")
                    return
        else:
            self._log_error("Unsupported operating system")

        # At this poitn full_path_backup is the path to the backup
        # directory of Horse that we want to restore.
        # It is different than the current backup directory.

        if full_path_backup:
            full_restore(configuration, full_path_backup, backup_file, True,
                         mainlog)
            self._log_success("Backup successfully restored !")