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