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