Mercurial > kallithea
annotate pylons_app/lib/backup_manager.py @ 38:7f88d7088f9c
mercurial added limit for sending backup file
author | marcink |
---|---|
date | Fri, 02 Apr 2010 12:46:35 +0200 |
parents | 846e2d8672c4 |
children | 787e7d307b69 |
rev | line source |
---|---|
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
1 '''BACKUP MANAGER''' |
25 | 2 import logging |
3 from mercurial import config | |
4 import tarfile | |
5 import os | |
6 import datetime | |
7 import sys | |
28 | 8 import subprocess |
33
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
9 logging.basicConfig(level=logging.DEBUG, |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
10 format="%(asctime)s %(levelname)-5.5s %(message)s") |
25 | 11 |
12 class BackupManager(object): | |
13 def __init__(self): | |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
14 self.repos_path = None |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
15 self.backup_file_name = None |
28 | 16 self.id_rsa_path = '/home/pylons/id_rsa' |
17 self.check_id_rsa() | |
27 | 18 cur_dir = os.path.realpath(__file__) |
25 | 19 dn = os.path.dirname |
27 | 20 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data') |
25 | 21 cfg = config.config() |
22 try: | |
27 | 23 cfg.read(os.path.join(dn(dn(dn(cur_dir))), 'hgwebdir.config')) |
25 | 24 except IOError: |
25 logging.error('Could not read hgwebdir.config') | |
26 sys.exit() | |
27 self.set_repos_path(cfg.items('paths')) | |
28 logging.info('starting backup for %s', self.repos_path) | |
29 logging.info('backup target %s', self.backup_file_path) | |
30 | |
31 if not os.path.isdir(self.repos_path): | |
32 raise Exception('Not a valid directory in %s' % self.repos_path) | |
33 | |
28 | 34 def check_id_rsa(self): |
35 if not os.path.isfile(self.id_rsa_path): | |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
36 logging.error('Could not load id_rsa key file in %s', |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
37 self.id_rsa_path) |
28 | 38 sys.exit() |
39 | |
25 | 40 def set_repos_path(self, paths): |
41 repos_path = paths[0][1].split('/') | |
42 if repos_path[-1] in ['*', '**']: | |
43 repos_path = repos_path[:-1] | |
44 if repos_path[0] != '/': | |
45 repos_path[0] = '/' | |
46 self.repos_path = os.path.join(*repos_path) | |
47 | |
48 def backup_repos(self): | |
49 today = datetime.datetime.now().weekday() + 1 | |
28 | 50 self.backup_file_name = "mercurial_repos.%s.tar.gz" % today |
51 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name) | |
25 | 52 tar = tarfile.open(bckp_file, "w:gz") |
53 | |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
54 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
|
55 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
|
56 tar.add(os.path.join(self.repos_path, dir_name), dir_name) |
25 | 57 tar.close() |
26 | 58 logging.info('finished backup of mercurial repositories') |
25 | 59 |
60 | |
28 | 61 |
62 def transfer_files(self): | |
63 params = { | |
64 'id_rsa_key': self.id_rsa_path, | |
65 'backup_file_path':self.backup_file_path, | |
66 'backup_file_name':self.backup_file_name, | |
67 } | |
38 | 68 cmd = ['scp', '-l', 40000, '-i', '%(id_rsa_key)s' % params, |
28 | 69 '%(backup_file_path)s/%(backup_file_name)s' % params, |
70 'root@192.168.2.102:/backups/mercurial' % params] | |
71 | |
35
c92f827280a3
Changed to blocking operation
Marcin Kuzminski <marcin@python-blog.com>
parents:
34
diff
changeset
|
72 subprocess.call(cmd) |
28 | 73 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
|
74 |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
75 |
22b2234e51cd
Added removing of files from data
Marcin Kuzminski <marcin@python-blog.com>
parents:
29
diff
changeset
|
76 def rm_file(self): |
36
846e2d8672c4
Added message when file removing
Marcin Kuzminski <marcin@python-blog.com>
parents:
35
diff
changeset
|
77 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
|
78 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
|
79 |
28 | 80 |
81 | |
25 | 82 if __name__ == "__main__": |
34
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
83 B_MANAGER = BackupManager() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
84 B_MANAGER.backup_repos() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
85 B_MANAGER.transfer_files() |
b4b25ece7797
Code refactoring, and changed proper way of removing files
Marcin Kuzminski <marcin@python-blog.com>
parents:
33
diff
changeset
|
86 B_MANAGER.rm_file() |
25 | 87 |
88 |