Mercurial > kallithea
annotate rhodecode/lib/backup_manager.py @ 1219:ca614631c2f8
fixed tests for stable
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Tue, 05 Apr 2011 20:09:34 +0200 |
parents | a3b2b4b4e440 |
children | bf263968da47 |
rev | line source |
---|---|
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
1 #!/usr/bin/env python |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
2 # encoding: utf-8 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
3 # mercurial repository backup manager |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
5 |
1217
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
7 # it under the terms of the GNU General Public License as published by |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
9 # (at your option) any later version. |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
10 # |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
14 # GNU General Public License for more details. |
1217
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
15 # |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
16 # You should have received a copy of the GNU General Public License |
1217
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
18 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
19 """ |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
20 Created on Feb 28, 2010 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
21 Mercurial repositories backup manager |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
22 @author: marcink |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
23 """ |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
24 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
25 |
25 | 26 import logging |
27 import tarfile | |
28 import os | |
29 import datetime | |
30 import sys | |
28 | 31 import subprocess |
33
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
32 logging.basicConfig(level=logging.DEBUG, |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
33 format="%(asctime)s %(levelname)-5.5s %(message)s") |
25 | 34 |
35 class BackupManager(object): | |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
36 def __init__(self, repos_location, rsa_key, backup_server): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
37 today = datetime.datetime.now().weekday() + 1 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
38 self.backup_file_name = "mercurial_repos.%s.tar.gz" % today |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
39 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
40 self.id_rsa_path = self.get_id_rsa(rsa_key) |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
41 self.repos_path = self.get_repos_path(repos_location) |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
42 self.backup_server = backup_server |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
43 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
44 self.backup_file_path = '/tmp' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
45 |
25 | 46 logging.info('starting backup for %s', self.repos_path) |
47 logging.info('backup target %s', self.backup_file_path) | |
48 | |
49 | |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
50 def get_id_rsa(self, rsa_key): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
51 if not os.path.isfile(rsa_key): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
52 logging.error('Could not load id_rsa key file in %s', rsa_key) |
28 | 53 sys.exit() |
440
9fe5ca6d72f6
fixed stupid typo in backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
434
diff
changeset
|
54 return rsa_key |
9fe5ca6d72f6
fixed stupid typo in backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
434
diff
changeset
|
55 |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
56 def get_repos_path(self, path): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
57 if not os.path.isdir(path): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
58 logging.error('Wrong location for repositories in %s', path) |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
59 sys.exit() |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
60 return path |
25 | 61 |
62 def backup_repos(self): | |
28 | 63 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name) |
25 | 64 tar = tarfile.open(bckp_file, "w:gz") |
65 | |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
66 for dir_name in os.listdir(self.repos_path): |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
67 logging.info('backing up %s', dir_name) |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
68 tar.add(os.path.join(self.repos_path, dir_name), dir_name) |
25 | 69 tar.close() |
26 | 70 logging.info('finished backup of mercurial repositories') |
25 | 71 |
72 | |
28 | 73 |
74 def transfer_files(self): | |
75 params = { | |
76 'id_rsa_key': self.id_rsa_path, | |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
77 'backup_file':os.path.join(self.backup_file_path, |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
78 self.backup_file_name), |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
79 'backup_server':self.backup_server |
28 | 80 } |
39 | 81 cmd = ['scp', '-l', '40000', '-i', '%(id_rsa_key)s' % params, |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
82 '%(backup_file)s' % params, |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
83 '%(backup_server)s' % params] |
28 | 84 |
35
c92f827280a3
Changed to blocking operation
Marcin Kuzminski <marcin@python-blog.com>
parents:
34
diff
changeset
|
85 subprocess.call(cmd) |
28 | 86 logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4]) |
33
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
87 |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
88 |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
89 def rm_file(self): |
36
846e2d8672c4
Added message when file removing
Marcin Kuzminski <marcin@python-blog.com>
parents:
35
diff
changeset
|
90 logging.info('Removing file %s', self.backup_file_name) |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
91 os.remove(os.path.join(self.backup_file_path, self.backup_file_name)) |
33
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
92 |
28 | 93 |
94 | |
25 | 95 if __name__ == "__main__": |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
96 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
97 repo_location = '/home/repo_path' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
98 backup_server = 'root@192.168.1.100:/backups/mercurial' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
99 rsa_key = '/home/id_rsa' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
100 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
101 B_MANAGER = BackupManager(repo_location, rsa_key, backup_server) |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
102 B_MANAGER.backup_repos() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
103 B_MANAGER.transfer_files() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
104 B_MANAGER.rm_file() |
25 | 105 |
106 |