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