2016-05-15 09:29:57 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2022-01-04 02:24:25 -06:00
|
|
|
# Copyright (C) 2013 - 2022, The pgAdmin Development Team
|
2016-05-15 09:29:57 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""Implements Restore Utility"""
|
|
|
|
|
2016-07-26 09:05:14 -05:00
|
|
|
import simplejson as json
|
2016-05-15 09:29:57 -05:00
|
|
|
import os
|
|
|
|
|
|
|
|
from flask import render_template, request, current_app, \
|
|
|
|
url_for, Response
|
2021-11-24 05:52:57 -06:00
|
|
|
from flask_babel import gettext as _
|
2016-07-22 10:25:23 -05:00
|
|
|
from flask_security import login_required, current_user
|
2016-06-21 08:12:14 -05:00
|
|
|
from pgadmin.misc.bgprocess.processes import BatchProcess, IProcessDesc
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
from pgadmin.utils import PgAdminModule, get_storage_directory, html, \
|
2021-04-22 06:59:04 -05:00
|
|
|
fs_short_path, document_dir, does_utility_exist, get_server
|
2016-06-21 08:12:14 -05:00
|
|
|
from pgadmin.utils.ajax import make_json_response, bad_request
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
from config import PG_DEFAULT_DRIVER
|
2021-04-14 01:41:55 -05:00
|
|
|
from pgadmin.model import Server, SharedServer
|
2020-08-20 09:56:51 -05:00
|
|
|
from pgadmin.utils.constants import MIMETYPE_APP_JS
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
# set template path for sql scripts
|
|
|
|
MODULE_NAME = 'restore'
|
|
|
|
server_info = {}
|
|
|
|
|
|
|
|
|
|
|
|
class RestoreModule(PgAdminModule):
|
|
|
|
"""
|
|
|
|
class RestoreModule(Object):
|
|
|
|
|
|
|
|
It is a utility which inherits PgAdminModule
|
|
|
|
class and define methods to load its own
|
|
|
|
javascript file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
LABEL = _('Restore')
|
|
|
|
|
|
|
|
def get_own_javascripts(self):
|
|
|
|
""""
|
|
|
|
Returns:
|
|
|
|
list: js files used by this module
|
|
|
|
"""
|
|
|
|
return [{
|
|
|
|
'name': 'pgadmin.tools.restore',
|
|
|
|
'path': url_for('restore.index') + 'restore',
|
|
|
|
'when': None
|
|
|
|
}]
|
|
|
|
|
2017-06-12 22:36:42 -05:00
|
|
|
def get_exposed_url_endpoints(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: URL endpoints for backup module
|
|
|
|
"""
|
2018-10-22 02:05:21 -05:00
|
|
|
return ['restore.create_job', 'restore.utility_exists']
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2018-01-26 10:54:21 -06:00
|
|
|
|
2016-05-15 09:29:57 -05:00
|
|
|
# Create blueprint for RestoreModule class
|
|
|
|
blueprint = RestoreModule(
|
|
|
|
MODULE_NAME, __name__, static_url_path=''
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class RestoreMessage(IProcessDesc):
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
def __init__(self, _sid, _bfile, *_args):
|
2016-05-15 09:29:57 -05:00
|
|
|
self.sid = _sid
|
|
|
|
self.bfile = _bfile
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
self.cmd = ''
|
|
|
|
|
2020-07-01 03:20:51 -05:00
|
|
|
def cmd_arg(x):
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
if x:
|
|
|
|
x = x.replace('\\', '\\\\')
|
|
|
|
x = x.replace('"', '\\"')
|
|
|
|
x = x.replace('""', '\\"')
|
2018-01-26 10:54:21 -06:00
|
|
|
return ' "' + x + '"'
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
return ''
|
|
|
|
|
|
|
|
for arg in _args:
|
|
|
|
if arg and len(arg) >= 2 and arg[:2] == '--':
|
|
|
|
self.cmd += ' ' + arg
|
|
|
|
else:
|
2020-07-01 03:20:51 -05:00
|
|
|
self.cmd += cmd_arg(arg)
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
|
2018-06-15 05:36:07 -05:00
|
|
|
def get_server_details(self):
|
2021-04-22 06:59:04 -05:00
|
|
|
|
2016-05-15 09:29:57 -05:00
|
|
|
# Fetch the server details like hostname, port, roles etc
|
2021-04-22 06:59:04 -05:00
|
|
|
s = get_server(self.sid)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2018-05-30 20:25:42 -05:00
|
|
|
from pgadmin.utils.driver import get_driver
|
|
|
|
driver = get_driver(PG_DEFAULT_DRIVER)
|
|
|
|
manager = driver.connection_manager(self.sid)
|
|
|
|
|
|
|
|
host = manager.local_bind_host if manager.use_ssh_tunnel else s.host
|
|
|
|
port = manager.local_bind_port if manager.use_ssh_tunnel else s.port
|
|
|
|
|
2018-06-15 05:36:07 -05:00
|
|
|
return s.name, host, port
|
|
|
|
|
|
|
|
@property
|
|
|
|
def message(self):
|
|
|
|
name, host, port = self.get_server_details()
|
|
|
|
|
2019-01-16 00:25:08 -06:00
|
|
|
return _("Restoring backup on the server '{0}'").format(
|
2019-01-24 10:34:18 -06:00
|
|
|
"{0} ({1}:{2})".format(
|
|
|
|
html.safe_str(name),
|
|
|
|
html.safe_str(host),
|
|
|
|
html.safe_str(port)
|
|
|
|
),
|
2016-05-15 09:29:57 -05:00
|
|
|
)
|
|
|
|
|
2019-01-16 00:25:08 -06:00
|
|
|
@property
|
|
|
|
def type_desc(self):
|
|
|
|
return _("Restoring backup on the server")
|
|
|
|
|
2016-05-15 09:29:57 -05:00
|
|
|
def details(self, cmd, args):
|
2018-06-15 05:36:07 -05:00
|
|
|
name, host, port = self.get_server_details()
|
2018-10-10 06:43:26 -05:00
|
|
|
res = '<div>'
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2016-05-16 01:28:36 -05:00
|
|
|
res += html.safe_str(
|
2016-05-15 09:29:57 -05:00
|
|
|
_(
|
2017-04-05 07:50:49 -05:00
|
|
|
"Restoring backup on the server '{0}'..."
|
2016-05-15 09:29:57 -05:00
|
|
|
).format(
|
2018-06-15 05:36:07 -05:00
|
|
|
"{0} ({1}:{2})".format(name, host, port)
|
2016-05-15 09:29:57 -05:00
|
|
|
)
|
2016-05-16 01:28:36 -05:00
|
|
|
)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
Improvement in the look and feel of the whole application
Changed the SCSS/CSS for the below third party libraries to adopt the
new look 'n' feel:
- wcDocker
- Alertify dialogs, and notifications
- AciTree
- Bootstrap Navbar
- Bootstrap Tabs
- Bootstrap Drop-Down menu
- Backgrid
- Select2
Adopated the new the look 'n' feel for the dialogs, wizard, properties,
tab panels, tabs, fieldset, subnode control, spinner control, HTML
table, and other form controls.
- Font is changed to Roboto
- Using SCSS variables to define the look 'n' feel
- Designer background images for the Login, and Forget password pages in
'web' mode
- Improved the look 'n' feel for the key selection in the preferences
dialog
- Table classes consistency changes across the application
- File Open and Save dialog list view changes
Author(s): Aditya Toshniwal & Khushboo Vashi
2018-12-21 05:44:55 -06:00
|
|
|
res += '</div><div class="py-1">'
|
|
|
|
res += _("Running command:")
|
|
|
|
res += '<div class="pg-bg-cmd enable-selection p-1">'
|
|
|
|
res += html.safe_str(cmd + self.cmd)
|
|
|
|
res += '</div></div>'
|
2016-05-15 09:29:57 -05:00
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/")
|
|
|
|
@login_required
|
|
|
|
def index():
|
2017-04-05 07:38:14 -05:00
|
|
|
return bad_request(errormsg=_("This URL cannot be called directly."))
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/restore.js")
|
|
|
|
@login_required
|
|
|
|
def script():
|
|
|
|
"""render own javascript"""
|
|
|
|
return Response(
|
|
|
|
response=render_template(
|
|
|
|
"restore/js/restore.js", _=_
|
|
|
|
),
|
|
|
|
status=200,
|
2020-08-20 09:56:51 -05:00
|
|
|
mimetype=MIMETYPE_APP_JS
|
2016-05-15 09:29:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
def filename_with_file_manager_path(_file):
|
2016-05-15 09:29:57 -05:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
file: File name returned from client file manager
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Filename to use for backup with full path taken from preference
|
|
|
|
"""
|
|
|
|
# Set file manager directory from preference
|
|
|
|
storage_dir = get_storage_directory()
|
|
|
|
|
|
|
|
if storage_dir:
|
2020-08-31 06:15:31 -05:00
|
|
|
_file = os.path.join(storage_dir, _file.lstrip('/').lstrip('\\'))
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
elif not os.path.isabs(_file):
|
|
|
|
_file = os.path.join(document_dir(), _file)
|
|
|
|
|
2018-06-29 09:14:37 -05:00
|
|
|
if not os.path.isfile(_file) and not os.path.exists(_file):
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
return None
|
2016-05-15 09:29:57 -05:00
|
|
|
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
return fs_short_path(_file)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
def _get_create_req_data():
|
2016-05-15 09:29:57 -05:00
|
|
|
"""
|
2020-08-25 07:43:01 -05:00
|
|
|
Get data from request for create restore job.
|
|
|
|
:return: return data if no error occurred.
|
2016-05-15 09:29:57 -05:00
|
|
|
"""
|
|
|
|
if request.form:
|
2019-01-10 03:59:55 -06:00
|
|
|
data = json.loads(request.form['data'], encoding='utf-8')
|
2016-05-15 09:29:57 -05:00
|
|
|
else:
|
2016-07-26 09:05:14 -05:00
|
|
|
data = json.loads(request.data, encoding='utf-8')
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2017-03-09 03:54:55 -06:00
|
|
|
try:
|
|
|
|
_file = filename_with_file_manager_path(data['file'])
|
|
|
|
except Exception as e:
|
2020-08-25 07:43:01 -05:00
|
|
|
return True, bad_request(errormsg=str(e)), data
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
|
|
|
|
if _file is None:
|
2020-08-25 07:43:01 -05:00
|
|
|
return True, make_json_response(
|
2018-06-15 05:36:07 -05:00
|
|
|
status=410,
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
success=0,
|
2017-11-01 10:18:07 -05:00
|
|
|
errormsg=_("File could not be found.")
|
2020-08-25 07:43:01 -05:00
|
|
|
), data, _file
|
|
|
|
|
|
|
|
return False, '', data, _file
|
|
|
|
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
def _connect_server(sid):
|
|
|
|
"""
|
|
|
|
Get server object and try to connect with it.
|
|
|
|
:param sid: Server ID.
|
|
|
|
:return: if not error occurred then return connection data.
|
|
|
|
"""
|
2021-04-22 06:59:04 -05:00
|
|
|
server = get_server(sid)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
if server is None:
|
2022-02-03 06:14:54 -06:00
|
|
|
return True, make_json_response(
|
2016-05-15 09:29:57 -05:00
|
|
|
success=0,
|
2016-06-17 16:05:49 -05:00
|
|
|
errormsg=_("Could not find the specified server.")
|
2022-02-03 06:14:54 -06:00
|
|
|
), None, None, None, None, None
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
# To fetch MetaData for the server
|
|
|
|
from pgadmin.utils.driver import get_driver
|
|
|
|
|
|
|
|
driver = get_driver(PG_DEFAULT_DRIVER)
|
|
|
|
manager = driver.connection_manager(server.id)
|
|
|
|
conn = manager.connection()
|
|
|
|
connected = conn.connected()
|
|
|
|
|
|
|
|
if not connected:
|
2020-08-25 07:43:01 -05:00
|
|
|
return True, make_json_response(
|
2016-05-15 09:29:57 -05:00
|
|
|
success=0,
|
2016-06-17 08:21:14 -05:00
|
|
|
errormsg=_("Please connect to the server first.")
|
2022-02-03 06:14:54 -06:00
|
|
|
), driver, manager, conn, connected, server
|
2020-08-25 07:43:01 -05:00
|
|
|
|
|
|
|
return False, '', driver, manager, conn, connected, server
|
|
|
|
|
|
|
|
|
|
|
|
def set_param(key, param, data, args):
|
|
|
|
"""
|
|
|
|
check and add parameter to args list.
|
|
|
|
:param key: Key.
|
|
|
|
:param param: Parameter to be add in the args list.
|
|
|
|
:param data: Data.
|
|
|
|
:param args: args list.
|
|
|
|
:return: Return true if key in data else return false.
|
|
|
|
"""
|
|
|
|
if key in data and data[key]:
|
|
|
|
args.append(param)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def set_value(key, param, data, args, default_value=None):
|
|
|
|
"""
|
|
|
|
Add values to args list if key not present in data set default value.
|
|
|
|
:param key: Key.
|
|
|
|
:param param: Parameter to be add in the args list.
|
|
|
|
:param data: Data.
|
|
|
|
:param args: args list.
|
|
|
|
:param default_value: default value flag.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if key in data and data[key] is not None and data[key] != '':
|
|
|
|
args.append(param)
|
|
|
|
args.append(data[key])
|
|
|
|
elif default_value is not None:
|
|
|
|
args.append(param)
|
|
|
|
args.append(default_value)
|
|
|
|
|
|
|
|
|
|
|
|
def _set_value_with_schema(data, key, args, param, driver, conn):
|
|
|
|
"""
|
|
|
|
Set value if with_schema flag is true.
|
|
|
|
:param data: Data.
|
|
|
|
:param key: Key.
|
|
|
|
:param args: args list.
|
|
|
|
:param param: parameter to be add in the args list.
|
|
|
|
:param driver: Driver.
|
|
|
|
:param conn: connection.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if isinstance(data[key], list):
|
|
|
|
s, t = data[key]
|
|
|
|
args.extend([
|
|
|
|
param,
|
|
|
|
driver.qtIdent(
|
|
|
|
conn, s
|
|
|
|
) + '.' + driver.qtIdent(conn, t)
|
|
|
|
])
|
|
|
|
else:
|
|
|
|
for s, o in data[key]:
|
|
|
|
args.extend([
|
|
|
|
param,
|
|
|
|
driver.qtIdent(
|
|
|
|
conn, s
|
|
|
|
) + '.' + driver.qtIdent(conn, o)
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
def set_multiple(key, param, data, args, driver, conn, with_schema=True):
|
|
|
|
if key in data and \
|
|
|
|
len(data[key]) > 0:
|
|
|
|
if with_schema:
|
|
|
|
# TODO:// This is temporary
|
|
|
|
# Once object tree is implemented then we will use
|
|
|
|
# list of tuples 'else' part
|
|
|
|
_set_value_with_schema(data, key, args, param, driver, conn)
|
|
|
|
else:
|
|
|
|
for o in data[key]:
|
|
|
|
args.extend([param, o])
|
|
|
|
return True
|
|
|
|
return False
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
def _set_args_param_values(data, manager, server, driver, conn, _file):
|
|
|
|
"""
|
|
|
|
add args to the list.
|
|
|
|
:param data: Data.
|
|
|
|
:param manager: Manager.
|
|
|
|
:param server: Server.
|
|
|
|
:param driver: Driver.
|
|
|
|
:param conn: Connection.
|
|
|
|
:param _file: File.
|
|
|
|
:return: args list.
|
|
|
|
"""
|
2016-05-15 09:29:57 -05:00
|
|
|
args = []
|
|
|
|
|
|
|
|
if 'list' in data:
|
|
|
|
args.append('--list')
|
|
|
|
else:
|
|
|
|
args.extend([
|
2018-05-30 20:25:42 -05:00
|
|
|
'--host',
|
|
|
|
manager.local_bind_host if manager.use_ssh_tunnel else server.host,
|
|
|
|
'--port',
|
|
|
|
str(manager.local_bind_port) if manager.use_ssh_tunnel
|
|
|
|
else str(server.port),
|
2016-05-15 09:29:57 -05:00
|
|
|
'--username', server.username, '--no-password'
|
|
|
|
])
|
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
set_value('role', '--role', data, args)
|
|
|
|
set_value('database', '--dbname', data, args)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
|
|
|
if data['format'] == 'directory':
|
2016-06-17 08:01:30 -05:00
|
|
|
args.extend(['--format=d'])
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
set_param('pre_data', '--section=pre-data', data, args)
|
|
|
|
set_param('data', '--section=data', data, args)
|
|
|
|
set_param('post_data', '--section=post-data', data, args)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
if not set_param('only_data', '--data-only', data, args):
|
|
|
|
set_param('dns_owner', '--no-owner', data, args)
|
|
|
|
set_param('dns_privilege', '--no-privileges', data, args)
|
|
|
|
set_param('dns_tablespace', '--no-tablespaces', data, args)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
if not set_param('only_schema', '--schema-only', data, args):
|
|
|
|
set_param('disable_trigger', '--disable-triggers', data, args)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
set_param('include_create_database', '--create', data, args)
|
|
|
|
set_param('clean', '--clean', data, args)
|
|
|
|
set_param('single_transaction', '--single-transaction', data, args)
|
|
|
|
set_param('no_data_fail_table', '--no-data-for-failed-tables', data,
|
|
|
|
args)
|
|
|
|
set_param('use_set_session_auth', '--use-set-session-authorization',
|
|
|
|
data, args)
|
|
|
|
set_param('exit_on_error', '--exit-on-error', data, args)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2018-08-22 01:47:50 -05:00
|
|
|
if manager.version >= 110000:
|
2020-08-25 07:43:01 -05:00
|
|
|
set_param('no_comments', '--no-comments', data, args)
|
2018-08-22 01:47:50 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
set_value('no_of_jobs', '--jobs', data, args)
|
|
|
|
set_param('verbose', '--verbose', data, args)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
set_multiple('schemas', '--schema', data, args, driver, conn, False)
|
|
|
|
set_multiple('tables', '--table', data, args, driver, conn, False)
|
|
|
|
set_multiple('functions', '--function', data, args, driver, conn,
|
|
|
|
False)
|
|
|
|
set_multiple('triggers', '--trigger', data, args, driver, conn, False)
|
|
|
|
set_multiple('trigger_funcs', '--function', data, args, driver, conn,
|
|
|
|
False)
|
|
|
|
set_multiple('indexes', '--index', data, args, driver, conn, False)
|
2016-05-15 09:29:57 -05:00
|
|
|
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
args.append(fs_short_path(_file))
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2020-08-25 07:43:01 -05:00
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/job/<int:sid>', methods=['POST'], endpoint='create_job')
|
|
|
|
@login_required
|
|
|
|
def create_restore_job(sid):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
sid: Server ID
|
|
|
|
|
|
|
|
Creates a new job for restore task
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
is_error, errmsg, data, _file = _get_create_req_data()
|
|
|
|
if is_error:
|
|
|
|
return errmsg
|
|
|
|
|
|
|
|
is_error, errmsg, driver, manager, conn, \
|
|
|
|
connected, server = _connect_server(sid)
|
|
|
|
if is_error:
|
|
|
|
return errmsg
|
|
|
|
|
|
|
|
utility = manager.utility('restore')
|
|
|
|
ret_val = does_utility_exist(utility)
|
|
|
|
if ret_val:
|
|
|
|
return make_json_response(
|
|
|
|
success=0,
|
|
|
|
errormsg=ret_val
|
|
|
|
)
|
|
|
|
|
|
|
|
args = _set_args_param_values(data, manager, server, driver, conn, _file)
|
|
|
|
|
2016-05-15 09:29:57 -05:00
|
|
|
try:
|
|
|
|
p = BatchProcess(
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
desc=RestoreMessage(
|
2021-04-22 06:59:04 -05:00
|
|
|
server.id,
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
data['file'].encode('utf-8') if hasattr(
|
|
|
|
data['file'], 'encode'
|
|
|
|
) else data['file'],
|
2018-01-26 10:54:21 -06:00
|
|
|
*args
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
),
|
2016-05-15 09:29:57 -05:00
|
|
|
cmd=utility, args=args
|
|
|
|
)
|
2016-05-15 11:59:14 -05:00
|
|
|
manager.export_password_env(p.id)
|
2018-06-19 18:58:46 -05:00
|
|
|
# Check for connection timeout and if it is greater than 0 then
|
|
|
|
# set the environment variable PGCONNECT_TIMEOUT.
|
|
|
|
if manager.connect_timeout > 0:
|
|
|
|
env = dict()
|
|
|
|
env['PGCONNECT_TIMEOUT'] = str(manager.connect_timeout)
|
|
|
|
p.set_env_variables(server, env=env)
|
|
|
|
else:
|
|
|
|
p.set_env_variables(server)
|
|
|
|
|
2016-05-15 09:29:57 -05:00
|
|
|
p.start()
|
|
|
|
jid = p.id
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return make_json_response(
|
|
|
|
status=410,
|
|
|
|
success=0,
|
|
|
|
errormsg=str(e)
|
|
|
|
)
|
|
|
|
# Return response
|
|
|
|
return make_json_response(
|
|
|
|
data={'job_id': jid, 'Success': 1}
|
|
|
|
)
|
|
|
|
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2018-10-22 02:05:21 -05:00
|
|
|
@blueprint.route(
|
|
|
|
'/utility_exists/<int:sid>', endpoint='utility_exists'
|
|
|
|
)
|
|
|
|
@login_required
|
|
|
|
def check_utility_exists(sid):
|
|
|
|
"""
|
|
|
|
This function checks the utility file exist on the given path.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sid: Server ID
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
2021-04-14 01:41:55 -05:00
|
|
|
# Fetch the server details like hostname, port, roles etc
|
2021-04-22 06:59:04 -05:00
|
|
|
server = get_server(sid)
|
2018-10-22 02:05:21 -05:00
|
|
|
|
|
|
|
if server is None:
|
|
|
|
return make_json_response(
|
|
|
|
success=0,
|
|
|
|
errormsg=_("Could not find the specified server.")
|
|
|
|
)
|
|
|
|
|
|
|
|
from pgadmin.utils.driver import get_driver
|
|
|
|
driver = get_driver(PG_DEFAULT_DRIVER)
|
|
|
|
manager = driver.connection_manager(server.id)
|
|
|
|
|
|
|
|
utility = manager.utility('restore')
|
2019-07-12 07:00:23 -05:00
|
|
|
ret_val = does_utility_exist(utility)
|
2018-10-22 02:05:21 -05:00
|
|
|
if ret_val:
|
|
|
|
return make_json_response(
|
|
|
|
success=0,
|
|
|
|
errormsg=ret_val
|
|
|
|
)
|
|
|
|
|
|
|
|
return make_json_response(success=1)
|