Mercurial > kallithea
annotate rhodecode/lib/backup_manager.py @ 1307:c1516b35f91d beta
pep8ify
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Tue, 03 May 2011 16:54:43 +0200 |
parents | a671db5bdd58 |
children | bf263968da47 2afe9320d5e6 |
rev | line source |
---|---|
1307 | 1 # -*- coding: utf-8 -*- |
2 """ | |
3 rhodecode.lib.backup_manager | |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
5 |
1307 | 6 Mercurial repositories backup manager, it allows to backups all |
7 repositories and send it to backup server using RSA key via ssh. | |
8 | |
9 :created_on: Feb 28, 2010 | |
10 :copyright: (c) 2010 by marcink. | |
11 :license: LICENSE_NAME, see LICENSE_FILE for more details. | |
12 """ | |
1206
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
13 # This program is free software: you can redistribute it and/or modify |
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
14 # it under the terms of the GNU General Public License as published by |
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
15 # the Free Software Foundation, either version 3 of the License, or |
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
16 # (at your option) any later version. |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
17 # |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
18 # 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
|
19 # 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
|
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
21 # GNU General Public License for more details. |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
22 # |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
23 # You should have received a copy of the GNU General Public License |
1206
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
24 # 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
|
25 |
1307 | 26 import os |
27 import sys | |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
241
diff
changeset
|
28 |
25 | 29 import logging |
30 import tarfile | |
31 import datetime | |
28 | 32 import subprocess |
1307 | 33 |
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 |
1307 | 37 |
25 | 38 class BackupManager(object): |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
39 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
|
40 today = datetime.datetime.now().weekday() + 1 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
41 self.backup_file_name = "mercurial_repos.%s.tar.gz" % today |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
42 |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
43 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
|
44 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
|
45 self.backup_server = backup_server |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
46 |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
47 self.backup_file_path = '/tmp' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
48 |
25 | 49 logging.info('starting backup for %s', self.repos_path) |
50 logging.info('backup target %s', self.backup_file_path) | |
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 |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
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 |
28 | 74 def transfer_files(self): |
75 params = { | |
76 'id_rsa_key': self.id_rsa_path, | |
1307 | 77 'backup_file': os.path.join(self.backup_file_path, |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
78 self.backup_file_name), |
1307 | 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]) |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
87 |
33
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
88 def rm_file(self): |
36
846e2d8672c4
Added message when file removing
Marcin Kuzminski <marcin@python-blog.com>
parents:
35
diff
changeset
|
89 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
|
90 os.remove(os.path.join(self.backup_file_path, self.backup_file_name)) |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
91 |
25 | 92 if __name__ == "__main__": |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
93 |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
94 repo_location = '/home/repo_path' |
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
95 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
|
96 rsa_key = '/home/id_rsa' |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
97 |
434
bd3f341cad45
simplified and updated backup manager
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
98 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
|
99 B_MANAGER.backup_repos() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
100 B_MANAGER.transfer_files() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
101 B_MANAGER.rm_file() |