Show More
@@ -1,114 +1,107 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | # mercurial repository backup manager |
|
4 | 4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
5 | 5 | |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; version 2 |
|
9 | 9 | # of the License or (at your opinion) any later version of the license. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 | 19 | # MA 02110-1301, USA. |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | Created on Feb 28, 2010 |
|
23 | 23 | Mercurial repositories backup manager |
|
24 | 24 | @author: marcink |
|
25 | 25 | """ |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | import logging |
|
29 | from mercurial import config | |
|
30 | 29 | import tarfile |
|
31 | 30 | import os |
|
32 | 31 | import datetime |
|
33 | 32 | import sys |
|
34 | 33 | import subprocess |
|
35 | 34 | logging.basicConfig(level=logging.DEBUG, |
|
36 | 35 | format="%(asctime)s %(levelname)-5.5s %(message)s") |
|
37 | 36 | |
|
38 | 37 | class BackupManager(object): |
|
39 |
def __init__(self, |
|
|
40 | self.repos_path = None | |
|
41 |
self.backup_file_name = |
|
|
42 | self.id_rsa_path = id_rsa_path | |
|
43 |
self. |
|
|
44 | cur_dir = os.path.realpath(__file__) | |
|
45 | dn = os.path.dirname | |
|
46 | self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data') | |
|
47 | cfg = config.config() | |
|
48 | try: | |
|
49 | cfg.read(os.path.join(dn(dn(dn(cur_dir))), repo_conf)) | |
|
50 | except IOError: | |
|
51 | logging.error('Could not read %s', repo_conf) | |
|
52 | sys.exit() | |
|
53 | self.set_repos_path(cfg.items('paths')) | |
|
38 | def __init__(self, repos_location, rsa_key, backup_server): | |
|
39 | today = datetime.datetime.now().weekday() + 1 | |
|
40 | self.backup_file_name = "mercurial_repos.%s.tar.gz" % today | |
|
41 | ||
|
42 | self.id_rsa_path = self.get_id_rsa(rsa_key) | |
|
43 | self.repos_path = self.get_repos_path(repos_location) | |
|
44 | self.backup_server = backup_server | |
|
45 | ||
|
46 | self.backup_file_path = '/tmp' | |
|
47 | ||
|
54 | 48 | logging.info('starting backup for %s', self.repos_path) |
|
55 | 49 | logging.info('backup target %s', self.backup_file_path) |
|
56 | 50 | |
|
57 | if not os.path.isdir(self.repos_path): | |
|
58 | raise Exception('Not a valid directory in %s' % self.repos_path) | |
|
59 | 51 | |
|
60 |
def |
|
|
61 |
if not os.path.isfile( |
|
|
62 | logging.error('Could not load id_rsa key file in %s', | |
|
63 | self.id_rsa_path) | |
|
52 | def get_id_rsa(self, rsa_key): | |
|
53 | if not os.path.isfile(rsa_key): | |
|
54 | logging.error('Could not load id_rsa key file in %s', rsa_key) | |
|
64 | 55 | sys.exit() |
|
65 | 56 | |
|
66 |
def |
|
|
67 | repos_path = paths[0][1].split('/') | |
|
68 | if repos_path[-1] in ['*', '**']: | |
|
69 | repos_path = repos_path[:-1] | |
|
70 | if repos_path[0] != '/': | |
|
71 | repos_path[0] = '/' | |
|
72 | self.repos_path = os.path.join(*repos_path) | |
|
57 | def get_repos_path(self, path): | |
|
58 | if not os.path.isdir(path): | |
|
59 | logging.error('Wrong location for repositories in %s', path) | |
|
60 | sys.exit() | |
|
61 | return path | |
|
73 | 62 | |
|
74 | 63 | def backup_repos(self): |
|
75 | today = datetime.datetime.now().weekday() + 1 | |
|
76 | self.backup_file_name = "mercurial_repos.%s.tar.gz" % today | |
|
77 | 64 | bckp_file = os.path.join(self.backup_file_path, self.backup_file_name) |
|
78 | 65 | tar = tarfile.open(bckp_file, "w:gz") |
|
79 | 66 | |
|
80 | 67 | for dir_name in os.listdir(self.repos_path): |
|
81 | 68 | logging.info('backing up %s', dir_name) |
|
82 | 69 | tar.add(os.path.join(self.repos_path, dir_name), dir_name) |
|
83 | 70 | tar.close() |
|
84 | 71 | logging.info('finished backup of mercurial repositories') |
|
85 | 72 | |
|
86 | 73 | |
|
87 | 74 | |
|
88 | 75 | def transfer_files(self): |
|
89 | 76 | params = { |
|
90 | 77 | 'id_rsa_key': self.id_rsa_path, |
|
91 |
'backup_file |
|
|
92 |
|
|
|
78 | 'backup_file':os.path.join(self.backup_file_path, | |
|
79 | self.backup_file_name), | |
|
80 | 'backup_server':self.backup_server | |
|
93 | 81 | } |
|
94 | 82 | cmd = ['scp', '-l', '40000', '-i', '%(id_rsa_key)s' % params, |
|
95 |
'%(backup_file |
|
|
96 |
' |
|
|
83 | '%(backup_file)s' % params, | |
|
84 | '%(backup_server)s' % params] | |
|
97 | 85 | |
|
98 | 86 | subprocess.call(cmd) |
|
99 | 87 | logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4]) |
|
100 | 88 | |
|
101 | 89 | |
|
102 | 90 | def rm_file(self): |
|
103 | 91 | logging.info('Removing file %s', self.backup_file_name) |
|
104 | 92 | os.remove(os.path.join(self.backup_file_path, self.backup_file_name)) |
|
105 | 93 | |
|
106 | 94 | |
|
107 | 95 | |
|
108 | 96 | if __name__ == "__main__": |
|
109 | B_MANAGER = BackupManager('/home/pylons/id_rsa', 'repositories.config') | |
|
97 | ||
|
98 | repo_location = '/home/repo_path' | |
|
99 | backup_server = 'root@192.168.1.100:/backups/mercurial' | |
|
100 | rsa_key = '/home/id_rsa' | |
|
101 | ||
|
102 | B_MANAGER = BackupManager(repo_location, rsa_key, backup_server) | |
|
110 | 103 | B_MANAGER.backup_repos() |
|
111 | 104 | B_MANAGER.transfer_files() |
|
112 | 105 | B_MANAGER.rm_file() |
|
113 | 106 | |
|
114 | 107 |
General Comments 0
You need to be logged in to leave comments.
Login now