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