comparison rhodecode/bin/rhodecode_backup.py @ 2776:63e58ef80ef1

Merge beta branch into stable
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 02 Sep 2012 21:19:54 +0200
parents rhodecode/lib/backup_manager.py@82a88013a3fd rhodecode/lib/backup_manager.py@7ac09514a178
children ffd45b185016
comparison
equal deleted inserted replaced
2301:9d097c2592d3 2776:63e58ef80ef1
1 # -*- coding: utf-8 -*-
2 """
3 rhodecode.bin.backup_manager
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 Repositories backup manager, it allows to backups all
7 repositories and send it to backup server using RSA key via ssh.
8
9 :created_on: Feb 28, 2010
10 :author: marcink
11 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
12 :license: GPLv3, see COPYING for more details.
13 """
14 # This program is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation, either version 3 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26
27 import os
28 import sys
29
30 import logging
31 import tarfile
32 import datetime
33 import subprocess
34
35 logging.basicConfig(level=logging.DEBUG,
36 format="%(asctime)s %(levelname)-5.5s %(message)s")
37
38
39 class BackupManager(object):
40 def __init__(self, repos_location, rsa_key, backup_server):
41 today = datetime.datetime.now().weekday() + 1
42 self.backup_file_name = "rhodecode_repos.%s.tar.gz" % today
43
44 self.id_rsa_path = self.get_id_rsa(rsa_key)
45 self.repos_path = self.get_repos_path(repos_location)
46 self.backup_server = backup_server
47
48 self.backup_file_path = '/tmp'
49
50 logging.info('starting backup for %s', self.repos_path)
51 logging.info('backup target %s', self.backup_file_path)
52
53 def get_id_rsa(self, rsa_key):
54 if not os.path.isfile(rsa_key):
55 logging.error('Could not load id_rsa key file in %s', rsa_key)
56 sys.exit()
57 return rsa_key
58
59 def get_repos_path(self, path):
60 if not os.path.isdir(path):
61 logging.error('Wrong location for repositories in %s', path)
62 sys.exit()
63 return path
64
65 def backup_repos(self):
66 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name)
67 tar = tarfile.open(bckp_file, "w:gz")
68
69 for dir_name in os.listdir(self.repos_path):
70 logging.info('backing up %s', dir_name)
71 tar.add(os.path.join(self.repos_path, dir_name), dir_name)
72 tar.close()
73 logging.info('finished backup of mercurial repositories')
74
75 def transfer_files(self):
76 params = {
77 'id_rsa_key': self.id_rsa_path,
78 'backup_file': os.path.join(self.backup_file_path,
79 self.backup_file_name),
80 'backup_server': self.backup_server
81 }
82 cmd = ['scp', '-l', '40000', '-i', '%(id_rsa_key)s' % params,
83 '%(backup_file)s' % params,
84 '%(backup_server)s' % params]
85
86 subprocess.call(cmd)
87 logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4])
88
89 def rm_file(self):
90 logging.info('Removing file %s', self.backup_file_name)
91 os.remove(os.path.join(self.backup_file_path, self.backup_file_name))
92
93 if __name__ == "__main__":
94
95 repo_location = '/home/repo_path'
96 backup_server = 'root@192.168.1.100:/backups/mercurial'
97 rsa_key = '/home/id_rsa'
98
99 B_MANAGER = BackupManager(repo_location, rsa_key, backup_server)
100 B_MANAGER.backup_repos()
101 B_MANAGER.transfer_files()
102 B_MANAGER.rm_file()