comparison pylons_app/lib/backup_manager.py @ 28:fd0c6fa2df82

backup script update
author Marcin Kuzminski
date Mon, 01 Mar 2010 12:29:27 +0100
parents 8f29ddc4c147
children 468e226bbaa5
comparison
equal deleted inserted replaced
27:8f29ddc4c147 28:fd0c6fa2df82
2 from mercurial import config 2 from mercurial import config
3 import tarfile 3 import tarfile
4 import os 4 import os
5 import datetime 5 import datetime
6 import sys 6 import sys
7 import subprocess
7 logging.basicConfig(level = logging.DEBUG, 8 logging.basicConfig(level = logging.DEBUG,
8 format = "%(asctime)s %(levelname)-5.5s %(message)s") 9 format = "%(asctime)s %(levelname)-5.5s %(message)s")
9 10
10 class BackupManager(object): 11 class BackupManager(object):
11 def __init__(self): 12 def __init__(self):
12 13 self.id_rsa_path = '/home/pylons/id_rsa'
14 self.check_id_rsa()
13 cur_dir = os.path.realpath(__file__) 15 cur_dir = os.path.realpath(__file__)
14 dn = os.path.dirname 16 dn = os.path.dirname
15 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data') 17 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data')
16 cfg = config.config() 18 cfg = config.config()
17 try: 19 try:
24 logging.info('backup target %s', self.backup_file_path) 26 logging.info('backup target %s', self.backup_file_path)
25 27
26 if not os.path.isdir(self.repos_path): 28 if not os.path.isdir(self.repos_path):
27 raise Exception('Not a valid directory in %s' % self.repos_path) 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 def set_repos_path(self, paths): 36 def set_repos_path(self, paths):
30 repos_path = paths[0][1].split('/') 37 repos_path = paths[0][1].split('/')
31 if repos_path[-1] in ['*', '**']: 38 if repos_path[-1] in ['*', '**']:
32 repos_path = repos_path[:-1] 39 repos_path = repos_path[:-1]
33 if repos_path[0] != '/': 40 if repos_path[0] != '/':
34 repos_path[0] = '/' 41 repos_path[0] = '/'
35 self.repos_path = os.path.join(*repos_path) 42 self.repos_path = os.path.join(*repos_path)
36 43
37 def backup_repos(self): 44 def backup_repos(self):
38 today = datetime.datetime.now().weekday() + 1 45 today = datetime.datetime.now().weekday() + 1
39 bckp_file = os.path.join(self.backup_file_path, 46 self.backup_file_name = "mercurial_repos.%s.tar.gz" % today
40 "mercurial_repos.%s.tar.gz" % today) 47 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name)
41 tar = tarfile.open(bckp_file, "w:gz") 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 logging.info('backing up %s', dir) 51 logging.info('backing up %s', dir)
45 tar.add(os.path.join(self.repos_path, dir), dir) 52 tar.add(os.path.join(self.repos_path, dir), dir)
46 tar.close() 53 tar.close()
47 logging.info('finished backup of mercurial repositories') 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 if __name__ == "__main__": 72 if __name__ == "__main__":
51 bm = BackupManager() 73 bm = BackupManager()
52 bm.backup_repos() 74 bm.backup_repos()
75 bm.transfer_files()
53 76
54 77