-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackupy.py
executable file
·157 lines (125 loc) · 4.38 KB
/
Backupy.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
#!/usr/bin/env python
import ConfigParser
import datetime
import logging
import smtplib
import os
import subprocess
from shutil import make_archive, move
from socket import gethostname
from email.mime.text import MIMEText
__author__ = "Leonardo Cezar, <[email protected]>"
__all__ = ['PostgreSQL', 'Svn']
CFG = 'backup.cfg'
# WTF!!! no inspiration...
STAGE_DIR = '/tmp/stage_bkp_dir/'
class Backupy:
"""
Rotina de Backup reescrita em Python
"""
def __init__(self):
self.section = 'general'
self.config = ConfigParser.ConfigParser()
self.config.readfp(open(CFG))
self.create_stagedir()
format = "%(asctime)s %(message)s"
logging.basicConfig(format=format, \
filename=self.config.get(self.section,'file_log'),level=logging.DEBUG)
@property
def users_mail(self):
domain = self.config.get(self.section,'default_domain')
users = self.config.get(self.section,'notify_users').split(',')
address = map(lambda k:( k.strip() + '@' + domain),users)
return ','.join(address)
def send_mail(self, subject, body):
header = MIMEText(body)
header['Subject'] = "%s %s" % (gethostname(), subject)
header['From'] = 'sysadmin'
header['To'] = self.users_mail
s = smtplib.SMTP('smtp-expresso')
s.sendmail('backup@dataprev', self.users_mail, header.as_string())
def copy_file(self,src,dst):
try:
os.copy(src, dst)
except IOError, e:
logging.error(e)
def __enter__(self):
# perhaps we should using this placeholder as a initializer?
pass
def __exit__(self,type,value,traceback):
backup_dir = self.config.get(self.section,'backup_dir')
item_backup = str(self.__class__).split('.')[1].lower()
server_name = gethostname().split('.')[0]
filename = "%s_%s_%s" % (server_name,item_backup, \
str(datetime.datetime.now().isoformat()).replace(":-",""),)
try:
make_archive(filename,'gztar',STAGE_DIR)
except OSError as e:
logging.critical(e.strerror)
print e.strerror + ' ' + source
def create_stagedir(self):
try:
if not os.path.exists(STAGE_DIR):
os.makedirs(STAGE_DIR)
except OSError, e:
logging.critical(e)
exit("Unable go ahead without a stage area")
except:
print 'unable to create pgbackup dir'
def run():
pass
def shrink(self):
pass
# Plugins are derivated classes that implements specific backup behavior.
# They need only provides the exec abstract method of superclass. if you're
# writing a new plugin you probably are going to known the better way to make
# backups in command line either using API approach or command line utilities.
class PostgreSQL(Backupy):
"""
we trust there is a .pgpass file within your $HOME with this format:
localhost:5432:*:postgres:postgres
localhost:5433:*:postgres:postgres
This is the only way to get pg_dump working securely
"""
def _exec(self):
dump_dir = 'db-backup'
databases = self.config.get('PostgreSQL', 'database')
file_output = STAGE_DIR+'postgres.tar.gz'
# file to be send to storage area
self._file = file_output
options = ['-Fc','-f'+file_output]
if databases == 'all':
cmd = ['pg_dumpall']+options
else:
cmd = ['pg_dump',self.config.get('PostgreSQL','database')]+options
try:
cmd_exec = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
logging.critical(e.output)
return
def run(self):
self._exec()
class Svn(Backupy):
def run(self):
print self.__class__
pass
class Trac(Backupy):
pass
class UserDir(Backupy):
pass
class Git(Backupy):
pass
class ConfFiles(Backupy):
pass
def _build_repr(cls):
with eval(cls)() as i:
if isinstance(i, Backupy):
i.run()
if __name__ == "__main__":
cfg = ConfigParser.ConfigParser()
cfg.readfp(open(CFG))
backup_items = cfg.get('general','items').split(',')
map(lambda b: _build_repr(b.strip()), backup_items)
# b = PostgreSQL()
# b.run()
# print b.send_mail('teste de assunto','teste backup')