This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
postgresql.py
49 lines (40 loc) · 1.51 KB
/
postgresql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging
import os
import tempfile
import dumpy
logger = logging.getLogger("dumper")
class PostgresqlBackup(dumpy.base.BackupBase):
def parse_config(self):
super(PostgresqlBackup, self).parse_config()
section = 'database %s' % self.db
self.name = self._get_option_value(self.config, section, 'name')
self.user = self._get_option_value(self.config, section, 'user')
self.host = self._get_option_value(self.config, section, 'host')
self.port = self._get_option_value(self.config, section, 'port', 'int')
self.binary = self._get_option_value(self.config, 'pg_dump options', 'path')
self.flags = self._get_option_value(self.config, 'pg_dump options', 'flags')
def get_flags(self):
# @@@ ugh. i don't like this.
if self.flags is None:
flags = ''
else:
flags = '%s' % self.flags
if self.user:
flags += ' -U %s' % self.user
if self.host:
flags += ' -h %s' % self.host
if self.port:
flags += ' -p %d' % self.port
return flags
def backup(self):
self.parse_config()
tmp_file = tempfile.NamedTemporaryFile()
cmd = '%(binary)s %(flags)s %(database)s > %(file)s' % {
'binary': self.binary,
'flags': self.get_flags(),
'database': self.name,
'file': tmp_file.name,
}
logger.info('%s - Command: %s' % (self.db, cmd))
os.system(cmd)
return tmp_file