From e6ee0854b22ec33653d64186f19d909c8e2dfd64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 5 Feb 2009 16:44:38 +0100 Subject: [PATCH] Move dump and restore into specific backend --- trytond/backend/database.py | 20 +++++++ trytond/backend/postgresql/database.py | 72 +++++++++++++++++++++++- trytond/web_service/db.py | 76 ++------------------------ 3 files changed, 97 insertions(+), 71 deletions(-) diff --git a/trytond/backend/database.py b/trytond/backend/database.py index 0fb54e0086..8215d8c209 100644 --- a/trytond/backend/database.py +++ b/trytond/backend/database.py @@ -51,6 +51,26 @@ class DatabaseInterface(object): ''' raise + @staticmethod + def dump(database_name): + ''' + Dump a database + + :param database_name: the database name + :return: the dump + ''' + raise + + @staticmethod + def restore(database_name, data): + ''' + Restore a database + + :param database_name: the database name + :param data: the data + :return: True if succeed + ''' + @staticmethod def list(cursor): ''' diff --git a/trytond/backend/postgresql/database.py b/trytond/backend/postgresql/database.py index 6462780022..3dd15baad3 100644 --- a/trytond/backend/postgresql/database.py +++ b/trytond/backend/postgresql/database.py @@ -63,6 +63,76 @@ class Database(DatabaseInterface): def drop(self, cursor, database_name): cursor.execute('DROP DATABASE "' + database_name + '"') + @staticmethod + def dump(database_name): + from trytond.tools import exec_pg_command_pipe + if CONFIG['db_password']: + raise Exception('Couldn\'t dump database with password!') + cmd = ['pg_dump', '--format=c'] + if CONFIG['db_user']: + cmd.append('--username=' + CONFIG['db_user']) + if CONFIG['db_host']: + cmd.append('--host=' + CONFIG['db_host']) + if CONFIG['db_port']: + cmd.append('--port=' + CONFIG['db_port']) + cmd.append(database_name) + + stdin, stdout = exec_pg_command_pipe(*tuple(cmd)) + stdin.close() + data = stdout.read() + res = stdout.close() + if res: + raise Exception('Couldn\'t dump database!') + return data + + @staticmethod + def restore(database_name, data): + from trytond.tools import exec_pg_command_pipe + if CONFIG['db_password']: + raise Exception('"Couldn\'t restore database with password!') + + database = Database().connect() + cursor = database.cursor(autocommit=True) + database.create(cursor, database_name) + cursor.commit() + cursor.close() + database.close() + + cmd = ['pg_restore'] + if CONFIG['db_user']: + cmd.append('--username=' + CONFIG['db_user']) + if CONFIG['db_host']: + cmd.append('--host=' + CONFIG['db_host']) + if CONFIG['db_port']: + cmd.append('--port=' + CONFIG['db_port']) + cmd.append('--dbname=' + database_name) + args2 = tuple(cmd) + + if os.name == "nt": + tmpfile = (os.environ['TMP'] or 'C:\\') + os.tmpnam() + file(tmpfile, 'wb').write(data) + args2 = list(args2) + args2.append(' ' + tmpfile) + args2 = tuple(args2) + + stdin, stdout = exec_pg_command_pipe(*args2) + if not os.name == "nt": + stdin.write(data) + stdin.close() + res = stdout.close() + if res: + raise Exception('Couldn\'t restore database') + + database = Database(database_name).connect() + cursor = database.cursor() + if not cursor.test(): + cursor.close() + database.close() + raise Exception('Couldn\'t restore database!') + cursor.close() + database.close() + return True + @staticmethod def list(cursor): db_user = CONFIG['db_user'] @@ -75,7 +145,7 @@ class Database(DatabaseInterface): "SELECT datdba " \ "FROM pg_database " \ "WHERE datname = %s)", - (tools.CONFIG["db_name"],)) + (CONFIG["db_name"],)) res = cursor.fetchone() db_user = res and res[0] if db_user: diff --git a/trytond/web_service/db.py b/trytond/web_service/db.py index faf35900d9..3ea5863ede 100644 --- a/trytond/web_service/db.py +++ b/trytond/web_service/db.py @@ -115,83 +115,19 @@ class DB(Service): return True def dump(self, password, db_name): - security.check_super(password) logger = logging.getLogger('web-service') - - if tools.CONFIG['db_password']: - logger.error('DUMP DB: %s doesn\'t work with password' % (db_name,)) - raise Exception, "Couldn't dump database with password" - - cmd = ['pg_dump', '--format=c'] - if tools.CONFIG['db_user']: - cmd.append('--username=' + tools.CONFIG['db_user']) - if tools.CONFIG['db_host']: - cmd.append('--host=' + tools.CONFIG['db_host']) - if tools.CONFIG['db_port']: - cmd.append('--port=' + tools.CONFIG['db_port']) - cmd.append(db_name) - - stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd)) - stdin.close() - data = stdout.read() - res = stdout.close() - if res: - logger.error('DUMP DB: %s failed\n%s' % (db_name, data)) - raise Exception, "Couldn't dump database" + security.check_super(password) + data = Database.dump(db_name) logger.info('DUMP DB: %s' % (db_name)) return base64.encodestring(data) def restore(self, password, db_name, data): - security.check_super(password) logger = logging.getLogger('web-service') - + security.check_super(password) if self.db_exist(db_name): - logger.warning('RESTORE DB: %s already exists' % (db_name,)) - raise Exception, "Database already exists" - - if tools.CONFIG['db_password']: - logger.error( - 'RESTORE DB: %s doesn\'t work with password' % (db_name,)) - raise Exception, "Couldn't restore database with password" - - database = Database().connect() - cursor = database.cursor(autocommit=True) - database.create(cursor, db_name) - cursor.commit() - cursor.close() - database.close() - - cmd = ['pg_restore'] - if tools.CONFIG['db_user']: - cmd.append('--username=' + tools.CONFIG['db_user']) - if tools.CONFIG['db_host']: - cmd.append('--host=' + tools.CONFIG['db_host']) - if tools.CONFIG['db_port']: - cmd.append('--port=' + tools.CONFIG['db_port']) - cmd.append('--dbname=' + db_name) - args2 = tuple(cmd) - - buf = base64.decodestring(data) - if os.name == "nt": - tmpfile = (os.environ['TMP'] or 'C:\\') + os.tmpnam() - file(tmpfile, 'wb').write(buf) - args2 = list(args2) - args2.append(' ' + tmpfile) - args2 = tuple(args2) - stdin, stdout = tools.exec_pg_command_pipe(*args2) - if not os.name == "nt": - stdin.write(base64.decodestring(data)) - stdin.close() - res = stdout.close() - if res: - raise Exception, "Couldn't restore database" - cursor = pooler.get_db_only(db_name, verbose=False).cursor() - if not cursor.test(): - cursor.close() - pooler.close_db(db_name) - raise Exception, "Couldn't restore database" - cursor.close() - pooler.close_db(db_name) + raise Exception("Database already exists!") + data = base64.decodestring(data) + Database.restore(db_name, data) logger.info('RESTORE DB: %s' % (db_name)) return True