| 403 | | if not db_str.startswith('sqlite:'): |
| 404 | | raise TracError(_('Can only backup sqlite databases')) |
| 405 | | db_name = os.path.join(self.path, db_str[7:]) |
| 406 | | if not dest: |
| 407 | | dest = '%s.%i.bak' % (db_name, self.get_version()) |
| 408 | | shutil.copy (db_name, dest) |
| | 403 | if db_str.startswith('sqlite:'): |
| | 404 | db_name = os.path.join(self.path, db_str[7:]) |
| | 405 | if not dest: |
| | 406 | dest = '%s.%i.bak' % (db_name, self.get_version()) |
| | 407 | shutil.copy (db_name, dest) |
| | 408 | elif db_str.startswith('postgres:'): |
| | 409 | db = self.get_db_cnx() |
| | 410 | cursor = db.cursor() |
| | 411 | db_schema = db.schema.encode('utf-8') |
| | 412 | sql = "SELECT tablename FROM pg_tables WHERE schemaname='%s'" % db_schema |
| | 413 | cursor.execute(sql) |
| | 414 | tables = [row[0] for row in cursor] |
| | 415 | bk_scheme = "%s-backup-%i" % (db_schema, self.get_version()) |
| | 416 | # XXX must have access rights to handle this |
| | 417 | sql = 'CREATE SCHEMA "%s"' % bk_scheme |
| | 418 | cursor.execute(sql) |
| | 419 | db.commit() |
| | 420 | for table in tables: |
| | 421 | sql = 'SELECT * into "%s".%s from "%s".%s' % (bk_scheme, table, db_schema, table) |
| | 422 | cursor.execute(sql) |
| | 423 | db.commit() |
| | 424 | else: |
| | 425 | raise TracError(_('Can only backup sqlite and postgres databases')) |