2016-05-15 05:29:32 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2018-01-05 04:42:49 -06:00
|
|
|
# Copyright (C) 2013 - 2018, The pgAdmin Development Team
|
2016-05-15 05:29:32 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""Implements Backup Utility"""
|
|
|
|
|
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 __future__ import unicode_literals
|
2016-07-26 09:05:14 -05:00
|
|
|
import simplejson as json
|
2016-05-15 05:29:32 -05:00
|
|
|
import os
|
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
|
2016-07-22 10:25:23 -05:00
|
|
|
from flask_babel import gettext as _
|
|
|
|
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, \
|
|
|
|
fs_short_path, document_dir
|
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
|
|
|
|
from pgadmin.model import Server
|
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
|
|
|
|
"""
|
|
|
|
return ['backup.create_server_job', 'backup.create_object_job']
|
|
|
|
|
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 = ''
|
2016-05-15 05:29:32 -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 cmdArg(x):
|
|
|
|
if x:
|
|
|
|
# x = html.safe_str(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:
|
|
|
|
self.cmd += cmdArg(arg)
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def message(self):
|
|
|
|
# Fetch the server details like hostname, port, roles etc
|
|
|
|
s = Server.query.filter_by(
|
|
|
|
id=self.sid, user_id=current_user.id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if self.backup_type == BACKUP.OBJECT:
|
|
|
|
return _(
|
2017-05-15 07:01:12 -05:00
|
|
|
"Backing up an object on the server '{0}' "
|
|
|
|
"from database '{1}'..."
|
2016-05-15 05:29:32 -05:00
|
|
|
).format(
|
2017-05-15 07:01:12 -05:00
|
|
|
"{0} ({1}:{2})".format(
|
|
|
|
s.name, s.host, s.port
|
|
|
|
),
|
2016-05-15 05:29:32 -05:00
|
|
|
self.database
|
|
|
|
)
|
|
|
|
if self.backup_type == BACKUP.GLOBALS:
|
2017-05-15 07:01:12 -05:00
|
|
|
return _("Backing up the global objects on "
|
|
|
|
"the server '{0}'...").format(
|
|
|
|
"{0} ({1}:{2})".format(
|
|
|
|
s.name, s.host, s.port
|
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
)
|
|
|
|
elif self.backup_type == BACKUP.SERVER:
|
2016-06-17 08:21:14 -05:00
|
|
|
return _("Backing up the server '{0}'...").format(
|
2017-05-15 07:01:12 -05:00
|
|
|
"{0} ({1}:{2})".format(
|
|
|
|
s.name, s.host, s.port
|
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# It should never reach here.
|
|
|
|
return "Unknown Backup"
|
|
|
|
|
|
|
|
def details(self, cmd, args):
|
|
|
|
# Fetch the server details like hostname, port, roles etc
|
|
|
|
s = Server.query.filter_by(
|
|
|
|
id=self.sid, user_id=current_user.id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
res = '<div class="h5">'
|
|
|
|
|
|
|
|
if self.backup_type == BACKUP.OBJECT:
|
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 += _(
|
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(
|
2017-05-15 07:01:12 -05:00
|
|
|
"{0} ({1}:{2})".format(
|
|
|
|
html.safe_str(s.name),
|
|
|
|
html.safe_str(s.host),
|
|
|
|
html.safe_str(s.port),
|
|
|
|
),
|
|
|
|
html.safe_str(self.database)
|
2016-05-16 01:28:36 -05:00
|
|
|
)
|
2016-06-17 16:05:49 -05:00
|
|
|
elif self.backup_type == BACKUP.GLOBALS:
|
2017-05-15 07:01:12 -05:00
|
|
|
res += _("Backing up the global objects on "
|
|
|
|
"the server '{0}'...").format(
|
|
|
|
"{0} ({1}:{2})".format(
|
|
|
|
html.safe_str(s.name),
|
|
|
|
html.safe_str(s.host),
|
|
|
|
html.safe_str(s.port)
|
|
|
|
)
|
2016-05-16 01:28:36 -05:00
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
elif self.backup_type == BACKUP.SERVER:
|
2017-04-05 07:50:49 -05:00
|
|
|
res += _("Backing up the server '{0}'...").format(
|
2017-05-15 07:01:12 -05:00
|
|
|
"{0} ({1}:{2})".format(
|
|
|
|
html.safe_str(s.name),
|
|
|
|
html.safe_str(s.host),
|
|
|
|
html.safe_str(s.port)
|
|
|
|
)
|
2016-05-16 01:28:36 -05:00
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
else:
|
|
|
|
# It should never reach here.
|
|
|
|
res += "Backup"
|
|
|
|
|
|
|
|
res += '</div><div class="h5">'
|
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:")
|
2016-11-18 06:05:19 -06:00
|
|
|
res += '</b><br><span class="pg-bg-cmd enable-selection">'
|
2017-05-15 07:01:12 -05:00
|
|
|
res += html.safe_str(self.cmd)
|
2016-11-18 06:05:19 -06:00
|
|
|
res += '</span></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,
|
|
|
|
mimetype="application/javascript"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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 05:29:32 -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
|
2016-05-15 09:29:57 -05:00
|
|
|
storage_dir = get_storage_directory()
|
|
|
|
|
|
|
|
if storage_dir:
|
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
|
|
|
_file = os.path.join(storage_dir, _file.lstrip(u'/').lstrip(u'\\'))
|
|
|
|
elif not os.path.isabs(_file):
|
|
|
|
_file = os.path.join(document_dir(), _file)
|
|
|
|
|
|
|
|
# Touch the file to get the short path of the file on windows.
|
|
|
|
with open(_file, 'a'):
|
|
|
|
pass
|
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 05:29:32 -05:00
|
|
|
|
|
|
|
|
2017-06-12 22:17:15 -05:00
|
|
|
@blueprint.route(
|
|
|
|
'/job/<int:sid>', methods=['POST'], endpoint='create_server_job'
|
|
|
|
)
|
2016-05-15 05:29:32 -05:00
|
|
|
@login_required
|
|
|
|
def create_backup_job(sid):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
sid: Server ID
|
|
|
|
|
|
|
|
Creates a new job for backup task (Backup Server/Globals)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
if request.form:
|
|
|
|
# Convert ImmutableDict to dict
|
|
|
|
data = dict(request.form)
|
2016-07-26 09:05:14 -05:00
|
|
|
data = json.loads(data['data'][0], encoding='utf-8')
|
2016-05-15 05:29:32 -05:00
|
|
|
else:
|
2016-07-26 09:05:14 -05:00
|
|
|
data = json.loads(request.data, encoding='utf-8')
|
2016-05-15 05:29:32 -05:00
|
|
|
|
2017-03-09 03:54:55 -06:00
|
|
|
try:
|
|
|
|
backup_file = filename_with_file_manager_path(data['file'])
|
|
|
|
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
|
|
|
|
server = Server.query.filter_by(
|
|
|
|
id=sid, user_id=current_user.id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
utility = manager.utility('backup_server')
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'--file',
|
2016-05-15 09:29:57 -05:00
|
|
|
backup_file,
|
2016-05-15 05:29:32 -05:00
|
|
|
'--host',
|
|
|
|
server.host,
|
|
|
|
'--port',
|
|
|
|
str(server.port),
|
|
|
|
'--username',
|
|
|
|
server.username,
|
|
|
|
'--no-password',
|
|
|
|
'--database',
|
2016-10-21 08:57:19 -05:00
|
|
|
server.maintenance_db
|
2016-05-15 05:29:32 -05:00
|
|
|
]
|
|
|
|
if 'role' in data and data['role']:
|
|
|
|
args.append('--role')
|
|
|
|
args.append(data['role'])
|
|
|
|
if 'verbose' in data and data['verbose']:
|
|
|
|
args.append('--verbose')
|
|
|
|
if 'dqoute' in data and data['dqoute']:
|
|
|
|
args.append('--quote-all-identifiers')
|
|
|
|
if data['type'] == 'global':
|
|
|
|
args.append('--globals-only')
|
|
|
|
|
|
|
|
try:
|
|
|
|
p = BatchProcess(
|
|
|
|
desc=BackupMessage(
|
|
|
|
BACKUP.SERVER if data['type'] != 'global' else BACKUP.GLOBALS,
|
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
|
|
|
sid,
|
|
|
|
data['file'].encode('utf-8') if hasattr(
|
|
|
|
data['file'], 'encode'
|
|
|
|
) else data['file'],
|
|
|
|
*args
|
2016-05-15 05:29:32 -05:00
|
|
|
),
|
|
|
|
cmd=utility, args=args
|
|
|
|
)
|
2016-05-15 11:59:14 -05:00
|
|
|
manager.export_password_env(p.id)
|
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}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-06-12 22:17:15 -05:00
|
|
|
@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
|
|
|
|
"""
|
|
|
|
if request.form:
|
|
|
|
# Convert ImmutableDict to dict
|
|
|
|
data = dict(request.form)
|
2016-07-26 09:05:14 -05:00
|
|
|
data = json.loads(data['data'][0], encoding='utf-8')
|
2016-05-15 05:29:32 -05:00
|
|
|
else:
|
2016-07-26 09:05:14 -05:00
|
|
|
data = json.loads(request.data, encoding='utf-8')
|
2016-05-15 05:29:32 -05:00
|
|
|
|
2017-08-17 06:05:42 -05:00
|
|
|
# Remove ratio from data in case of empty string
|
|
|
|
if 'ratio' in data and data['ratio'] == '':
|
|
|
|
data.pop("ratio")
|
|
|
|
|
2017-03-09 03:54:55 -06:00
|
|
|
try:
|
|
|
|
backup_file = filename_with_file_manager_path(data['file'])
|
|
|
|
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
|
|
|
|
server = Server.query.filter_by(
|
|
|
|
id=sid, user_id=current_user.id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
utility = manager.utility('backup')
|
|
|
|
args = [
|
|
|
|
'--file',
|
2016-05-15 09:29:57 -05:00
|
|
|
backup_file,
|
2016-05-15 05:29:32 -05:00
|
|
|
'--host',
|
|
|
|
server.host,
|
|
|
|
'--port',
|
|
|
|
str(server.port),
|
|
|
|
'--username',
|
|
|
|
server.username,
|
|
|
|
'--no-password'
|
|
|
|
]
|
|
|
|
|
|
|
|
def set_param(key, param):
|
2016-05-15 09:29:57 -05:00
|
|
|
if key in data and data[key]:
|
2016-05-15 05:29:32 -05:00
|
|
|
args.append(param)
|
|
|
|
|
|
|
|
def set_value(key, param, value):
|
|
|
|
if key in data:
|
|
|
|
if value:
|
2016-06-21 03:17:13 -05:00
|
|
|
if value is True and data[key]:
|
2016-06-17 08:01:30 -05:00
|
|
|
args.append(param)
|
2016-06-21 03:17:13 -05:00
|
|
|
args.append(data[key])
|
2016-05-15 05:29:32 -05:00
|
|
|
else:
|
2016-06-17 08:01:30 -05:00
|
|
|
args.append(param)
|
2016-05-15 05:29:32 -05:00
|
|
|
args.append(value)
|
|
|
|
|
|
|
|
set_param('verbose', '--verbose')
|
|
|
|
set_param('dqoute', '--quote-all-identifiers')
|
2016-06-17 08:01:30 -05:00
|
|
|
set_value('role', '--role', True)
|
2016-05-15 05:29:32 -05:00
|
|
|
if data['format'] is not None:
|
|
|
|
if data['format'] == 'custom':
|
2016-06-17 08:01:30 -05:00
|
|
|
args.extend(['--format=c'])
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
set_param('blobs', '--blobs')
|
|
|
|
set_value('ratio', '--compress', True)
|
|
|
|
|
|
|
|
elif data['format'] == 'tar':
|
2016-06-17 08:01:30 -05:00
|
|
|
args.extend(['--format=t'])
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
set_param('blobs', '--blobs')
|
|
|
|
|
|
|
|
elif data['format'] == 'plain':
|
2016-06-17 08:01:30 -05:00
|
|
|
args.extend(['--format=p'])
|
2016-05-24 01:32:46 -05:00
|
|
|
if 'only_data' in data and data['only_data']:
|
2016-05-15 05:29:32 -05:00
|
|
|
args.append('--data-only')
|
|
|
|
set_param('disable_trigger', '--disable-triggers')
|
|
|
|
else:
|
|
|
|
set_param('only_schema', '--schema-only')
|
|
|
|
set_param('dns_owner', '--no-owner')
|
|
|
|
set_param('include_create_database', '--create')
|
|
|
|
set_param('include_drop_database', '--clean')
|
|
|
|
elif data['format'] == 'directory':
|
2016-06-17 08:01:30 -05:00
|
|
|
args.extend(['--format=d'])
|
2016-05-15 05:29:32 -05:00
|
|
|
|
2016-06-17 08:01:30 -05:00
|
|
|
set_param('pre_data', '--section=pre-data')
|
|
|
|
set_param('data', '--section=data')
|
|
|
|
set_param('post_data', '--section=post-data')
|
2016-05-15 05:29:32 -05:00
|
|
|
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')
|
|
|
|
|
2016-06-17 08:01:30 -05:00
|
|
|
set_value('encoding', '--encoding', True)
|
2016-05-15 05:29:32 -05:00
|
|
|
set_value('no_of_jobs', '--jobs', True)
|
|
|
|
|
|
|
|
for s in data['schemas']:
|
2016-10-21 08:57:19 -05:00
|
|
|
args.extend(['--schema', s])
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
for s, t in data['tables']:
|
|
|
|
args.extend([
|
2016-05-24 01:39:25 -05:00
|
|
|
'--table', driver.qtIdent(conn, s, t)
|
2016-05-15 05:29:32 -05:00
|
|
|
])
|
|
|
|
|
2016-10-21 08:57:19 -05:00
|
|
|
args.append(data['database'])
|
2016-05-15 05:29:32 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
p = BatchProcess(
|
|
|
|
desc=BackupMessage(
|
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
|
|
|
BACKUP.OBJECT, sid,
|
|
|
|
data['file'].encode('utf-8') if hasattr(
|
|
|
|
data['file'], 'encode'
|
|
|
|
) else data['file'],
|
|
|
|
*args,
|
|
|
|
database=data['database']
|
2016-05-15 05:29:32 -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
|
|
|
cmd=utility, args=args
|
|
|
|
)
|
2016-05-15 11:59:14 -05:00
|
|
|
manager.export_password_env(p.id)
|
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}
|
|
|
|
)
|