-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-dump.py
39 lines (34 loc) · 972 Bytes
/
generate-dump.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
import argparse
import logging
import subprocess
import os
import tempfile
from tempfile import mkstemp
import configparser
import gzip
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
def backup_postgres_db(host, database_name, port, user, password, dest_file):
"""
Backup postgres db to a file.
"""
process = subprocess.Popen(
['pg_dump',
'--dbname=postgresql://{}:{}@{}:{}/{}'.format(user, password, host, port, database_name),
'-Fc',
'-f', dest_file,
'-v'],
stdout=subprocess.PIPE
)
output = process.communicate()[0]
if int(process.returncode) != 0:
print('Command failed. Return code : {}'.format(process.returncode))
exit(1)
return output
host = "localhost"
database_name = "test"
port="5432"
user="postgres"
password="postgres"
dest_file="dump.sql"
backup_postgres_db(host, database_name, port, user, password, dest_file)