Move dump and restore into specific backend

This commit is contained in:
Cédric Krier
2009-02-05 16:44:38 +01:00
parent 101c60c31a
commit e6ee0854b2
3 changed files with 97 additions and 71 deletions
+20
View File
@@ -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):
'''
+71 -1
View File
@@ -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:
+6 -70
View File
@@ -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