##// END OF EJS Templates
backup script update
Marcin Kuzminski -
r28:fd0c6fa2 default
parent child Browse files
Show More
@@ -1,54 +1,77 b''
1 1 import logging
2 2 from mercurial import config
3 3 import tarfile
4 4 import os
5 5 import datetime
6 6 import sys
7 import subprocess
7 8 logging.basicConfig(level = logging.DEBUG,
8 9 format = "%(asctime)s %(levelname)-5.5s %(message)s")
9 10
10 11 class BackupManager(object):
11 12 def __init__(self):
12
13 self.id_rsa_path = '/home/pylons/id_rsa'
14 self.check_id_rsa()
13 15 cur_dir = os.path.realpath(__file__)
14 16 dn = os.path.dirname
15 17 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data')
16 18 cfg = config.config()
17 19 try:
18 20 cfg.read(os.path.join(dn(dn(dn(cur_dir))), 'hgwebdir.config'))
19 21 except IOError:
20 22 logging.error('Could not read hgwebdir.config')
21 23 sys.exit()
22 24 self.set_repos_path(cfg.items('paths'))
23 25 logging.info('starting backup for %s', self.repos_path)
24 26 logging.info('backup target %s', self.backup_file_path)
25 27
26 28 if not os.path.isdir(self.repos_path):
27 29 raise Exception('Not a valid directory in %s' % self.repos_path)
28 30
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
29 36 def set_repos_path(self, paths):
30 37 repos_path = paths[0][1].split('/')
31 38 if repos_path[-1] in ['*', '**']:
32 39 repos_path = repos_path[:-1]
33 40 if repos_path[0] != '/':
34 41 repos_path[0] = '/'
35 42 self.repos_path = os.path.join(*repos_path)
36 43
37 44 def backup_repos(self):
38 45 today = datetime.datetime.now().weekday() + 1
39 bckp_file = os.path.join(self.backup_file_path,
40 "mercurial_repos.%s.tar.gz" % today)
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)
41 48 tar = tarfile.open(bckp_file, "w:gz")
42 49
43 for dir in os.listdir(self.repos_path):
50 for dir in os.listdir(self.repos_path)[:2]:
44 51 logging.info('backing up %s', dir)
45 52 tar.add(os.path.join(self.repos_path, dir), dir)
46 53 tar.close()
47 54 logging.info('finished backup of mercurial repositories')
48 55
49 56
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])
70
71
50 72 if __name__ == "__main__":
51 73 bm = BackupManager()
52 74 bm.backup_repos()
75 bm.transfer_files()
53 76
54 77
General Comments 0
You need to be logged in to leave comments. Login now