##// END OF EJS Templates
fixed the backup script path problems
Marcin Kuzminski -
r27:8f29ddc4 default
parent child Browse files
Show More
@@ -1,53 +1,54 b''
1 import logging
1 import logging
2 from mercurial import config
2 from mercurial import config
3 import tarfile
3 import tarfile
4 import os
4 import os
5 import datetime
5 import datetime
6 import sys
6 import sys
7 logging.basicConfig(level = logging.DEBUG,
7 logging.basicConfig(level = logging.DEBUG,
8 format = "%(asctime)s %(levelname)-5.5s %(message)s")
8 format = "%(asctime)s %(levelname)-5.5s %(message)s")
9
9
10 class BackupManager(object):
10 class BackupManager(object):
11 def __init__(self):
11 def __init__(self):
12
12
13 cur_dir = os.path.realpath(__file__)
13 dn = os.path.dirname
14 dn = os.path.dirname
14 self.backup_file_path = os.path.join(dn(dn(dn(__file__))), 'data')
15 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data')
15 cfg = config.config()
16 cfg = config.config()
16 try:
17 try:
17 cfg.read(os.path.join(dn(dn(dn(__file__))), 'hgwebdir.config'))
18 cfg.read(os.path.join(dn(dn(dn(cur_dir))), 'hgwebdir.config'))
18 except IOError:
19 except IOError:
19 logging.error('Could not read hgwebdir.config')
20 logging.error('Could not read hgwebdir.config')
20 sys.exit()
21 sys.exit()
21 self.set_repos_path(cfg.items('paths'))
22 self.set_repos_path(cfg.items('paths'))
22 logging.info('starting backup for %s', self.repos_path)
23 logging.info('starting backup for %s', self.repos_path)
23 logging.info('backup target %s', self.backup_file_path)
24 logging.info('backup target %s', self.backup_file_path)
24
25
25 if not os.path.isdir(self.repos_path):
26 if not os.path.isdir(self.repos_path):
26 raise Exception('Not a valid directory in %s' % self.repos_path)
27 raise Exception('Not a valid directory in %s' % self.repos_path)
27
28
28 def set_repos_path(self, paths):
29 def set_repos_path(self, paths):
29 repos_path = paths[0][1].split('/')
30 repos_path = paths[0][1].split('/')
30 if repos_path[-1] in ['*', '**']:
31 if repos_path[-1] in ['*', '**']:
31 repos_path = repos_path[:-1]
32 repos_path = repos_path[:-1]
32 if repos_path[0] != '/':
33 if repos_path[0] != '/':
33 repos_path[0] = '/'
34 repos_path[0] = '/'
34 self.repos_path = os.path.join(*repos_path)
35 self.repos_path = os.path.join(*repos_path)
35
36
36 def backup_repos(self):
37 def backup_repos(self):
37 today = datetime.datetime.now().weekday() + 1
38 today = datetime.datetime.now().weekday() + 1
38 bckp_file = os.path.join(self.backup_file_path,
39 bckp_file = os.path.join(self.backup_file_path,
39 "mercurial_repos.%s.tar.gz" % today)
40 "mercurial_repos.%s.tar.gz" % today)
40 tar = tarfile.open(bckp_file, "w:gz")
41 tar = tarfile.open(bckp_file, "w:gz")
41
42
42 for dir in os.listdir(self.repos_path):
43 for dir in os.listdir(self.repos_path):
43 logging.info('backing up %s', dir)
44 logging.info('backing up %s', dir)
44 tar.add(os.path.join(self.repos_path, dir), dir)
45 tar.add(os.path.join(self.repos_path, dir), dir)
45 tar.close()
46 tar.close()
46 logging.info('finished backup of mercurial repositories')
47 logging.info('finished backup of mercurial repositories')
47
48
48
49
49 if __name__ == "__main__":
50 if __name__ == "__main__":
50 bm = BackupManager()
51 bm = BackupManager()
51 bm.backup_repos()
52 bm.backup_repos()
52
53
53
54
General Comments 0
You need to be logged in to leave comments. Login now