25
|
1 import logging
|
|
2 from mercurial import config
|
|
3 import tarfile
|
|
4 import os
|
|
5 import datetime
|
|
6 import sys
|
|
7 logging.basicConfig(level = logging.DEBUG,
|
|
8 format = "%(asctime)s %(levelname)-5.5s %(message)s")
|
|
9
|
|
10 class BackupManager(object):
|
|
11 def __init__(self):
|
|
12
|
|
13 dn = os.path.dirname
|
|
14 self.backup_file_path = os.path.join(dn(dn(dn(__file__))), 'data')
|
|
15 cfg = config.config()
|
|
16 try:
|
|
17 cfg.read(os.path.join(dn(dn(dn(__file__))), 'hgwebdir.config'))
|
|
18 except IOError:
|
|
19 logging.error('Could not read hgwebdir.config')
|
|
20 sys.exit()
|
|
21 self.set_repos_path(cfg.items('paths'))
|
|
22 logging.info('starting backup for %s', self.repos_path)
|
|
23 logging.info('backup target %s', self.backup_file_path)
|
|
24
|
|
25 if not os.path.isdir(self.repos_path):
|
|
26 raise Exception('Not a valid directory in %s' % self.repos_path)
|
|
27
|
|
28 def set_repos_path(self, paths):
|
|
29 repos_path = paths[0][1].split('/')
|
|
30 if repos_path[-1] in ['*', '**']:
|
|
31 repos_path = repos_path[:-1]
|
|
32 if repos_path[0] != '/':
|
|
33 repos_path[0] = '/'
|
|
34 self.repos_path = os.path.join(*repos_path)
|
|
35
|
|
36 def backup_repos(self):
|
|
37 today = datetime.datetime.now().weekday() + 1
|
|
38 bckp_file = os.path.join(self.backup_file_path,
|
|
39 "mercurial_repos.%s.tar.gz" % today)
|
|
40 tar = tarfile.open(bckp_file, "w:gz")
|
|
41
|
|
42 for dir in os.listdir(self.repos_path):
|
|
43 logging.info('backing up %s', dir)
|
|
44 tar.add(os.path.join(self.repos_path, dir), dir)
|
|
45 tar.close()
|
|
46
|
|
47
|
|
48 if __name__ == "__main__":
|
|
49 bm = BackupManager()
|
|
50 bm.backup_repos()
|
|
51
|
|
52
|