Show More
@@ -1,77 +1,83 b'' | |||
|
1 | 1 | import logging |
|
2 | 2 | from mercurial import config |
|
3 | 3 | import tarfile |
|
4 | 4 | import os |
|
5 | 5 | import datetime |
|
6 | 6 | import sys |
|
7 | 7 | import subprocess |
|
8 | 8 |
logging.basicConfig(level |
|
9 | 9 |
format |
|
10 | 10 | |
|
11 | 11 | class BackupManager(object): |
|
12 | 12 | def __init__(self): |
|
13 | 13 | self.id_rsa_path = '/home/pylons/id_rsa' |
|
14 | 14 | self.check_id_rsa() |
|
15 | 15 | cur_dir = os.path.realpath(__file__) |
|
16 | 16 | dn = os.path.dirname |
|
17 | 17 | self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data') |
|
18 | 18 | cfg = config.config() |
|
19 | 19 | try: |
|
20 | 20 | cfg.read(os.path.join(dn(dn(dn(cur_dir))), 'hgwebdir.config')) |
|
21 | 21 | except IOError: |
|
22 | 22 | logging.error('Could not read hgwebdir.config') |
|
23 | 23 | sys.exit() |
|
24 | 24 | self.set_repos_path(cfg.items('paths')) |
|
25 | 25 | logging.info('starting backup for %s', self.repos_path) |
|
26 | 26 | logging.info('backup target %s', self.backup_file_path) |
|
27 | 27 | |
|
28 | 28 | if not os.path.isdir(self.repos_path): |
|
29 | 29 | raise Exception('Not a valid directory in %s' % self.repos_path) |
|
30 | 30 | |
|
31 | 31 | def check_id_rsa(self): |
|
32 | 32 | if not os.path.isfile(self.id_rsa_path): |
|
33 | 33 | logging.error('Could not load id_rsa key file in %s', self.id_rsa_path) |
|
34 | 34 | sys.exit() |
|
35 | 35 | |
|
36 | 36 | def set_repos_path(self, paths): |
|
37 | 37 | repos_path = paths[0][1].split('/') |
|
38 | 38 | if repos_path[-1] in ['*', '**']: |
|
39 | 39 | repos_path = repos_path[:-1] |
|
40 | 40 | if repos_path[0] != '/': |
|
41 | 41 | repos_path[0] = '/' |
|
42 | 42 | self.repos_path = os.path.join(*repos_path) |
|
43 | 43 | |
|
44 | 44 | def backup_repos(self): |
|
45 | 45 | today = datetime.datetime.now().weekday() + 1 |
|
46 | 46 | self.backup_file_name = "mercurial_repos.%s.tar.gz" % today |
|
47 | 47 | bckp_file = os.path.join(self.backup_file_path, self.backup_file_name) |
|
48 | 48 | tar = tarfile.open(bckp_file, "w:gz") |
|
49 | 49 | |
|
50 | 50 | for dir in os.listdir(self.repos_path): |
|
51 | 51 | logging.info('backing up %s', dir) |
|
52 | 52 | tar.add(os.path.join(self.repos_path, dir), dir) |
|
53 | 53 | tar.close() |
|
54 | 54 | logging.info('finished backup of mercurial repositories') |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def transfer_files(self): |
|
59 | 59 | params = { |
|
60 | 60 | 'id_rsa_key': self.id_rsa_path, |
|
61 | 61 | 'backup_file_path':self.backup_file_path, |
|
62 | 62 | 'backup_file_name':self.backup_file_name, |
|
63 | 63 | } |
|
64 | 64 | cmd = ['scp', '-i', '%(id_rsa_key)s' % params, |
|
65 | 65 | '%(backup_file_path)s/%(backup_file_name)s' % params, |
|
66 | 66 | 'root@192.168.2.102:/backups/mercurial' % params] |
|
67 | 67 | |
|
68 | 68 | subprocess.Popen(cmd) |
|
69 | 69 | logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4]) |
|
70 | 70 | |
|
71 | 71 | |
|
72 | def rm_file(self): | |
|
73 | os.remove(self.backup_file_path) | |
|
74 | ||
|
75 | ||
|
76 | ||
|
72 | 77 | if __name__ == "__main__": |
|
73 | 78 | bm = BackupManager() |
|
74 | 79 | bm.backup_repos() |
|
75 | 80 | bm.transfer_files() |
|
81 | bm.rm_file() | |
|
76 | 82 | |
|
77 | 83 |
General Comments 0
You need to be logged in to leave comments.
Login now