-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
180 lines (146 loc) · 6.09 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
#!/usr/bin/python3
import os
import yaml
import time
import shutil
import logging
import zipfile
import subprocess
class MasterBackup:
# General tool settings
settings = dict()
tmpPaths = dict()
timestamp = None
active_client = dict()
logger = None
backup_dir = str()
def __init__(self):
self.logger = logging
self.timestamp = time.strftime('%Y-%m-%d-%I:%M:%S')
self.load_settings()
self.active_client = self.get_specific_client(os.getenv('ACTIVE_CLIENT'))
self.backup_dir = "{backup_dir}{client}_{timestamp}".format(
backup_dir=self.active_client['backup_dir']['default'],
client=self.active_client['name'],
timestamp=self.timestamp
)
self.create_backup_dir()
def create_backup_dir(self):
if(os.path.isdir(self.backup_dir)):
return self.backup_dir
else:
try:
os.mkdir(self.backup_dir)
self.logger.info("Created new BK directory in {dir}".format(
dir=self.backup_dir
))
if(os.path.isdir(self.backup_dir)):
return self.backup_dir
except:
msg = "Failed creating BK directory in {dir}".format(
dir=self.backup_dir
)
self.logger.warn(msg)
raise Exception(msg)
def load_settings(self):
with open(os.getenv('GENERAL_SETTINGS')) as settings:
settings_file = yaml.full_load(settings)
for item, doc in settings_file.items():
self.settings[item] = doc
active_client = self.get_specific_client(os.getenv('ACTIVE_CLIENT'))
self.logger.basicConfig(
filename=active_client['log_file'],
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=self.logger.DEBUG)
self.logger.info('Loaded enviroment and databases config')
def get_specific_client(self, client: str):
if client in self.settings['databases']:
return self.settings['databases'][client]
return None
def do_sql_backup(self):
try:
self.logger.info('SQL Backup [START] on {backup_dir}'.format(
backup_dir=self.backup_dir
))
dest_folder = '{dest}/{name}_{time}.sql'.format(
dest=self.backup_dir,
name=self.active_client['name'],
time=self.timestamp
)
task = os.popen("mysqldump -u {user} -p{password} -e --opt -c {database} > {backup}".format(
host=self.active_client['host'],
user=self.active_client['username'],
password=self.active_client['password'],
database=self.active_client['database'],
backup=dest_folder
))
self.tmpPaths['db'] = dest_folder
self.logger.info('SQL Backup [SUCCESS]')
return True
except:
msg = 'Error ocurred on SQL backup action'
self.logger.warn(msg)
raise Exception(msg)
def do_project_backup(self):
if(os.path.isdir(self.active_client['working_dir'])):
try:
destPath = "{dest}/{name}/".format(
dest=self.backup_dir,
name=self.active_client['name']
)
self.logger.info('Project backup [START] on {path}'.format(
path=destPath
))
taskPath = shutil.copytree(
src=self.active_client['working_dir'],
dst=destPath,
symlinks=True
)
if(destPath == taskPath):
self.logger.info('Project backup [SUCCESS] in {path}'. format(
path=taskPath
))
self.tmpPaths['project'] = taskPath
return True
except:
msg = 'Error ocurred on project backup action'
self.logger.warn(msg)
raise Exception(msg)
def get_file_paths(self, path: str):
# Files paths
filePaths = []
for root, dirs, files in os.walk(path):
for filename in files:
filePath = os.path.join(root, filename)
filePaths.append(filePath)
return filePaths
def compress_and_save(self):
if(len(self.tmpPaths.keys()) > 0):
try:
self.logger.info('ZIP Compression [START]')
zip_file_name = "{base}{name}_{timestamp}.zip".format(
base=self.active_client['backup_dir']['default'],
name=self.active_client['name'],
timestamp=self.timestamp
)
zip_file = zipfile.ZipFile(
zip_file_name,
'w',
zipfile.ZIP_DEFLATED
)
filePaths = self.get_file_paths(self.backup_dir)
# Patch timestamps before 1980 error
timestamp = time.mktime((1980, 1, 1, 0, 0, 0, 0, 0, 0))
with zip_file:
for path in filePaths:
os.utime(path, (timestamp, timestamp))
zip_file.write(path)
self.logger.info('ZIP Compression [SUCCESS]')
shutil.rmtree(self.backup_dir)
except:
raise Exception('Compression task failed')
else:
raise Exception('Compression task [ERROR]')
if __name__ == '__main__':
settings = MasterBackup()
if(settings.do_sql_backup() and settings.do_project_backup()):
settings.compress_and_save()