Yedidyah Bar David has uploaded a new change for review. Change subject: packaging: setup: support compressed backup/restore ......................................................................
packaging: setup: support compressed backup/restore Bug-Url: https://bugzilla.redhat.com/1059286 Change-Id: I684f025c9c1d29c701068ba967be5a2115c7a6fb Signed-off-by: Yedidyah Bar David <d...@redhat.com> --- M packaging/setup/ovirt_engine_setup/database.py 1 file changed, 117 insertions(+), 29 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/73/26073/1 diff --git a/packaging/setup/ovirt_engine_setup/database.py b/packaging/setup/ovirt_engine_setup/database.py index f431eac..9b153a2 100644 --- a/packaging/setup/ovirt_engine_setup/database.py +++ b/packaging/setup/ovirt_engine_setup/database.py @@ -21,6 +21,7 @@ import base64 import tempfile import datetime +import subprocess import gettext _ = lambda m: gettext.dgettext(message=m, domain='ovirt-engine-setup') @@ -374,6 +375,7 @@ def detectCommands(self): self.command.detect('pg_dump') self.command.detect('psql') + self.command.detect('bzip2') def createPgPass(self): @@ -650,13 +652,15 @@ self, dir, prefix, + compr='bzip2', + compr_suffix='.bz2', ): fd, backupFile = tempfile.mkstemp( prefix='%s-%s.' % ( prefix, datetime.datetime.now().strftime('%Y%m%d%H%M%S') ), - suffix='.sql', + compr_suffix='.sql%s' % compr_suffix, dir=dir, ) os.close(fd) @@ -668,45 +672,129 @@ file=backupFile, ) ) - self._plugin.execute( - ( - self.command.get('pg_dump'), - '-E', 'UTF8', - '--disable-dollar-quoting', - '--disable-triggers', - '--format=p', - '-U', self.environment[self._dbenvkeys['user']], - '-h', self.environment[self._dbenvkeys['host']], - '-p', str(self.environment[self._dbenvkeys['port']]), - '-f', backupFile, - self.environment[self._dbenvkeys['database']], - ), - envAppend={ + with open(backupFile, 'w') as output_file: + env = os.environ.copy() + env.update({ 'PGPASSWORD': '', 'PGPASSFILE': self.environment[self._dbenvkeys['pgpassfile']], - }, - ) + }) + dump_proc = subprocess.Popen( + args=( + self.command.get('pg_dump'), + '-E', 'UTF8', + '--disable-dollar-quoting', + '--disable-triggers', + '--format=p', + '-U', self.environment[self._dbenvkeys['user']], + '-h', self.environment[self._dbenvkeys['host']], + '-p', str(self.environment[self._dbenvkeys['port']]), + self.environment[self._dbenvkeys['database']], + ), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + close_fds=True, + env=env, + ) + compr_proc = subprocess.Popen( + args=( + compr, + ), + stdin=dump_proc.stdout, + stdout=output_file, + stderr=subprocess.PIPE, + close_fds=True, + ) + dump_stdout, dump_stderr = dump_proc.communicate() + compr_stdout, compr_stderr = compr_proc.communicate() + dump_rc = dump_proc.returncode + compr_rc = compr_proc.returncode + self.logger.debug( + "dump proc: returncode={rc} stderr={stderr}".format( + rc=dump_rc, + stderr=dump_stderr, + ) + ) + self.logger.debug( + "{compr} proc: returncode={rc} stderr={stderr}".format( + compr=compr, + rc=compr_rc, + stderr=compr_stderr, + ) + ) + if (dump_rc != 0) or (compr_rc != 0): + raise RuntimeError( + _('Failed to backup database') + ) return backupFile def restore( self, backupFile, + compr='bzip2', + compr_suffix='.bz2', ): - self._plugin.execute( - ( - self.command.get('psql'), - '-w', - '-h', self.environment[self._dbenvkeys['host']], - '-p', str(self.environment[self._dbenvkeys['port']]), - '-U', self.environment[self._dbenvkeys['user']], - '-d', self.environment[self._dbenvkeys['database']], - '-f', backupFile, - ), - envAppend={ + compressed = backupFile.endswith(compr_suffix) + with open(backupFile, 'r') as input_file: + env = os.environ.copy() + env.update({ 'PGPASSWORD': '', 'PGPASSFILE': self.environment[self._dbenvkeys['pgpassfile']], - }, + }) + + if not compressed: + psql_stdin = input_file + else: + decompr_proc = subprocess.Popen( + args=( + compr, + '-d', + ), + stdin=input_file, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + close_fds=True, + ) + psql_stdin = decompr_proc.stdout + + psql_proc = subprocess.Popen( + args=( + self.command.get('psql'), + '-w', + '-h', self.environment[self._dbenvkeys['host']], + '-p', str(self.environment[self._dbenvkeys['port']]), + '-U', self.environment[self._dbenvkeys['user']], + '-d', self.environment[self._dbenvkeys['database']], + ), + stdin=psql_stdin, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + close_fds=True, + env=env, + ) + psql_stdout, psql_stderr = psql_proc.communicate() + psql_rc = psql_proc.returncode + self.logger.debug( + "psql proc: returncode={rc} stderr={stderr}".format( + rc=psql_rc, + stderr=psql_stderr, + ) + ) + decompr_rc = 0 + if compressed: + decompr_stdout, decompr_stderr = decompr_proc.communicate() + decompr_rc = decompr_proc.returncode + self.logger.debug( + "{compr} -d proc: returncode={rc} stderr={stderr}".format( + compr=compr, + rc=decompr_rc, + stderr=decompr_stderr, + ) + ) + if (psql_rc != 0) or (decompr_rc != 0): + raise RuntimeError( + _('Failed to backup database') + ) ) -- To view, visit http://gerrit.ovirt.org/26073 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I684f025c9c1d29c701068ba967be5a2115c7a6fb Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Yedidyah Bar David <d...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches