Mercurial > kallithea
annotate rhodecode/lib/backup_manager.py @ 759:a7f50911a945 beta
Models code cleanups
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 25 Nov 2010 22:16:29 +0100 |
parents | 1e757ac98988 |
children | 07a6e8c65526 a3b2b4b4e440 |
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 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
6 # This program is free software; you can redistribute it and/or |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
7 # modify it under the terms of the GNU General Public License |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
8 # as published by the Free Software Foundation; version 2 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
9 # of the License or (at your opinion) any later version of the license. |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
10 # |
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. |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
15 # |
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 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
17 # along with this program; if not, write to the Free Software |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
19 # MA 02110-1301, USA. |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
20 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
21 """ |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
22 Created on Feb 28, 2010 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
23 Mercurial repositories backup manager |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
24 @author: marcink |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
25 """ |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
26 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
27 |
25 | 28 import logging |
29 import tarfile | |
30 import os | |
31 import datetime | |
32 import sys | |
28 | 33 import subprocess |
33
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
34 logging.basicConfig(level=logging.DEBUG, |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
35 format="%(asctime)s %(levelname)-5.5s %(message)s") |
25 | 36 |
37 class BackupManager(object): | |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
38 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
|
39 today = datetime.datetime.now().weekday() + 1 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
40 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
|
41 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
42 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
|
43 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
|
44 self.backup_server = backup_server |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
45 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
46 self.backup_file_path = '/tmp' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
47 |
25 | 48 logging.info('starting backup for %s', self.repos_path) |
49 logging.info('backup target %s', self.backup_file_path) | |
50 | |
51 | |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
52 def get_id_rsa(self, rsa_key): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
53 if not os.path.isfile(rsa_key): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
54 logging.error('Could not load id_rsa key file in %s', rsa_key) |
28 | 55 sys.exit() |
440
9fe5ca6d72f6
fixed stupid typo in backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
434
diff
changeset
|
56 return rsa_key |
9fe5ca6d72f6
fixed stupid typo in backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
434
diff
changeset
|
57 |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
58 def get_repos_path(self, path): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
59 if not os.path.isdir(path): |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
60 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
|
61 sys.exit() |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
62 return path |
25 | 63 |
64 def backup_repos(self): | |
28 | 65 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name) |
25 | 66 tar = tarfile.open(bckp_file, "w:gz") |
67 | |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
68 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
|
69 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
|
70 tar.add(os.path.join(self.repos_path, dir_name), dir_name) |
25 | 71 tar.close() |
26 | 72 logging.info('finished backup of mercurial repositories') |
25 | 73 |
74 | |
28 | 75 |
76 def transfer_files(self): | |
77 params = { | |
78 'id_rsa_key': self.id_rsa_path, | |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
79 '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
|
80 self.backup_file_name), |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
81 'backup_server':self.backup_server |
28 | 82 } |
39 | 83 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
|
84 '%(backup_file)s' % params, |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
85 '%(backup_server)s' % params] |
28 | 86 |
35
c92f827280a3
Changed to blocking operation
Marcin Kuzminski <marcin@python-blog.com>
parents:
34
diff
changeset
|
87 subprocess.call(cmd) |
28 | 88 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
|
89 |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
90 |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
91 def rm_file(self): |
36
846e2d8672c4
Added message when file removing
Marcin Kuzminski <marcin@python-blog.com>
parents:
35
diff
changeset
|
92 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
|
93 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
|
94 |
28 | 95 |
96 | |
25 | 97 if __name__ == "__main__": |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
98 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
99 repo_location = '/home/repo_path' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
100 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
|
101 rsa_key = '/home/id_rsa' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
102 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
103 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
|
104 B_MANAGER.backup_repos() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
105 B_MANAGER.transfer_files() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
106 B_MANAGER.rm_file() |
25 | 107 |
108 |