-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
275 lines (229 loc) · 10.2 KB
/
backup.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin python
# -*- coding: utf-8 -*-
"""
Backuping database script with insert sorting
"""
import psycopg2
import ConfigParser
import os
import sys
import sql_reserved_words
import decimal
import datetime
import subprocess
import codecs
from shutil import move
NONE_TYPE = type(None)
DEFAULT_OUTPUT_FILE_NAME = 'database.sql'
DEFAULT_SCHEMA = 'public'
DEFAULT_OPTIONS_FILE = 'credentials.ini'
def check_identifier(identifier):
"""
Wraps identifier with \" if it is reserved word
"""
if identifier.upper() in sql_reserved_words.RESERVED_WORDS:
return '"' + identifier + '"'
else:
return identifier
def pg_dump_pre_data(host, port, password, user, database, schema, output_file_name):
"""
Getting pre data for defined db
"""
if not password is None:
os.environ['PGPASSWORD'] = password
pg_dump_process = subprocess.Popen(["C:/Program Files/PostgreSQL/9.3/bin/pg_dump.exe",
"--host",
host,
"--port",
port,
"--username",
user,
"--format",
"plain",
"--no-owner",
"--section",
"pre-data",
"--encoding",
"UTF8",
"--no-privileges",
"--no-tablespaces",
"--verbose",
"--no-unlogged-table-data",
"--schema=%s"%schema,
"--file",
output_file_name,
database],
env=os.environ)
pg_dump_process.wait()
def pg_dump_post_data(host, port, password, user, database, schema, output_file_name):
"""
Getting post data for defined db
"""
if not password is None:
os.environ['PGPASSWORD'] = password
pg_dump_process = subprocess.Popen(["C:/Program Files/PostgreSQL/9.3/bin/pg_dump.exe",
"--host",
host,
"--port",
port,
"--username",
user,
"--format",
"plain",
"--no-owner",
"--section",
"post-data",
"--encoding",
"UTF8",
"--no-privileges",
"--no-tablespaces",
"--verbose",
"--no-unlogged-table-data",
"--schema=%s"%schema,
"--file",
output_file_name,
database],
env=os.environ)
pg_dump_process.wait()
def get_db_con(host, database, user, password, port):
"""
Get connection to db
"""
if password is None:
return psycopg2.connect("host=%s dbname=%s user=%s port=%s" % (host, database, user, port))
else:
return psycopg2.connect("host=%s dbname=%s user=%s password=%s port=%s" % (host, database, user, password, port))
def preprocess_input_params():
"""
Checking argv and connecting to db
"""
if len(sys.argv) == 2 or len(sys.argv) > 2:
output_file = sys.argv[1]
else:
output_file = DEFAULT_OUTPUT_FILE_NAME
if len(sys.argv) > 2:
ini_file = sys.argv[2]
else:
ini_file = DEFAULT_OPTIONS_FILE
if not os.path.exists(ini_file):
print u"ERROR: Move backup script into the root project directory, for example 'C:\\xampp\\htdocs', or provide path to credentials.ini as second parameter and output file name as first"
sys.exit(1)
config = ConfigParser.ConfigParser()
config.readfp(open(ini_file))
if config.has_option('Database', 'schema'):
schema = config.get('Database', 'schema')
else:
schema = DEFAULT_SCHEMA
if config.has_option('Database', 'password'):
# To secure store password we can use
# this feature: http://www.postgresql.org/docs/9.3/static/libpq-pgpass.html
# ("The password file" feature)
password = config.get('Database', 'password')
else:
password = None
user = config.get('Database', 'user')
port = config.get('Database', 'port')
host = config.get('Database', 'host')
database = config.get('Database', 'database')
return (host, database, user, password, port, schema, output_file)
def process_tuple_to_string(tuple_):
"""
Convert python types to PostgreSQL insert strings
"""
result = []
for item in tuple_:
type_ = type(item)
if type_ == NONE_TYPE:
result.append("NULL")
elif (type_ == unicode or
type_ == str):
result.append("'%s'"%item.replace("'", "''").replace("\r", ""))
elif (type(item) == int or
type(item) == long):
result.append("%s"%item)
elif type(item) == decimal.Decimal:
result.append("%s"%unicode(item))
elif type(item) == datetime.datetime:
result.append("'%s'"%item.isoformat(' '))
elif type(item) == bool:
result.append(str(item))
else:
raise Exception("Unknown type to convert: '%s'"%type(item))
return u", ".join([item.decode('utf-8') for item in result])
def get_data(con, schema, output_file_name):
"""
Get inserts for dump
"""
output_file = codecs.open(output_file_name, "a", encoding='utf-8')
cur = con.cursor()
cur.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = %s AND table_type = 'BASE TABLE' ORDER BY table_name", (schema, ))
for table in cur.fetchall():
table_name = check_identifier(table[0])
cur.execute("SELECT column_name FROM information_schema.columns WHERE table_name = %s ORDER BY ordinal_position", (table[0], ))
columns = [check_identifier(tuple_[0]) for tuple_ in cur.fetchall()]
columns_list = ", ".join(columns)
cur.execute("""SELECT kcu.column_name
FROM information_schema.tables t
LEFT JOIN information_schema.table_constraints tc
ON tc.table_catalog = t.table_catalog
AND tc.table_schema = t.table_schema
AND tc.table_name = t.table_name
AND tc.constraint_type = 'PRIMARY KEY'
LEFT JOIN information_schema.key_column_usage kcu
ON kcu.table_catalog = tc.table_catalog
AND kcu.table_schema = tc.table_schema
AND kcu.table_name = tc.table_name
AND kcu.constraint_name = tc.constraint_name
WHERE t.table_schema = %s AND
t.table_name = %s
ORDER BY t.table_catalog,
t.table_schema,
t.table_name,
kcu.constraint_name,
kcu.ordinal_position""", (schema, table_name))
pkeys = [tuple_[0] for tuple_ in cur.fetchall() if not tuple_[0] is None]
insert_sql_ = "INSERT INTO %s (%s) VALUES (%s);\n"%(table_name, columns_list, '%s')
if len(pkeys) > 0:
select_sql_ = "SELECT %s FROM %s ORDER BY %s;"%(columns_list, table_name, ", ".join([check_identifier(pkey) for pkey in pkeys]))
else:
select_sql_ = "SELECT %s FROM %s ORDER BY %s;"%(columns_list, table_name, columns_list)
cur.execute(select_sql_)
for tuple_ in cur.fetchall():
output_file.write(insert_sql_%process_tuple_to_string(tuple_))
output_file.write("\n")
for column in columns:
cur.execute("SELECT pg_get_serial_sequence(%s, %s)", (table_name.replace('"', ''), column.replace('"', '')))
for seq in cur.fetchall():
if not seq[0] is None:
cur.execute("SELECT sequence_name, last_value, is_called FROM %s"%seq[0])
output_file.write("SELECT pg_catalog.setval('%s', %s, %s);\n\n\n"%cur.fetchone())
con.close()
def clear_dump(file_name):
"""
Remove comments from dump to reduce differences between dumps
"""
res_file = open(file_name+'clean_file', 'w')
for line in open(file_name, 'r'):
if (not line.startswith('-- TOC') and
not line.startswith('-- Dependencies') and
not line.startswith('-- Started') and
not line.startswith('-- Completed')):
res_file.write(line)
else:
res_file.write('\n')
res_file.close()
move(file_name+'clean_file', file_name)
def main():
"""
Main rutine
"""
host, database, user, password, port, schema, output_file_name = preprocess_input_params()
con = get_db_con(host, database, user, password, port)
pg_dump_pre_data(host, port, password, user, database, schema, output_file_name)
get_data(con, schema, output_file_name)
buffer_file_name = output_file_name + ".buffer"
pg_dump_post_data(host, port, password, user, database, schema, buffer_file_name)
open(output_file_name, "a").write(open(buffer_file_name, "r").read())
os.remove(buffer_file_name)
clear_dump(output_file_name)
main()