-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
executable file
·382 lines (291 loc) · 12.4 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
import datetime
from datetime import timedelta
import yaml
import logging
import logging.config
import subprocess
import os
import sys
import re
import glob
import argparse
class AbstractDriver(object):
def __init__(self, config = None, logger = None):
self.config = {'bin_path': '', 'env': None, 'aws_access_key': None, 'aws_secret_key': None}
if config:
self.config.update(config)
self.logger = logger or logging.getLogger(__name__)
def _run(self, cmd):
env = os.environ.copy()
if self.config['env']:
env.update(self.config['env'])
process = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
for line in process.stdout:
self.logger.debug(line.rstrip())
for line in process.stderr:
self.logger.info(line.rstrip())
process.wait()
class RcloneDriver(AbstractDriver):
def __init__(self, config = None, logger = None):
super(RcloneDriver, self).__init__(config, logger)
if not self.config['bin_path']:
self.config['bin_path'] = 'rclone'
if not self.config['aws_access_key'] or not self.config['aws_secret_key']:
if 'aws_env_auth' in self.config and not self.config['aws_env_auth']:
raise Exception('Either configure aws_env_auth or configure access/secret key')
else:
self.config['aws_env_auth'] = True
else:
self.config['aws_env_auth'] = False
self.flags = self.build_flags()
def build_flags(self):
flags = ['-v', '--s3-provider', 'AWS']
if 'aws_env_auth' in self.config and self.config['aws_env_auth']:
flags.extend(['--s3-env-auth'])
else:
flags.extend(['--s3-access-key-id', self.config['aws_access_key']])
flags.extend(['--s3-secret-access-key', self.config['aws_secret_key']])
if 'aws_region' in self.config:
flags.extend(['--s3-region', self.config['aws_region']])
if 'aws_s3_acl' in self.config:
flags.extend(['--s3-acl', self.config['aws_s3_acl']])
if 'aws_s3_server_side_encryption' in self.config:
flags.extend(['--s3-server-side-encryption', self.config['aws_s3_server_side_encryption']])
if 'aws_s3_storage_class' in self.config:
flags.extend(['--s3-storage-class', self.config['aws_s3_storage_class']])
return flags
def fix_s3_path(self, path):
return path.replace('s3://', ':s3:')
def sync(self, src, dest):
self.logger.info("rclone::syncing '%s' to amazon s3", src.get_name())
flags = self.flags[:]
for path in src.get_excluded_paths():
flags.extend(['--exclude', self.fix_s3_path(path)])
dest = dest.replace('s3://', ':s3:')
included_paths = src.get_included_paths(self)
if included_paths:
for path in src.get_included_paths(self):
cmd = [self.config['bin_path'], 'sync', self.fix_s3_path(path), dest]
cmd.extend(flags)
self._run(cmd)
else:
self.logger.info('Skipping copy as source list is empty')
def remove(self, dest):
self.logger.info("rclone::removing '%s' from amazon s3", dest)
dest = dest.replace('s3://', ':s3:')
cmd = [self.config['bin_path'], 'purge', self.fix_s3_path(dest)]
cmd.extend(self.flags)
self._run(cmd)
def exists(self, dest):
return True
class RsyncDriver(AbstractDriver):
def __init__(self, config = None, logger = None):
super(RsyncDriver, self).__init__(config, logger)
if not self.config['bin_path']:
self.config['bin_path'] = 'rsync'
def sync(self, src, dest):
self.logger.info("rsync::syncing '%s' to filesystem", src.get_name())
cmd = [self.config['bin_path'], '-avP']
for path in src.get_excluded_paths():
cmd.extend(['--exclude', path])
included_paths = src.get_included_paths(self)
if not os.path.exists(dest):
os.makedirs(dest)
if not os.path.isdir(dest):
raise Exception('Destination is not a directory %s' % dest);
if included_paths:
for path in included_paths:
cmd.append(path)
cmd.append(dest)
self._run(cmd)
else:
self.logger.info('Skipping copy as source list is empty')
def remove(self, dest):
self.logger.info("rclone::removing '%s' from filesytem", dest)
if dest == '/':
self.logger.error("rsync::cannot remove '/' directory")
else:
self._run(['rm', '-rf', dest])
def exists(self, dest):
return os.path.isdir(dest)
class AmazonS3Store():
def __init__(self, config, driver = None):
# self.config = config
self.logger = logging.getLogger(__name__)
if 'bucket' not in config:
raise Exception("'bucket' is required for amazons3 store")
self.prefix = 's3://' + config['bucket']
if 'prefix' in config:
self.prefix = 's3://' + self.config['bucket'] + '/' + config['prefix']
if driver == None:
driver = RcloneDriver(config)
self.driver = driver
def __get_destination(self, backup, version):
return os.path.join(self.prefix, version.strftime('%Y-%m-%d'), backup.get_name())
def add(self, version, backup):
self.driver.sync(backup, self.__get_destination(backup, version))
def remove(self, backup, version):
dest = self.__get_destination(backup, version)
if self.driver.exists(dest):
self.driver.remove(dest)
class FileSystemStore():
def __init__(self, config, driver = None):
self.config = config
self.logger = logging.getLogger(__name__)
if 'path' not in config:
raise Exception("'path' is required for filesystem store")
if os.path.isdir(config['path']) == False:
raise Exception("%s is not a directory" % config['path'])
if not os.access(config['path'], os.W_OK | os.X_OK):
raise Exception("%s is not writeable" % config['path'])
if driver == None:
driver = RsyncDriver()
self.driver = driver
def __get_destination(self, backup, version):
return os.path.join(self.config['path'], version.strftime('%Y-%m-%d'), backup.get_name())
def add(self, version, backup):
self.driver.sync(backup, self.__get_destination(backup, version))
def remove(self, backup, version):
dest = self.__get_destination(backup, version)
if self.driver.exists(dest):
self.driver.remove(dest)
class Backup():
def __init__(self, config):
self.config = {'onerror': 'exception'}
self.config.update(config)
self.logger = logging.getLogger(__name__)
if 'include' not in config:
raise "'include' is missing for source %s" % self.get_name()
def get_name(self):
return self.config['name']
@staticmethod
def replace_placeholder(match):
placeholder = match.group(1) or match.group(2)
if placeholder in ['Y', 'm', 'd']:
return datetime.datetime.now().strftime('%' + placeholder)
elif placeholder == 'LATEST':
return 'LATEST'
else:
raise Exception("Invalid placeholder '%s'" % placeholder)
def handle_error(self, exception):
self.logger.error(str(exception))
print(self.config)
if self.config['onerror'] == 'alert':
# Send Alert
print('Send Alert')
elif self.config['onerror'] != 'continue':
raise exception
def get_included_paths(self, driver):
include = []
now = datetime.datetime.now()
for path in self.config['include']:
path = re.sub('%([A-Za-z])|%\{([A-Za-z])\}+', Backup.replace_placeholder, path)
paths = glob.glob(path)
if not paths:
self.handle_error(Exception('Path not found %s' % path))
else:
for path in paths:
self.logger.debug('Including path %s' % path)
include.extend(paths)
return include
def get_excluded_paths(self):
if 'exclude' in self.config:
return self.config['exclude']
else:
return []
class Manager():
def __init__(self, file, logger = None):
self.backups = []
self.destinations = []
self.config = {
'log_file': './backup.log'
}
self.config.update(self.load_config(file))
self.init_logger()
self.logger = logger or logging.getLogger(__name__)
self.obsolete = self.load_obsolete()
for destination in self.config['destination']:
if destination['store'] == 'amazons3':
destination = AmazonS3Store(destination['options'])
elif destination['store'] == 'filesystem':
destination = FileSystemStore(destination['options'])
else:
raise Exception('Unsupported storage store - %s' % (destination['name']));
self.destinations.append(destination)
for source in self.config['source']:
backup = Backup(source)
self.backups.append(backup)
def init_logger(self, default_level=logging.DEBUG):
value = os.getenv('LOG_CFG', None)
if value and os.path.exists(value):
path = value
with open(path, 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
elif 'logging' in self.config:
logging.config.dictConfig(self.config['logging'])
else:
logging.basicConfig(level=default_level)
def load_config(self, file):
stream = open(file, "r")
config = yaml.safe_load(stream)
if config.get('extends'):
base = self.load_config(config['extends'])
del config['extends']
base.update(config)
config = base
return config
def load_obsolete(self):
now = datetime.datetime.now()
dow = now.weekday()
obsolete = []
if dow == 1:
date = now - timedelta(days=28)
if date.day > 7:
obsolete.append(date)
date = now - timedelta(days=364)
if date.day <= 8 and date.month > 1:
obsolete.append(date)
else:
date = now - timedelta(days=7)
obsolete.append(date)
return obsolete;
def run(self):
version = datetime.datetime.now()
for backup in self.backups:
self.logger.info('Preparing to backup %s' % backup.get_name())
for destination in self.destinations:
try:
destination.add(version, backup)
for date in self.obsolete:
destination.remove(backup, date)
except Exception as e:
backup.handle_error(e)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-c', '--config-file', help='config file', default='./config.yaml')
args = parser.parse_args()
try:
manager = Manager(args.config_file)
manager.run()
except Exception as e:
sys.exit( str(e))
# if [ $DOW -eq 1 ]; then
# DATE_DAY=$(date -d "-28 days" +"%d")
# if [ $DATE_DAY -gt 7 ]; then
# DATE=$(date -d "-28 days" +"%Y-%m-%d")
# delete $DATE
# fi
#
# DATE_DAY=$(date -d "-364 days" +"%d")
# DATE_MONTH=$(date -d "-364 days" +"%m")
# if [ $DATE_DAY -le 7 ] && [ $DATE_MONTH -gt 1 ]; then
# DATE=$(date -d "-364 days" +"%Y-%m-%d")
# delete $DATE
# fi
# else
# DATE=$(date -d "-7 days" +"%Y-%m-%d")
# echo $DATE
# delete $DATE
# fi