2016-05-15 05:29:32 -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 05:29:32 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""Implements Backup Utility"""
|
|
|
|
|
2016-07-26 09:05:14 -05:00
|
|
|
import simplejson as json
|
2016-05-15 05:29:32 -05:00
|
|
|
import os
|
2020-08-25 02:09:14 -05:00
|
|
|
import functools
|
|
|
|
import operator
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2016-05-15 05:29:32 -05:00
|
|
|
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-05-15 05:29:32 -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 05:29:32 -05:00
|
|
|
|
2016-06-21 08:12:14 -05:00
|
|
|
from config import PG_DEFAULT_DRIVER
|
2021-04-14 01:41:55 -05:00
|
|
|
from pgadmin.model import Server, SharedServer
|
2019-10-10 07:28:32 -05:00
|
|
|
from pgadmin.misc.bgprocess import escape_dquotes_process_arg
|
2020-08-20 09:56:51 -05:00
|
|
|
from pgadmin.utils.constants import MIMETYPE_APP_JS
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
# set template path for sql scripts
|
|
|
|
MODULE_NAME = 'backup'
|
|
|
|
server_info = {}
|
|
|
|
|
|
|
|
|
|
|
|
class BackupModule(PgAdminModule):
|
|
|
|
"""
|
|
|
|
class BackupModule(Object):
|
|
|
|
|
|
|
|
It is a utility which inherits PgAdminModule
|
|
|
|
class and define methods to load its own
|
|
|
|
javascript file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
LABEL = _('Backup')
|
|
|
|
|
|
|
|
def get_own_javascripts(self):
|
|
|
|
""""
|
|
|
|
Returns:
|
|
|
|
list: js files used by this module
|
|
|
|
"""
|
|
|
|
return [{
|
|
|
|
'name': 'pgadmin.tools.backup',
|
|
|
|
'path': url_for('backup.index') + 'backup',
|
|
|
|
'when': None
|
|
|
|
}]
|
|
|
|
|
|
|
|
def show_system_objects(self):
|
|
|
|
"""
|
|
|
|
return system preference objects
|
|
|
|
"""
|
|
|
|
return self.pref_show_system_objects
|
|
|
|
|
2017-06-12 22:17:15 -05:00
|
|
|
def get_exposed_url_endpoints(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: URL endpoints for backup module
|
|
|
|
"""
|
2018-10-22 02:05:21 -05:00
|
|
|
return ['backup.create_server_job', 'backup.create_object_job',
|
|
|
|
'backup.utility_exists']
|
2017-06-12 22:17:15 -05:00
|
|
|
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
# Create blueprint for BackupModule class
|
|
|
|
blueprint = BackupModule(
|
|
|
|
MODULE_NAME, __name__, static_url_path=''
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BACKUP(object):
|
|
|
|
"""
|
|
|
|
Constants defined for Backup utilities
|
|
|
|
"""
|
|
|
|
GLOBALS = 1
|
|
|
|
SERVER = 2
|
|
|
|
OBJECT = 3
|
|
|
|
|
|
|
|
|
|
|
|
class BackupMessage(IProcessDesc):
|
|
|
|
"""
|
|
|
|
BackupMessage(IProcessDesc)
|
|
|
|
|
|
|
|
Defines the message shown for the backup operation.
|
|
|
|
"""
|
2016-06-21 08:21:06 -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 __init__(self, _type, _sid, _bfile, *_args, **_kwargs):
|
2016-05-15 05:29:32 -05:00
|
|
|
self.backup_type = _type
|
|
|
|
self.sid = _sid
|
2016-05-15 09:29:57 -05:00
|
|
|
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.database = _kwargs['database'] if 'database' in _kwargs else None
|
|
|
|
self.cmd = ''
|
2020-09-29 04:38:14 -05:00
|
|
|
self.args_str = "{0} ({1}:{2})"
|
2016-05-15 05:29:32 -05:00
|
|
|
|
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('""', '\\"')
|
2017-05-15 07:01:12 -05: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)
|
2016-05-15 05:29:32 -05:00
|
|
|
|
2018-06-15 05:36:07 -05:00
|
|
|
def get_server_details(self):
|
2021-04-22 06:59:04 -05:00
|
|
|
s = get_server(self.sid)
|
2016-05-15 05:29:32 -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
|
|
|
|
|
2019-01-16 00:25:08 -06:00
|
|
|
@property
|
|
|
|
def type_desc(self):
|
|
|
|
if self.backup_type == BACKUP.OBJECT:
|
|
|
|
return _("Backing up an object on the server")
|
|
|
|
if self.backup_type == BACKUP.GLOBALS:
|
|
|
|
return _("Backing up the global objects")
|
|
|
|
elif self.backup_type == BACKUP.SERVER:
|
|
|
|
return _("Backing up the server")
|
|
|
|
else:
|
|
|
|
# It should never reach here.
|
|
|
|
return _("Unknown Backup")
|
|
|
|
|
2018-06-15 05:36:07 -05:00
|
|
|
@property
|
|
|
|
def message(self):
|
|
|
|
name, host, port = self.get_server_details()
|
2019-01-24 10:34:18 -06:00
|
|
|
name = html.safe_str(name)
|
|
|
|
host = html.safe_str(host)
|
|
|
|
port = html.safe_str(port)
|
2018-06-15 05:36:07 -05:00
|
|
|
|
2016-05-15 05:29:32 -05:00
|
|
|
if self.backup_type == BACKUP.OBJECT:
|
|
|
|
return _(
|
2017-05-15 07:01:12 -05:00
|
|
|
"Backing up an object on the server '{0}' "
|
2019-01-16 00:25:08 -06:00
|
|
|
"from database '{1}'"
|
2020-09-29 04:38:14 -05:00
|
|
|
).format(self.args_str.format(name, host, port),
|
|
|
|
html.safe_str(self.database)
|
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
if self.backup_type == BACKUP.GLOBALS:
|
2017-05-15 07:01:12 -05:00
|
|
|
return _("Backing up the global objects on "
|
2019-01-16 00:25:08 -06:00
|
|
|
"the server '{0}'").format(
|
2020-09-29 04:38:14 -05:00
|
|
|
self.args_str.format(
|
2018-06-15 05:36:07 -05:00
|
|
|
name, host, port
|
2017-05-15 07:01:12 -05:00
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
)
|
|
|
|
elif self.backup_type == BACKUP.SERVER:
|
2019-01-16 00:25:08 -06:00
|
|
|
return _("Backing up the server '{0}'").format(
|
2020-09-29 04:38:14 -05:00
|
|
|
self.args_str.format(
|
2018-06-15 05:36:07 -05:00
|
|
|
name, host, port
|
2017-05-15 07:01:12 -05:00
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# It should never reach here.
|
|
|
|
return "Unknown Backup"
|
|
|
|
|
|
|
|
def details(self, cmd, args):
|
2018-06-15 05:36:07 -05:00
|
|
|
name, host, port = self.get_server_details()
|
2018-05-30 20:25:42 -05:00
|
|
|
|
2018-10-10 06:43:26 -05:00
|
|
|
res = '<div>'
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
if self.backup_type == BACKUP.OBJECT:
|
2019-01-24 10:34:18 -06:00
|
|
|
msg = _(
|
2017-05-15 07:01:12 -05:00
|
|
|
"Backing up an object on the server '{0}' "
|
|
|
|
"from database '{1}'..."
|
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
|
|
|
).format(
|
2020-09-29 04:38:14 -05:00
|
|
|
self.args_str.format(
|
2019-01-24 10:34:18 -06:00
|
|
|
name, host, port
|
2017-05-15 07:01:12 -05:00
|
|
|
),
|
2019-01-24 10:34:18 -06:00
|
|
|
self.database
|
2016-05-16 01:28:36 -05:00
|
|
|
)
|
2019-01-24 10:34:18 -06:00
|
|
|
res += html.safe_str(msg)
|
2016-06-17 16:05:49 -05:00
|
|
|
elif self.backup_type == BACKUP.GLOBALS:
|
2019-01-24 10:34:18 -06:00
|
|
|
msg = _("Backing up the global objects on "
|
|
|
|
"the server '{0}'...").format(
|
2020-09-29 04:38:14 -05:00
|
|
|
self.args_str.format(
|
2019-01-24 10:34:18 -06:00
|
|
|
name, host, port
|
2017-05-15 07:01:12 -05:00
|
|
|
)
|
2016-05-16 01:28:36 -05:00
|
|
|
)
|
2019-01-24 10:34:18 -06:00
|
|
|
res += html.safe_str(msg)
|
2016-05-15 05:29:32 -05:00
|
|
|
elif self.backup_type == BACKUP.SERVER:
|
2019-01-24 10:34:18 -06:00
|
|
|
msg = _("Backing up the server '{0}'...").format(
|
2020-09-29 04:38:14 -05:00
|
|
|
self.args_str.format(
|
2019-01-24 10:34:18 -06:00
|
|
|
name, host, port
|
2017-05-15 07:01:12 -05:00
|
|
|
)
|
2016-05-16 01:28:36 -05:00
|
|
|
)
|
2019-01-24 10:34:18 -06:00
|
|
|
res += html.safe_str(msg)
|
2016-05-15 05:29:32 -05:00
|
|
|
else:
|
|
|
|
# It should never reach here.
|
|
|
|
res += "Backup"
|
|
|
|
|
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">'
|
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
|
|
|
res += _("Running command:")
|
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 class="pg-bg-cmd enable-selection p-1">'
|
2018-03-13 15:45:20 -05:00
|
|
|
res += html.safe_str(cmd + self.cmd)
|
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>'
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2018-01-26 10:54:21 -06:00
|
|
|
|
2016-05-15 05:29:32 -05:00
|
|
|
@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 05:29:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/backup.js")
|
|
|
|
@login_required
|
|
|
|
def script():
|
|
|
|
"""render own javascript"""
|
|
|
|
return Response(
|
|
|
|
response=render_template(
|
|
|
|
"backup/js/backup.js", _=_
|
|
|
|
),
|
|
|
|
status=200,
|
2020-08-20 09:56:51 -05:00
|
|
|
mimetype=MIMETYPE_APP_JS
|
2016-05-15 05:29:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-06-29 09:14:37 -05:00
|
|
|
def filename_with_file_manager_path(_file, create_file=True):
|
2016-05-15 05:29:32 -05:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
file: File name returned from client file manager
|
2018-06-29 09:14:37 -05:00
|
|
|
create_file: Set flag to False when file creation doesn't required
|
2016-05-15 05:29:32 -05:00
|
|
|
Returns:
|
|
|
|
Filename to use for backup with full path taken from preference
|
|
|
|
"""
|
|
|
|
# Set file manager directory from preference
|
2016-05-15 09:29:57 -05:00
|
|
|
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)
|
|
|
|
|
2020-06-26 02:48:27 -05:00
|
|
|
def short_filepath():
|
|
|
|
short_path = fs_short_path(_file)
|
|
|
|
# fs_short_path() function may return empty path on Windows
|
|
|
|
# if directory doesn't exists. In that case we strip the last path
|
|
|
|
# component and get the short path.
|
|
|
|
if os.name == 'nt' and short_path == '':
|
|
|
|
base_name = os.path.basename(_file)
|
|
|
|
dir_name = os.path.dirname(_file)
|
|
|
|
short_path = fs_short_path(dir_name) + '\\' + base_name
|
|
|
|
return short_path
|
|
|
|
|
2018-06-29 09:14:37 -05:00
|
|
|
if create_file:
|
|
|
|
# Touch the file to get the short path of the file on windows.
|
|
|
|
with open(_file, 'a'):
|
2020-06-26 02:48:27 -05:00
|
|
|
return short_filepath()
|
2018-07-17 07:04:28 -05:00
|
|
|
|
2020-06-26 02:48:27 -05:00
|
|
|
return short_filepath()
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
|
2020-08-25 02:09:14 -05:00
|
|
|
def _get_args_params_values(data, conn, backup_obj_type, backup_file, server,
|
|
|
|
manager):
|
|
|
|
"""
|
|
|
|
Used internally by create_backup_objects_job. This function will create
|
|
|
|
the required args and params for the job.
|
|
|
|
:param data: input data
|
|
|
|
:param conn: connection obj
|
|
|
|
:param backup_obj_type: object type
|
|
|
|
:param backup_file: file name
|
|
|
|
:param server: server obj
|
|
|
|
:param manager: connection manager
|
|
|
|
:return: args array
|
|
|
|
"""
|
|
|
|
from pgadmin.utils.driver import get_driver
|
|
|
|
driver = get_driver(PG_DEFAULT_DRIVER)
|
|
|
|
|
|
|
|
host, port = (manager.local_bind_host, str(manager.local_bind_port)) \
|
|
|
|
if manager.use_ssh_tunnel else (server.host, str(server.port))
|
|
|
|
args = [
|
|
|
|
'--file',
|
|
|
|
backup_file,
|
|
|
|
'--host',
|
|
|
|
host,
|
|
|
|
'--port',
|
|
|
|
port,
|
|
|
|
'--username',
|
|
|
|
server.username,
|
|
|
|
'--no-password'
|
|
|
|
]
|
|
|
|
|
|
|
|
def set_param(key, param, assertion=True):
|
|
|
|
if not assertion:
|
|
|
|
return
|
|
|
|
if data.get(key, None):
|
|
|
|
args.append(param)
|
|
|
|
|
|
|
|
def set_value(key, param, default_value=None, assertion=True):
|
|
|
|
if not assertion:
|
|
|
|
return
|
|
|
|
val = data.get(key, default_value)
|
|
|
|
if val:
|
|
|
|
args.append(param)
|
|
|
|
args.append(val)
|
|
|
|
|
|
|
|
if backup_obj_type != 'objects':
|
|
|
|
args.append('--database')
|
|
|
|
args.append(server.maintenance_db)
|
|
|
|
|
|
|
|
if backup_obj_type == 'globals':
|
|
|
|
args.append('--globals-only')
|
|
|
|
|
|
|
|
set_param('verbose', '--verbose')
|
|
|
|
set_param('dqoute', '--quote-all-identifiers')
|
|
|
|
set_value('role', '--role')
|
|
|
|
|
|
|
|
if backup_obj_type == 'objects' and data.get('format', None):
|
|
|
|
args.extend(['--format={0}'.format({
|
|
|
|
'custom': 'c',
|
|
|
|
'tar': 't',
|
|
|
|
'plain': 'p',
|
|
|
|
'directory': 'd'
|
|
|
|
}[data['format']])])
|
|
|
|
|
|
|
|
set_param('blobs', '--blobs', data['format'] in ['custom', 'tar'])
|
|
|
|
set_value('ratio', '--compress', None,
|
|
|
|
['custom', 'plain', 'directory'])
|
|
|
|
|
|
|
|
set_param('only_data', '--data-only',
|
|
|
|
data.get('only_data', None))
|
|
|
|
set_param('disable_trigger', '--disable-triggers',
|
|
|
|
data.get('only_data', None) and
|
|
|
|
data.get('format', '') == 'plain')
|
|
|
|
|
|
|
|
set_param('only_schema', '--schema-only',
|
|
|
|
data.get('only_schema', None) and
|
|
|
|
not data.get('only_data', None))
|
|
|
|
|
|
|
|
set_param('dns_owner', '--no-owner')
|
|
|
|
set_param('include_create_database', '--create')
|
|
|
|
set_param('include_drop_database', '--clean')
|
|
|
|
set_param('pre_data', '--section=pre-data')
|
|
|
|
set_param('data', '--section=data')
|
|
|
|
set_param('post_data', '--section=post-data')
|
|
|
|
set_param('dns_privilege', '--no-privileges')
|
|
|
|
set_param('dns_tablespace', '--no-tablespaces')
|
|
|
|
set_param('dns_unlogged_tbl_data', '--no-unlogged-table-data')
|
|
|
|
set_param('use_insert_commands', '--inserts')
|
|
|
|
set_param('use_column_inserts', '--column-inserts')
|
|
|
|
set_param('disable_quoting', '--disable-dollar-quoting')
|
|
|
|
set_param('with_oids', '--oids')
|
|
|
|
set_param('use_set_session_auth', '--use-set-session-authorization')
|
|
|
|
|
|
|
|
set_param('no_comments', '--no-comments', manager.version >= 110000)
|
|
|
|
set_param('load_via_partition_root', '--load-via-partition-root',
|
|
|
|
manager.version >= 110000)
|
|
|
|
|
|
|
|
set_value('encoding', '--encoding')
|
|
|
|
set_value('no_of_jobs', '--jobs')
|
|
|
|
|
|
|
|
args.extend(
|
|
|
|
functools.reduce(operator.iconcat, map(
|
|
|
|
lambda s: ['--schema', r'{0}'.format(driver.qtIdent(conn, s).
|
|
|
|
replace('"', '\"'))],
|
|
|
|
data.get('schemas', [])), []
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
args.extend(
|
|
|
|
functools.reduce(operator.iconcat, map(
|
2020-08-25 07:36:38 -05:00
|
|
|
lambda t: ['--table',
|
|
|
|
r'{0}'.format(driver.qtIdent(conn, t[0], t[1])
|
|
|
|
.replace('"', '\"'))],
|
2020-08-25 02:09:14 -05:00
|
|
|
data.get('tables', [])), []
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
2017-06-12 22:17:15 -05:00
|
|
|
@blueprint.route(
|
|
|
|
'/job/<int:sid>', methods=['POST'], endpoint='create_server_job'
|
|
|
|
)
|
|
|
|
@blueprint.route(
|
|
|
|
'/job/<int:sid>/object', methods=['POST'], endpoint='create_object_job'
|
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
@login_required
|
|
|
|
def create_backup_objects_job(sid):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
sid: Server ID
|
|
|
|
|
2018-01-26 10:54:21 -06:00
|
|
|
Creates a new job for backup task
|
|
|
|
(Backup Database(s)/Schema(s)/Table(s))
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
|
2020-08-25 02:09:14 -05:00
|
|
|
data = json.loads(request.data, encoding='utf-8')
|
|
|
|
backup_obj_type = data.get('type', 'objects')
|
2017-08-17 06:05:42 -05:00
|
|
|
|
2017-03-09 03:54:55 -06:00
|
|
|
try:
|
2020-08-25 02:09:14 -05:00
|
|
|
backup_file = filename_with_file_manager_path(
|
|
|
|
data['file'], (data.get('format', '') != 'directory'))
|
2017-03-09 03:54:55 -06:00
|
|
|
except Exception as e:
|
|
|
|
return bad_request(errormsg=str(e))
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
# Fetch the server details like hostname, port, roles etc
|
2021-04-22 06:59:04 -05:00
|
|
|
server = get_server(sid)
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
if server is None:
|
|
|
|
return make_json_response(
|
|
|
|
success=0,
|
2016-06-17 08:21:14 -05:00
|
|
|
errormsg=_("Could not find the specified server.")
|
2016-05-15 05:29:32 -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:
|
|
|
|
return make_json_response(
|
|
|
|
success=0,
|
2016-06-17 08:21:14 -05:00
|
|
|
errormsg=_("Please connect to the server first.")
|
2016-05-15 05:29:32 -05:00
|
|
|
)
|
|
|
|
|
2018-08-22 01:47:50 -05:00
|
|
|
utility = manager.utility('backup') if backup_obj_type == 'objects' \
|
|
|
|
else manager.utility('backup_server')
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2020-08-25 02:09:14 -05:00
|
|
|
args = _get_args_params_values(
|
|
|
|
data, conn, backup_obj_type, backup_file, server, manager)
|
2016-05-15 05:29:32 -05:00
|
|
|
|
2019-10-10 07:28:32 -05:00
|
|
|
escaped_args = [
|
|
|
|
escape_dquotes_process_arg(arg) for arg in args
|
|
|
|
]
|
2016-05-15 05:29:32 -05:00
|
|
|
try:
|
2020-08-25 02:09:14 -05:00
|
|
|
bfile = data['file'].encode('utf-8') \
|
|
|
|
if hasattr(data['file'], 'encode') else data['file']
|
2018-08-22 01:47:50 -05:00
|
|
|
if backup_obj_type == 'objects':
|
|
|
|
args.append(data['database'])
|
2019-10-10 07:28:32 -05:00
|
|
|
escaped_args.append(data['database'])
|
2018-08-22 01:47:50 -05:00
|
|
|
p = BatchProcess(
|
|
|
|
desc=BackupMessage(
|
2021-04-22 06:59:04 -05:00
|
|
|
BACKUP.OBJECT, server.id, bfile,
|
2018-08-22 01:47:50 -05:00
|
|
|
*args,
|
|
|
|
database=data['database']
|
|
|
|
),
|
2019-10-10 07:28:32 -05:00
|
|
|
cmd=utility, args=escaped_args
|
2018-08-22 01:47:50 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
p = BatchProcess(
|
|
|
|
desc=BackupMessage(
|
|
|
|
BACKUP.SERVER if backup_obj_type != 'globals'
|
|
|
|
else BACKUP.GLOBALS,
|
2021-04-22 06:59:04 -05:00
|
|
|
server.id, bfile,
|
2018-08-22 01:47:50 -05:00
|
|
|
*args
|
|
|
|
),
|
2019-10-10 07:28:32 -05:00
|
|
|
cmd=utility, args=escaped_args
|
2018-08-22 01:47:50 -05:00
|
|
|
)
|
|
|
|
|
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 05:29:32 -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}
|
|
|
|
)
|
2018-10-22 02:05:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route(
|
|
|
|
'/utility_exists/<int:sid>/<backup_obj_type>', endpoint='utility_exists'
|
|
|
|
)
|
|
|
|
@login_required
|
|
|
|
def check_utility_exists(sid, backup_obj_type):
|
|
|
|
"""
|
|
|
|
This function checks the utility file exist on the given path.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sid: Server ID
|
|
|
|
backup_obj_type: Type of the object
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
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('backup') if backup_obj_type == 'objects' \
|
|
|
|
else manager.utility('backup_server')
|
|
|
|
|
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)
|