-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.py
executable file
·228 lines (201 loc) · 8.22 KB
/
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
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
#!/usr/bin/env python
from __future__ import with_statement
import commands
import sys,os
from optparse import OptionParser
import ConfigParser
import logging
import glob
from stat import *
from time import strftime
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
config = ConfigParser.ConfigParser()
config.read(rel('config.ini'))
class Dump:
def __init__(self, raw=False, vars=None):
self.conf = {}
for option in config.options('config'):
value = config.get('config', option, raw, vars)
self.conf[option] = value
self.conf['time_str'] = strftime("%Y%m%d_%H%M")
self.dumpconf = ConfigParser.ConfigParser()
self.dumpconf.read(rel(self.conf['inifile']))
def listjob(self, raw=False, vars=None):
for section in self.dumpconf.sections():
result = {}
if section not in result:
result[section] = {}
for option in self.dumpconf.options(section):
value = self.dumpconf.get(section, option, raw, vars)
result[option] = value
print result
def crontab(self):
for hourly in range(24):
print "0 %s * * * %s -j %s" % (hourly, rel('dump.py'), hourly)
def nagios(self, raw=False, vars=None):
commands = open(rel('template/nagios.tmp'), "r").read()
host = open(rel('template/nagios_host.tmp'), "r").read()
print host
for section in self.dumpconf.sections():
result = {}
if section not in result:
result[section] = {}
for option in self.dumpconf.options(section):
value = self.dumpconf.get(section, option, raw, vars)
result[option] = value
result['name'] = section
print commands % result
def startdump(self, job, raw=False, vars=None):
for section in self.dumpconf.sections():
result = {}
if section not in result:
result[section] = {}
for option in self.dumpconf.options(section):
value = self.dumpconf.get(section, option, raw, vars)
result[option] = value
for jobs in result['job'].split():
self.env = {}
self.env = self.conf
self.env['name'] = section
self.env['dir'] = '%s%s' % (self.env['dest_path'], section)
if jobs is job:
if result['type'] == 'files':
self.env['path'] = result['path']
self.env['ext'] = self.env['archiver_ext']
self.dump_files()
elif result['type'] == 'database_pgsql':
self.env['ext'] = self.env['dump_ext']
self.env['host'] = result['host']
self.env['user'] = result['user']
self.env['database'] = result['database']
self.env['port'] = result['port']
self.dump_database_pgsql()
elif result['type'] == 'database_pgsql_table':
self.env['ext'] = self.env['dump_ext']
self.env['host'] = result['host']
self.env['user'] = result['user']
self.env['database'] = result['database']
self.env['port'] = result['port']
self.env['tables'] = result['tables']
self.dump_database_pgsql_tables()
def start(self, name, raw=False, vars=None):
result = {}
for option in config.options('%s' % name):
value = config.get('%s' % name, option, raw, vars)
result[option] = value
self.env = {}
self.env = self.conf
self.env['name'] = section
self.env['dir'] = '%s%s' % (self.env['dest_path'], section)
if result['type'] == 'files':
self.env['path'] = result['path']
self.env['ext'] = self.env['archiver_ext']
self.dump_files()
elif result['type'] == 'database_pgsql':
self.env['ext'] = self.env['dump_ext']
self.env['host'] = result['host']
self.env['user'] = result['user']
self.env['database'] = result['database']
self.env['port'] = result['port']
self.dump_database_pgsql()
elif result['type'] == 'database_pgsql_table':
self.env['ext'] = self.env['dump_ext']
self.env['host'] = result['host']
self.env['user'] = result['user']
self.env['database'] = result['database']
self.env['port'] = result['port']
self.env['tables'] = result['tables']
self.dump_database_pgsql_tables()
def zip(self, file):
commands.getstatusoutput(self.compress_app + ' ' + file)
def dump(self):
ret = commands.getstatusoutput(self.env['c'])
self.env['return_code'] = ret[0]
logging.info("dump status: %s" % ret[0])
def send_nsca(self):
return_code = '0'
self.env['output'] = 'create %s - size %s' % (strftime("%Y-%m-%d %H:%M:%S"), os.path.getsize('%(dir)s/%(name)s_%(time_str)s%(ext)s' % self.env) )
msg = ("backup\\t%(name)s\\t%(return_code)s\\t%(output)s\\n" % self.env)
cmd = ("%(send_nsca)s -H %(nagios_server)s" % self.env)
commands.getstatusoutput('printf "%s" | %s' % (msg,cmd))[0]
logging.info("send nagios %(name)s" % self.env)
def create_dir(self):
try:
os.mkdir(self.env['dir'])
logging.info("created dir %(dir)s" % self.env)
except OSError:
pass
def symlink(self):
try:
os.symlink('%(dir)s/%(name)s_%(time_str)s%(ext)s' % self.env, '%(dest_path)s%(name)s%(ext)s' % self.env)
logging.info('create symlink %(dest_path)s%(name)s%(ext)s' % self.env)
except OSError:
os.remove('%(dest_path)s%(name)s%(ext)s' % self.env)
logging.info('remove symlink %(dest_path)s%(name)s%(ext)s' % self.env)
os.symlink('%(dir)s/%(name)s_%(time_str)s%(ext)s' % self.env, '%(dest_path)s%(name)s%(ext)s' % self.env)
logging.info('create symlink %(dest_path)s%(name)s%(ext)s' % self.env)
def delete_old(self):
names = glob.glob('%(dir)s/*' % self.env)
for file in sorted(names)[:-10]:
os.remove(file)
logging.info("remove old backup %s " % file)
def dump_database_pgsql(self):
self.create_dir()
c = '%(pg_dump)s %(pg_dump_options)s -h %(host)s -U %(user)s -f %(dir)s/%(name)s_%(time_str)s%(ext)s %(database)s' % self.env
self.env['c'] = c
logging.info("started %(name)s: %(c)s" % self.env)
self.dump()
self.symlink()
self.delete_old()
self.send_nsca()
def dump_files(self):
self.create_dir()
c = '%(archiver)s %(archiver_options)s %(dir)s/%(name)s_%(time_str)s%(ext)s %(path)s' % self.env
self.env['c'] = c
logging.info("started %(name)s: %(c)s" % self.env)
self.dump()
self.symlink()
self.delete_old()
self.send_nsca()
def dump_database_pgsql_tables(self):
self.create_dir()
tab = []
for i in self.env['tables'].split():
tab.append( ' -t %s ' % i)
self.env['tab'] = ' '.join(tab)
c = '%(pg_dump)s %(pg_dump_options)s -h %(host)s -U %(user)s %(tab)s -f %(dir)s/%(name)s_%(time_str)s%(ext)s %(database)s' % self.env
self.env['c'] = c
logging.info("started %(name)s: %(c)s" % self.env)
self.dump()
self.symlink()
self.delete_old()
self.send_nsca()
print self.env
def parse_setup():
parser = OptionParser()
parser.add_option("-l", "--listjob", dest="listjob", action="store_true")
parser.add_option("-j", "--job", dest="job")
parser.add_option("-c", "--cron", dest="crontab", action="store_true")
parser.add_option("-n", "--nagios", dest="nagios", action="store_true")
parser.add_option("-s", "--start", dest="start")
(options, arg) = parser.parse_args()
return (options, arg)
def main():
(options, arg) = parse_setup()
logfile = config.get('config', 'logfile')
FORMAT = "%(asctime)-15s %(levelname)s %(message)s"
logging.basicConfig(level=logging.DEBUG, filename=logfile,filemode='a', format=FORMAT, datefmt='%d %b %Y %H:%M:%S',)
dump = Dump()
if options.listjob:
dump.listjob()
elif options.crontab:
dump.crontab()
elif options.nagios:
dump.nagios()
elif options.start:
dump.start(options.start)
else:
dump.startdump(options.job)
if __name__ == "__main__":
main()