-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg_export.py
221 lines (193 loc) · 6.69 KB
/
pg_export.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from datetime import datetime
import time
import sqlite3 as sqlite
import psycopg2
"""
Script for exporting the weewx archive database to a remote postgresql database.
"""
SQLITE_ARCHIVE = "/var/lib/weewx/weewx.sdb"
PG_HOST = "some.host.tld"
PG_DB = "db_name"
PG_PORT = 5432
PG_USER = "username"
PG_PASS = "password"
## TESTING AREA
def main():
local_dt = get_latest_archive_row_datetime_local()
remote_dt = get_latest_archive_row_datetime_remote()
if not local_dt:
print "Local database empty. Not populated yet?"
sys.exit(0)
if not remote_dt:
print "Remote database empty"
remote_dt = datetime.fromtimestamp(0)
if local_dt != remote_dt:
print "Unequal timestamps between local and remote database."
result = fetch_archive_rows_since_datetime(remote_dt)
print "Number of new rows: %s" % len(result)
#convert_values(result)
write_to_pg_db(result)
else:
print "In sync. Nothing to do."
def _query_sqlite(query):
con = None
try:
con = sqlite.connect(SQLITE_ARCHIVE)
cur = con.cursor()
cur.execute(query)
result = cur.fetchall()
return result
except sqlite.Error, e:
print "Error fetching SQLite data %s" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
def _connect_to_remote_pg():
try:
con_string = "host={host} dbname={db_name} user={user} password={password}".format(host=PG_HOST, db_name=PG_DB, user=PG_USER, password=PG_PASS)
con = psycopg2.connect(con_string)
return con
except Exception, e:
print "Error fetching PostgreSQL data: %s" % e.args[0]
sys.exit(1)
def _query_pg(query):
con = None
try:
con = _connect_to_remote_pg()
cur = con.cursor()
cur.execute(query)
result = cur.fetchall()
return result
except Exception, e:
print "Error fetching PostgreSQL data: %s" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
def _insert_pg(query):
con = None
try:
con = _connect_to_remote_pg()
cur = con.cursor()
cur.execute(query)
result = cur.fetchall()
return result
except Exception, e:
print "Error inserting data to PostgreSQL database: %s" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
def fetch_archive_rows_since_datetime(date_time):
"""Fetch the rows from the 'archive' SQLite database since given datetime"""
timestamp = int(time.mktime(date_time.timetuple()))
query = "SELECT * FROM archive WHERE dateTime > {timestamp}".format(
timestamp=timestamp)
return _query_sqlite(query)
def get_latest_archive_row_datetime_local():
"""Return the datetime of the latest row written"""
query = "SELECT dateTime FROM archive ORDER BY dateTime DESC LIMIT 1"
result = _query_sqlite(query)
if len(result):
dt = datetime.fromtimestamp(result[0][0])
print dt.strftime('%Y-%m-%d %H:%M:%S')
return dt
else:
return None
def get_latest_archive_row_datetime_remote():
"""Return the datetime of the latest row written"""
query = "SELECT datetime FROM archive ORDER BY datetime DESC LIMIT 1"
result = _query_pg(query)
if len(result):
print result[0][0].strftime('%Y-%m-%d %H:%M:%S')
return result[0][0]
else:
return None
def convert_values(rows):
"""Convert values to a more suited format"""
print rows[0]
def write_to_pg_db(rows):
"""Write retrieved archive rows to remote PostgreSQL database"""
con = _connect_to_remote_pg()
cur = con.cursor()
try:
for row in rows:
query = """INSERT INTO archive (
datetime,
usunits,
interval,
barometer,
pressure,
altimeter,
intemp,
outtemp,
inhumidity,
outhumidity,
windspeed,
winddir,
windgust,
windgustdir,
rainrate,
rain,
dewpoint,
windchill,
heatindex,
et,
radiation,
uv,
extratemp1,
extratemp2,
extratemp3,
soiltemp1,
soiltemp2,
soiltemp3,
soiltemp4,
leaftemp1,
leaftemp2,
extrahumid1,
extrahumid2,
soilmoist1,
soilmoist2,
soilmoist3,
soilmoist4,
leafwet1,
leafwet2,
rxcheckpercent,
txbatterystatus,
consbatteryvoltage,
hail,
hailrate,
heatingtemp,
heatingvoltage,
supplyvoltage,
referencevoltage,
windbatterystatus,
rainbatterystatus,
outtempbatterystatus,
intempbatterystatus
) VALUES (
to_timestamp(%s), %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s
);"""
cur.execute(query, row)
print "Add data point from %s" % datetime.fromtimestamp(row[0]).strftime('%Y-%m-%d %H:%M:%S')
#print cur.fetchone()[0]
con.commit()
except psycopg2.DatabaseError, e:
if con:
con.rollback()
print "PostgreSQL error: %s" % e
sys.exit(1)
finally:
if con:
con.close()
if __name__ == "__main__":
main()