##// END OF EJS Templates
r435:0e8ef6f1 merge default
parent child Browse files
Show More
@@ -1,60 +1,61 b''
1 #!/sbin/runscript
1 #!/sbin/runscript
2 ########################################
2 ########################################
3 #### THIS IS AN GENTOO INIT.D SCRIPT####
3 #### THIS IS AN GENTOO INIT.D SCRIPT####
4 ########################################
4 ########################################
5
5
6 APP_NAME="hg_app"
6 APP_NAME="hg_app"
7 APP_HOMEDIR="marcink/python_workspace"
7 APP_HOMEDIR="marcink/python_workspace"
8 APP_PATH="/home/$APP_HOMEDIR/$APP_NAME"
8 APP_PATH="/home/$APP_HOMEDIR/$APP_NAME"
9
9
10 CONF_NAME="production.ini"
10 CONF_NAME="production.ini"
11
11
12 PID_PATH="$APP_PATH/$APP_NAME.pid"
12 PID_PATH="$APP_PATH/$APP_NAME.pid"
13 LOG_PATH="$APP_PATH/$APP_NAME.log"
13 LOG_PATH="$APP_PATH/$APP_NAME.log"
14
14
15 PYTHON_PATH="/home/$APP_HOMEDIR/v-env"
15 PYTHON_PATH="/home/$APP_HOMEDIR/v-env"
16
16
17 RUN_AS="marcink"
17 RUN_AS="marcink"
18
18
19 DAEMON="$PYTHON_PATH/bin/paster"
19 DAEMON="$PYTHON_PATH/bin/paster"
20
20
21 DAEMON_OPTS="serve --daemon \
21 DAEMON_OPTS="serve --daemon \
22 --user=$RUN_AS \
22 --user=$RUN_AS \
23 --group=$RUN_AS \
23 --group=$RUN_AS \
24 --pid-file=$PID_PATH \
24 --pid-file=$PID_PATH \
25 --log-file=$LOG_PATH $APP_PATH/$CONF_NAME"
25 --log-file=$LOG_PATH $APP_PATH/$CONF_NAME"
26
26
27 #extra options
27 #extra options
28 opts="${opts} restartdelay"
28 opts="${opts} restartdelay"
29
29
30 depend() {
30 depend() {
31 need nginx
31 need nginx
32 }
32 }
33
33
34 start() {
34 start() {
35 ebegin "Starting $APP_NAME"
35 ebegin "Starting $APP_NAME"
36 cd $APP_PATH
36 start-stop-daemon -d $APP_PATH -e PYTHON_EGG_CACHE="/tmp" \
37 start-stop-daemon --start --quiet\
37 --start --quiet \
38 --pidfile $PID_PATH \
38 --pidfile $PID_PATH \
39 --user $RUN_AS \
39 --user $RUN_AS \
40 --exec $DAEMON -- $DAEMON_OPTS
40 --exec $DAEMON -- $DAEMON_OPTS
41 eend $?
41 eend $?
42 }
42 }
43
43
44 stop() {
44 stop() {
45 ebegin "Stopping $APP_NAME"
45 ebegin "Stopping $APP_NAME"
46 start-stop-daemon --stop --quiet \
46 start-stop-daemon -d $APP_PATH \
47 --stop --quiet \
47 --pidfile $PID_PATH || echo "$APP_NAME - Not running!"
48 --pidfile $PID_PATH || echo "$APP_NAME - Not running!"
48 if [ -f $PID_PATH ]; then
49 if [ -f $PID_PATH ]; then
49 rm $PID_PATH
50 rm $PID_PATH
50 fi
51 fi
51 eend $?
52 eend $?
52 }
53 }
53
54
54 restartdelay() {
55 restartdelay() {
55 #stop()
56 #stop()
56 echo "sleep3"
57 echo "sleep3"
57 sleep 3
58 sleep 3
58
59
59 #start()
60 #start()
60 }
61 }
@@ -1,72 +1,77 b''
1 #!/bin/sh -e
1 #!/bin/sh -e
2 ########################################
2 ########################################
3 #### THIS IS AN DEBIAN INIT.D SCRIPT####
3 #### THIS IS AN DEBIAN INIT.D SCRIPT####
4 ########################################
4 ########################################
5
5
6 ### BEGIN INIT INFO
6 ### BEGIN INIT INFO
7 # Provides: hg-app
7 # Provides: hg-app
8 # Required-Start: $all
8 # Required-Start: $all
9 # Required-Stop: $all
9 # Required-Stop: $all
10 # Default-Start: 2 3 4 5
10 # Default-Start: 2 3 4 5
11 # Default-Stop: 0 1 6
11 # Default-Stop: 0 1 6
12 # Short-Description: starts instance of hg-app
12 # Short-Description: starts instance of hg-app
13 # Description: starts instance of hg-app using start-stop-daemon
13 # Description: starts instance of hg-app using start-stop-daemon
14 ### END INIT INFO
14 ### END INIT INFO
15
15
16 APP_NAME="hg_app"
16 APP_NAME="hg_app"
17 APP_HOMEDIR="marcink/python_workspace"
17 APP_HOMEDIR="marcink/python_workspace"
18 APP_PATH="/home/$APP_HOMEDIR/$APP_NAME"
18 APP_PATH="/home/$APP_HOMEDIR/$APP_NAME"
19
19
20 CONF_NAME="production.ini"
20 CONF_NAME="production.ini"
21
21
22 PID_PATH="$APP_PATH/$APP_NAME.pid"
22 PID_PATH="$APP_PATH/$APP_NAME.pid"
23 LOG_PATH="$APP_PATH/$APP_NAME.log"
23 LOG_PATH="$APP_PATH/$APP_NAME.log"
24
24
25 PYTHON_PATH="/home/$APP_HOMEDIR/v-env"
25 PYTHON_PATH="/home/$APP_HOMEDIR/v-env"
26
26
27 RUN_AS="marcink"
27 RUN_AS="marcink"
28
28
29 DAEMON="$PYTHON_PATH/bin/paster"
29 DAEMON="$PYTHON_PATH/bin/paster"
30
30
31 DAEMON_OPTS="serve --daemon \
31 DAEMON_OPTS="serve --daemon \
32 --user=$RUN_AS \
32 --user=$RUN_AS \
33 --group=$RUN_AS \
33 --group=$RUN_AS \
34 --pid-file=$PID_PATH \
34 --pid-file=$PID_PATH \
35 --log-file=$LOG_PATH $APP_PATH/$CONF_NAME"
35 --log-file=$LOG_PATH $APP_PATH/$CONF_NAME"
36
36
37
37
38 case "$1" in
38 case "$1" in
39 start)
39 start)
40 echo "Starting $APP_NAME"
40 echo "Starting $APP_NAME"
41 cd $APP_PATH
41 start-stop-daemon -d $APP_PATH -e PYTHON_EGG_CACHE="/tmp" \
42 start-stop-daemon --start --quiet\
42 --start --quiet \
43 --pidfile $PID_PATH \
43 --pidfile $PID_PATH \
44 --user $RUN_AS \
44 --user $RUN_AS \
45 --exec $DAEMON -- $DAEMON_OPTS
45 --exec $DAEMON -- $DAEMON_OPTS
46 ;;
46 ;;
47 stop)
47 stop)
48 echo "Stopping $APP_NAME"
48 echo "Stopping $APP_NAME"
49 start-stop-daemon --stop --quiet \
49 start-stop-daemon -d $APP_PATH \
50 --stop --quiet \
50 --pidfile $PID_PATH || echo "$APP_NAME - Not running!"
51 --pidfile $PID_PATH || echo "$APP_NAME - Not running!"
51 if [ -f $PID_PATH ]; then
52 if [ -f $PID_PATH ]; then
52 rm $PID_PATH
53 rm $PID_PATH
53 fi
54 fi
54 ;;
55 ;;
55 restart)
56 restart)
56 echo "Restarting $APP_NAME"
57 echo "Restarting $APP_NAME"
57 #stop
58 ### stop ###
58 start-stop-daemon --stop --quiet \
59 echo "Stopping $APP_NAME"
60 start-stop-daemon -d $APP_PATH \
61 --stop --quiet \
59 --pidfile $PID_PATH || echo "$APP_NAME - Not running!"
62 --pidfile $PID_PATH || echo "$APP_NAME - Not running!"
60 if [ -f $PID_PATH ]; then
63 if [ -f $PID_PATH ]; then
61 rm $PID_PATH
64 rm $PID_PATH
62 fi
65 fi
63 #start
66 ### start ###
64 start-stop-daemon --start --quiet\
67 echo "Starting $APP_NAME"
68 start-stop-daemon -d $APP_PATH -e PYTHON_EGG_CACHE="/tmp" \
69 --start --quiet \
65 --pidfile $PID_PATH \
70 --pidfile $PID_PATH \
66 --user $RUN_AS \
71 --user $RUN_AS \
67 --exec $DAEMON -- $DAEMON_OPTS
72 --exec $DAEMON -- $DAEMON_OPTS
68 ;;
73 ;;
69 *)
74 *)
70 echo "Usage: $0 {start|stop|restart}"
75 echo "Usage: $0 {start|stop|restart}"
71 exit 1
76 exit 1
72 esac No newline at end of file
77 esac
@@ -1,114 +1,107 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 # mercurial repository backup manager
3 # mercurial repository backup manager
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5
5
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
8 # as published by the Free Software Foundation; version 2
9 # of the License or (at your opinion) any later version of the license.
9 # of the License or (at your opinion) any later version of the license.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
19 # MA 02110-1301, USA.
20
20
21 """
21 """
22 Created on Feb 28, 2010
22 Created on Feb 28, 2010
23 Mercurial repositories backup manager
23 Mercurial repositories backup manager
24 @author: marcink
24 @author: marcink
25 """
25 """
26
26
27
27
28 import logging
28 import logging
29 from mercurial import config
30 import tarfile
29 import tarfile
31 import os
30 import os
32 import datetime
31 import datetime
33 import sys
32 import sys
34 import subprocess
33 import subprocess
35 logging.basicConfig(level=logging.DEBUG,
34 logging.basicConfig(level=logging.DEBUG,
36 format="%(asctime)s %(levelname)-5.5s %(message)s")
35 format="%(asctime)s %(levelname)-5.5s %(message)s")
37
36
38 class BackupManager(object):
37 class BackupManager(object):
39 def __init__(self, id_rsa_path, repo_conf):
38 def __init__(self, repos_location, rsa_key, backup_server):
40 self.repos_path = None
39 today = datetime.datetime.now().weekday() + 1
41 self.backup_file_name = None
40 self.backup_file_name = "mercurial_repos.%s.tar.gz" % today
42 self.id_rsa_path = id_rsa_path
41
43 self.check_id_rsa()
42 self.id_rsa_path = self.get_id_rsa(rsa_key)
44 cur_dir = os.path.realpath(__file__)
43 self.repos_path = self.get_repos_path(repos_location)
45 dn = os.path.dirname
44 self.backup_server = backup_server
46 self.backup_file_path = os.path.join(dn(dn(dn(cur_dir))), 'data')
45
47 cfg = config.config()
46 self.backup_file_path = '/tmp'
48 try:
47
49 cfg.read(os.path.join(dn(dn(dn(cur_dir))), repo_conf))
50 except IOError:
51 logging.error('Could not read %s', repo_conf)
52 sys.exit()
53 self.set_repos_path(cfg.items('paths'))
54 logging.info('starting backup for %s', self.repos_path)
48 logging.info('starting backup for %s', self.repos_path)
55 logging.info('backup target %s', self.backup_file_path)
49 logging.info('backup target %s', self.backup_file_path)
56
50
57 if not os.path.isdir(self.repos_path):
58 raise Exception('Not a valid directory in %s' % self.repos_path)
59
51
60 def check_id_rsa(self):
52 def get_id_rsa(self, rsa_key):
61 if not os.path.isfile(self.id_rsa_path):
53 if not os.path.isfile(rsa_key):
62 logging.error('Could not load id_rsa key file in %s',
54 logging.error('Could not load id_rsa key file in %s', rsa_key)
63 self.id_rsa_path)
64 sys.exit()
55 sys.exit()
65
56
66 def set_repos_path(self, paths):
57 def get_repos_path(self, path):
67 repos_path = paths[0][1].split('/')
58 if not os.path.isdir(path):
68 if repos_path[-1] in ['*', '**']:
59 logging.error('Wrong location for repositories in %s', path)
69 repos_path = repos_path[:-1]
60 sys.exit()
70 if repos_path[0] != '/':
61 return path
71 repos_path[0] = '/'
72 self.repos_path = os.path.join(*repos_path)
73
62
74 def backup_repos(self):
63 def backup_repos(self):
75 today = datetime.datetime.now().weekday() + 1
76 self.backup_file_name = "mercurial_repos.%s.tar.gz" % today
77 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name)
64 bckp_file = os.path.join(self.backup_file_path, self.backup_file_name)
78 tar = tarfile.open(bckp_file, "w:gz")
65 tar = tarfile.open(bckp_file, "w:gz")
79
66
80 for dir_name in os.listdir(self.repos_path):
67 for dir_name in os.listdir(self.repos_path):
81 logging.info('backing up %s', dir_name)
68 logging.info('backing up %s', dir_name)
82 tar.add(os.path.join(self.repos_path, dir_name), dir_name)
69 tar.add(os.path.join(self.repos_path, dir_name), dir_name)
83 tar.close()
70 tar.close()
84 logging.info('finished backup of mercurial repositories')
71 logging.info('finished backup of mercurial repositories')
85
72
86
73
87
74
88 def transfer_files(self):
75 def transfer_files(self):
89 params = {
76 params = {
90 'id_rsa_key': self.id_rsa_path,
77 'id_rsa_key': self.id_rsa_path,
91 'backup_file_path':self.backup_file_path,
78 'backup_file':os.path.join(self.backup_file_path,
92 'backup_file_name':self.backup_file_name,
79 self.backup_file_name),
80 'backup_server':self.backup_server
93 }
81 }
94 cmd = ['scp', '-l', '40000', '-i', '%(id_rsa_key)s' % params,
82 cmd = ['scp', '-l', '40000', '-i', '%(id_rsa_key)s' % params,
95 '%(backup_file_path)s/%(backup_file_name)s' % params,
83 '%(backup_file)s' % params,
96 'root@192.168.2.102:/backups/mercurial' % params]
84 '%(backup_server)s' % params]
97
85
98 subprocess.call(cmd)
86 subprocess.call(cmd)
99 logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4])
87 logging.info('Transfered file %s to %s', self.backup_file_name, cmd[4])
100
88
101
89
102 def rm_file(self):
90 def rm_file(self):
103 logging.info('Removing file %s', self.backup_file_name)
91 logging.info('Removing file %s', self.backup_file_name)
104 os.remove(os.path.join(self.backup_file_path, self.backup_file_name))
92 os.remove(os.path.join(self.backup_file_path, self.backup_file_name))
105
93
106
94
107
95
108 if __name__ == "__main__":
96 if __name__ == "__main__":
109 B_MANAGER = BackupManager('/home/pylons/id_rsa', 'repositories.config')
97
98 repo_location = '/home/repo_path'
99 backup_server = 'root@192.168.1.100:/backups/mercurial'
100 rsa_key = '/home/id_rsa'
101
102 B_MANAGER = BackupManager(repo_location, rsa_key, backup_server)
110 B_MANAGER.backup_repos()
103 B_MANAGER.backup_repos()
111 B_MANAGER.transfer_files()
104 B_MANAGER.transfer_files()
112 B_MANAGER.rm_file()
105 B_MANAGER.rm_file()
113
106
114
107
General Comments 0
You need to be logged in to leave comments. Login now