-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.py
49 lines (37 loc) · 1.1 KB
/
command.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
49
import logging
import subprocess
from datetime import datetime
from settings import (
LOGGING,
PG_HOST,
PG_PORT,
PG_USER,
DB_NAME,
SQL_DIR
)
logger = logging.getLogger('*')
def backup_filename():
return f'backup-{datetime.now().strftime("%m-%d-%YT%H-%M-%S")}.sql'
def path_to_backup_dir(filename):
return SQL_DIR.joinpath(filename)
def dump_command(output_file_path):
"""
Returns pg_dump command with output path
"""
return ['pg_dump', '-h' , PG_HOST , '-p', PG_PORT, '-U', PG_USER, DB_NAME, '-f', output_file_path]
def run_command(command):
"""
Runs pg_dump command as subprocess.
You may pass your own pg_dump command.
Note that this only allows pg_dump commands!
"""
# avoid shenanigans
if command[0] != 'pg_dump':
raise Exception('Warning: run_command() is meant to only run pg_dump commands!')
success = False
try:
subprocess.call(command)
success = True
except subprocess.SubprocessError as e:
logger.error('Error at %s', 'SubprocessError', exc_info=e)
return success