Skip to content

Commit

Permalink
Added environment variable control
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuluPro committed May 16, 2016
1 parent 8f47ad1 commit e30755d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
14 changes: 11 additions & 3 deletions dbbackup/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,31 @@ class BaseCommandDBConnector(BaseDBConnector):
dump_suffix = ''
restore_prefix = ''
restore_suffix = ''
env = {}
dump_env = {}
restore_env = {}

def run_command(self, command, stdin=None):
def run_command(self, command, stdin=None, env=None):
"""
Launch a shell command line.
:param command: Command line to launch
:type command: str
:param stdin: Standard input of command
:type stdin: file
:param env: Environment variable used in command
:type env: dict
:return: Standard output of command
:rtype: file
"""
cmd = shlex.split(command)
stdout = SpooledTemporaryFile(max_size=10 * 1024 * 1024)
stderr = SpooledTemporaryFile(max_size=10 * 1024 * 1024)
cmd = shlex.split(command)
full_env = self.env.copy()
full_env.update(env or {})
try:
process = Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr)
process = Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr,
env=full_env)
process.wait()
if process.poll():
stderr.seek(0)
Expand Down
4 changes: 2 additions & 2 deletions dbbackup/db/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _create_dump(self):
cmd += ' --excludeCollection {}'.format(collection)
cmd += ' --archive'
cmd = '{} {} {}'.format(self.dump_prefix, cmd, self.dump_suffix)
stdout, stderr = self.run_command(cmd)
stdout, stderr = self.run_command(cmd, env=self.dump_env)
return stdout

def _restore_dump(self, dump):
Expand All @@ -38,4 +38,4 @@ def _restore_dump(self, dump):
cmd += ' --drop'
cmd += ' --archive'
cmd = '{} {} {}'.format(self.restore_prefix, cmd, self.restore_suffix)
return self.run_command(cmd, stdin=dump)
return self.run_command(cmd, stdin=dump, env=self.restore_env)
4 changes: 2 additions & 2 deletions dbbackup/db/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _create_dump(self):
for table in self.exclude:
cmd += ' --ignore-table={}.{}'.format(self.settings['NAME'], table)
cmd = '{} {} {}'.format(self.dump_prefix, cmd, self.dump_suffix)
stdout, stderr = self.run_command(cmd)
stdout, stderr = self.run_command(cmd, env=self.dump_env)
return stdout

def _restore_dump(self, dump):
Expand All @@ -36,5 +36,5 @@ def _restore_dump(self, dump):
if self.settings.get('PASSWORD'):
cmd += ' --password={}'.format(self.settings['PASSWORD'])
cmd = '{} {} {}'.format(self.restore_prefix, cmd, self.restore_suffix)
stdout, stderr = self.run_command(cmd, stdin=dump)
stdout, stderr = self.run_command(cmd, stdin=dump, env=self.restore_env)
return stdout, stderr
4 changes: 2 additions & 2 deletions dbbackup/db/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _create_dump(self):
if self.drop:
cmd += ' --clean'
cmd = '{} {} {}'.format(self.dump_prefix, cmd, self.dump_suffix)
stdout, stderr = self.run_command(cmd)
stdout, stderr = self.run_command(cmd, env=self.dump_env)
return stdout

def _restore_dump(self, dump):
Expand All @@ -47,7 +47,7 @@ def _restore_dump(self, dump):
if self.single_transaction:
cmd += ' --single-transaction'
cmd = '{} {} {}'.format(self.restore_prefix, cmd, self.restore_suffix)
stdout, stderr = self.run_command(cmd, stdin=dump)
stdout, stderr = self.run_command(cmd, stdin=dump, env=self.restore_env)
return stdout, stderr


Expand Down
13 changes: 13 additions & 0 deletions dbbackup/tests/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ def test_run_command_stdin(self):
self.assertEqual(stdout.read(), b'foo')
self.assertFalse(stderr.read())

def test_run_command_with_env(self):
connector = BaseCommandDBConnector()
# Empty env
stdout, stderr = connector.run_command('env')
self.assertFalse(stdout.read())
# env from self.env
connector.env = {'foo': 'bar'}
stdout, stderr = connector.run_command('env')
self.assertEqual(stdout.read(), b'foo=bar\n')
# method overide gloabal env
stdout, stderr = connector.run_command('env', env={'foo': 'ham'})
self.assertEqual(stdout.read(), b'foo=ham\n')


class SqliteConnectorTest(TestCase):
def test_write_dump(self):
Expand Down
7 changes: 7 additions & 0 deletions docs/databases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ DUMP_SUFFIX and RESTORE_PREFIX
String to include as suffix of dump or restore command. It will be add with
a space between launched command and its suffix.

ENV, DUMP_ENV and RESTORE_ENV
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Environment variables used during command running, default are ``{}``. ``ENV``
is used for every command, ``DUMP_ENV`` and ``RESTORE_ENV`` override the
values defined in ``ENV`` during the dedicated commands.

SQLite
------

Expand Down

0 comments on commit e30755d

Please sign in to comment.