##// END OF EJS Templates
Changed to blocking operation
Marcin Kuzminski -
r35:c92f8272 default
parent child Browse files
Show More
@@ -1,87 +1,87 b''
1 '''BACKUP MANAGER'''
1 '''BACKUP MANAGER'''
2 import logging
2 import logging
3 from mercurial import config
3 from mercurial import config
4 import tarfile
4 import tarfile
5 import os
5 import os
6 import datetime
6 import datetime
7 import sys
7 import sys
8 import subprocess
8 import subprocess
9 logging.basicConfig(level=logging.DEBUG,
9 logging.basicConfig(level=logging.DEBUG,
10 format="%(asctime)s %(levelname)-5.5s %(message)s")
10 format="%(asctime)s %(levelname)-5.5s %(message)s")
11
11
12 class BackupManager(object):
12 class BackupManager(object):
13 def __init__(self):
13 def __init__(self):
14 self.repos_path = None
14 self.repos_path = None
15 self.backup_file_name = None
15 self.backup_file_name = None
16 self.id_rsa_path = '/home/pylons/id_rsa'
16 self.id_rsa_path = '/home/pylons/id_rsa'
17 self.check_id_rsa()
17 self.check_id_rsa()
18 cur_dir = os.path.realpath(__file__)
18 cur_dir = os.path.realpath(__file__)
19 dn = os.path.dirname
19 dn = os.path.dirname
20 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data')
20 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data')
21 cfg = config.config()
21 cfg = config.config()
22 try:
22 try:
23 cfg.read(os.path.join(dn(dn(dn(cur_dir))), 'hgwebdir.config'))
23 cfg.read(os.path.join(dn(dn(dn(cur_dir))), 'hgwebdir.config'))
24 except IOError:
24 except IOError:
25 logging.error('Could not read hgwebdir.config')
25 logging.error('Could not read hgwebdir.config')
26 sys.exit()
26 sys.exit()
27 self.set_repos_path(cfg.items('paths'))
27 self.set_repos_path(cfg.items('paths'))
28 logging.info('starting backup for %s', self.repos_path)
28 logging.info('starting backup for %s', self.repos_path)
29 logging.info('backup target %s', self.backup_file_path)
29 logging.info('backup target %s', self.backup_file_path)
30
30
31 if not os.path.isdir(self.repos_path):
31 if not os.path.isdir(self.repos_path):
32 raise Exception('Not a valid directory in %s' % self.repos_path)
32 raise Exception('Not a valid directory in %s' % self.repos_path)
33
33
34 def check_id_rsa(self):
34 def check_id_rsa(self):
35 if not os.path.isfile(self.id_rsa_path):
35 if not os.path.isfile(self.id_rsa_path):
36 logging.error('Could not load id_rsa key file in %s',
36 logging.error('Could not load id_rsa key file in %s',
37 self.id_rsa_path)
37 self.id_rsa_path)
38 sys.exit()
38 sys.exit()
39
39
40 def set_repos_path(self, paths):
40 def set_repos_path(self, paths):
41 repos_path = paths[0][1].split('/')
41 repos_path = paths[0][1].split('/')
42 if repos_path[-1] in ['*', '**']:
42 if repos_path[-1] in ['*', '**']:
43 repos_path = repos_path[:-1]
43 repos_path = repos_path[:-1]
44 if repos_path[0] != '/':
44 if repos_path[0] != '/':
45 repos_path[0] = '/'
45 repos_path[0] = '/'
46 self.repos_path = os.path.join(*repos_path)
46 self.repos_path = os.path.join(*repos_path)
47
47
48 def backup_repos(self):
48 def backup_repos(self):
49 today = datetime.datetime.now().weekday() + 1
49 today = datetime.datetime.now().weekday() + 1
50 self.backup_file_name = "mercurial_repos.%s.tar.gz" % today
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)
51 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name)
52 tar = tarfile.open(bckp_file, "w:gz")
52 tar = tarfile.open(bckp_file, "w:gz")
53
53
54 for dir_name in os.listdir(self.repos_path):
54 for dir_name in os.listdir(self.repos_path):
55 logging.info('backing up %s', dir_name)
55 logging.info('backing up %s', dir_name)
56 tar.add(os.path.join(self.repos_path, dir_name), dir_name)
56 tar.add(os.path.join(self.repos_path, dir_name), dir_name)
57 tar.close()
57 tar.close()
58 logging.info('finished backup of mercurial repositories')
58 logging.info('finished backup of mercurial repositories')
59
59
60
60
61
61
62 def transfer_files(self):
62 def transfer_files(self):
63 params = {
63 params = {
64 'id_rsa_key': self.id_rsa_path,
64 'id_rsa_key': self.id_rsa_path,
65 'backup_file_path':self.backup_file_path,
65 'backup_file_path':self.backup_file_path,
66 'backup_file_name':self.backup_file_name,
66 'backup_file_name':self.backup_file_name,
67 }
67 }
68 cmd = ['scp', '-i', '%(id_rsa_key)s' % params,
68 cmd = ['scp', '-i', '%(id_rsa_key)s' % params,
69 '%(backup_file_path)s/%(backup_file_name)s' % params,
69 '%(backup_file_path)s/%(backup_file_name)s' % params,
70 'root@192.168.2.102:/backups/mercurial' % params]
70 'root@192.168.2.102:/backups/mercurial' % params]
71
71
72 subprocess.Popen(cmd)
72 subprocess.call(cmd)
73 logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4])
73 logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4])
74
74
75
75
76 def rm_file(self):
76 def rm_file(self):
77 os.remove(os.path.join(self.backup_file_path, self.backup_file_name))
77 os.remove(os.path.join(self.backup_file_path, self.backup_file_name))
78
78
79
79
80
80
81 if __name__ == "__main__":
81 if __name__ == "__main__":
82 B_MANAGER = BackupManager()
82 B_MANAGER = BackupManager()
83 B_MANAGER.backup_repos()
83 B_MANAGER.backup_repos()
84 B_MANAGER.transfer_files()
84 B_MANAGER.transfer_files()
85 B_MANAGER.rm_file()
85 B_MANAGER.rm_file()
86
86
87
87
General Comments 0
You need to be logged in to leave comments. Login now