Show More
@@ -1,107 +1,108 | |||
|
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 | 29 | import tarfile |
|
30 | 30 | import os |
|
31 | 31 | import datetime |
|
32 | 32 | import sys |
|
33 | 33 | import subprocess |
|
34 | 34 | logging.basicConfig(level=logging.DEBUG, |
|
35 | 35 | format="%(asctime)s %(levelname)-5.5s %(message)s") |
|
36 | 36 | |
|
37 | 37 | class BackupManager(object): |
|
38 | 38 | def __init__(self, repos_location, rsa_key, backup_server): |
|
39 | 39 | today = datetime.datetime.now().weekday() + 1 |
|
40 | 40 | self.backup_file_name = "mercurial_repos.%s.tar.gz" % today |
|
41 | 41 | |
|
42 | 42 | self.id_rsa_path = self.get_id_rsa(rsa_key) |
|
43 | 43 | self.repos_path = self.get_repos_path(repos_location) |
|
44 | 44 | self.backup_server = backup_server |
|
45 | 45 | |
|
46 | 46 | self.backup_file_path = '/tmp' |
|
47 | 47 | |
|
48 | 48 | logging.info('starting backup for %s', self.repos_path) |
|
49 | 49 | logging.info('backup target %s', self.backup_file_path) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | def get_id_rsa(self, rsa_key): |
|
53 | 53 | if not os.path.isfile(rsa_key): |
|
54 | 54 | logging.error('Could not load id_rsa key file in %s', rsa_key) |
|
55 | 55 | sys.exit() |
|
56 | ||
|
56 | return rsa_key | |
|
57 | ||
|
57 | 58 | def get_repos_path(self, path): |
|
58 | 59 | if not os.path.isdir(path): |
|
59 | 60 | logging.error('Wrong location for repositories in %s', path) |
|
60 | 61 | sys.exit() |
|
61 | 62 | return path |
|
62 | 63 | |
|
63 | 64 | def backup_repos(self): |
|
64 | 65 | bckp_file = os.path.join(self.backup_file_path, self.backup_file_name) |
|
65 | 66 | tar = tarfile.open(bckp_file, "w:gz") |
|
66 | 67 | |
|
67 | 68 | for dir_name in os.listdir(self.repos_path): |
|
68 | 69 | logging.info('backing up %s', dir_name) |
|
69 | 70 | tar.add(os.path.join(self.repos_path, dir_name), dir_name) |
|
70 | 71 | tar.close() |
|
71 | 72 | logging.info('finished backup of mercurial repositories') |
|
72 | 73 | |
|
73 | 74 | |
|
74 | 75 | |
|
75 | 76 | def transfer_files(self): |
|
76 | 77 | params = { |
|
77 | 78 | 'id_rsa_key': self.id_rsa_path, |
|
78 | 79 | 'backup_file':os.path.join(self.backup_file_path, |
|
79 | 80 | self.backup_file_name), |
|
80 | 81 | 'backup_server':self.backup_server |
|
81 | 82 | } |
|
82 | 83 | cmd = ['scp', '-l', '40000', '-i', '%(id_rsa_key)s' % params, |
|
83 | 84 | '%(backup_file)s' % params, |
|
84 | 85 | '%(backup_server)s' % params] |
|
85 | 86 | |
|
86 | 87 | subprocess.call(cmd) |
|
87 | 88 | logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4]) |
|
88 | 89 | |
|
89 | 90 | |
|
90 | 91 | def rm_file(self): |
|
91 | 92 | logging.info('Removing file %s', self.backup_file_name) |
|
92 | 93 | os.remove(os.path.join(self.backup_file_path, self.backup_file_name)) |
|
93 | 94 | |
|
94 | 95 | |
|
95 | 96 | |
|
96 | 97 | if __name__ == "__main__": |
|
97 | 98 | |
|
98 | 99 | repo_location = '/home/repo_path' |
|
99 | 100 | backup_server = 'root@192.168.1.100:/backups/mercurial' |
|
100 | 101 | rsa_key = '/home/id_rsa' |
|
101 | 102 | |
|
102 | 103 | B_MANAGER = BackupManager(repo_location, rsa_key, backup_server) |
|
103 | 104 | B_MANAGER.backup_repos() |
|
104 | 105 | B_MANAGER.transfer_files() |
|
105 | 106 | B_MANAGER.rm_file() |
|
106 | 107 | |
|
107 | 108 |
General Comments 0
You need to be logged in to leave comments.
Login now