1#! /bin/sh
2#
3# Copyright (C) 2000-2020 Kern Sibbald
4# License: BSD 2-Clause; see file LICENSE-FOSS
5#
6# bacula       This shell script takes care of starting and stopping
7#	       the bacula daemons.
8#
9#   It runs with different ports than the production version,
10#   and using the current development enviornment.
11#
12#   This is pretty much watered down version of the RedHat script
13#   that works on Solaris as well as Linux, but it won't work everywhere.
14#
15# description: The Leading Open Source Backup Solution.
16#
17
18PSCMD="@PSCMD@"
19pwd=`pwd`
20
21BACDIRBIN=${pwd}/src/dird
22BACDIRCFG=${pwd}/src/dird
23BACFDBIN=${pwd}/src/filed
24BACFDCFG=${pwd}/src/filed
25BACSDBIN=${pwd}/src/stored
26BACSDCFG=${pwd}/src/stored
27PIDDIR=@piddir@
28SUBSYSDIR=@subsysdir@
29
30# Use non-production ports
31DIR_PORT=8101
32FD_PORT=8102
33SD_PORT=8103
34
35DIR_USER=@dir_user@
36DIR_GROUP=@dir_group@
37FD_USER=@fd_user@
38FD_GROUP=@fd_group@
39SD_USER=@sd_user@
40SD_GROUP=@sd_group@
41
42# A function to stop a program.
43killproc() {
44    RC=0
45    # Test syntax.
46    if [ $# = 0 ]; then
47	echo "Usage: killproc {program} [signal]"
48	return 1
49    fi
50
51    notset=0
52    # check for third arg to be kill level
53    if [ "$3" != "" ] ; then
54	killlevel=$3
55    else
56	notset=1
57	killlevel="-9"
58    fi
59
60    # Get base program name
61    base=`basename $1`
62
63    # Find pid.
64    pid=`pidofproc $base $2`
65
66    # Kill it.
67    if [ "$pid" != "" ] ; then
68	if [ "$notset" = "1" ] ; then
69	    if ps -p $pid>/dev/null 2>&1; then
70		# TERM first, then KILL if not dead
71		kill -TERM $pid 2>/dev/null
72		sleep 1
73		if ps -p $pid >/dev/null 2>&1 ; then
74		    sleep 1
75		    if ps -p $pid >/dev/null 2>&1 ; then
76			sleep 3
77			if ps -p $pid >/dev/null 2>&1 ; then
78			    kill -KILL $pid 2>/dev/null
79			fi
80		    fi
81		fi
82	     fi
83	     ps -p $pid >/dev/null 2>&1
84	     RC=$?
85	     [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
86	#    RC=$((! $RC))
87	# use specified level only
88	else
89	    if ps -p $pid >/dev/null 2>&1; then
90		kill $killlevel $pid 2>/dev/null
91		RC=$?
92		[ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
93	    fi
94	fi
95    else
96	failure "$base shutdown"
97    fi
98    # Remove pid file if any.
99    if [ "$notset" = "1" ]; then
100	rm -f ${PIDDIR}/$base.$2.pid
101    fi
102    return $RC
103}
104
105# A function to find the pid of a program.
106pidofproc() {
107    pid=""
108    # Test syntax.
109    if [ $# = 0 ] ; then
110	echo "Usage: pidofproc {program}"
111	return 1
112    fi
113
114    # Get base program name
115    base=`basename $1`
116
117    # First try PID file
118    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
119	pid=`head -1 ${PIDDIR}/$base.$2.pid`
120	if [ "$pid" != "" ] ; then
121	    echo $pid
122	    return 0
123	fi
124    fi
125
126    # Next try "pidof"
127   if [ -x /sbin/pidof ] ; then
128       pid=`/sbin/pidof $1`
129   fi
130   if [ "$pid" != "" ] ; then
131       echo $pid
132       return 0
133   fi
134
135    # Finally try to extract it from ps
136    ${PSCMD} | grep $1 | awk '{ print $1 }' | tr '\n' ' '
137    return 0
138}
139
140status() {
141    # Test syntax.
142    if [ $# = 0 ] ; then
143	echo "Usage: status {program}"
144	return 1
145    fi
146
147    # Get base program name
148    base=`basename $1`
149
150   # First try "pidof"
151   if [ -x /sbin/pidof ] ; then
152       pid=`/sbin/pidof $1`
153   fi
154   if [ "$pid" != "" ] ; then
155       echo "$base (pid $pid) is running..."
156       return 0
157   else
158       pid=`${PSCMD} | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
159	     { if ((prog == $2) || (("(" prog ")") == $2) ||
160		  (("[" prog "]") == $2) ||
161		  ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
162       if [ "$pid" != "" ] ; then
163	   echo "$base (pid $pid) is running..."
164	   return 0
165       fi
166   fi
167
168    # Next try the PID files
169    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
170	pid=`head -1 ${PIDDIR}/$base.$2.pid`
171	if [ "$pid" != "" ] ; then
172	    echo "$base dead but pid file exists"
173	    return 1
174	fi
175    fi
176    # See if the subsys lock exists
177    if [ -f ${SUBSYSDIR}/$base ] ; then
178	echo "$base dead but subsys locked"
179	return 2
180    fi
181    echo "$base is stopped"
182    return 3
183}
184
185success() {
186  return 0
187}
188
189failure() {
190  rc=$?
191  return $rc
192}
193
194case "$1" in
195    start)
196       [ -x ${BACSDBIN}/bacula-sd ] && {
197	  echo "Starting the Storage daemon"
198	  OPTIONS=''
199	  if [ "${SD_USER}" != '' ]; then
200	     OPTIONS="${OPTIONS} -u ${SD_USER}"
201	  fi
202
203	  if [ "${SD_GROUP}" != '' ]; then
204	     OPTIONS="${OPTIONS} -g ${SD_GROUP}"
205	  fi
206
207	  ${BACSDBIN}/bacula-sd $2 ${OPTIONS} -v -c ${BACSDCFG}/stored.conf
208       }
209
210       [ -x ${BACFDBIN}/bacula-fd ] && {
211	  echo "Starting the File daemon"
212	  OPTIONS=''
213	  if [ "${FD_USER}" != '' ]; then
214	     OPTIONS="${OPTIONS} -u ${FD_USER}"
215	  fi
216
217	  if [ "${FD_GROUP}" != '' ]; then
218	     OPTIONS="${OPTIONS} -g ${FD_GROUP}"
219	  fi
220
221	  ${BACFDBIN}/bacula-fd $2 ${OPTIONS} -v -c ${BACFDCFG}/filed.conf
222       }
223
224       [ -x ${BACDIRBIN}/bacula-dir ] && {
225	   sleep 2
226	   echo "Starting the Director daemon"
227	  OPTIONS=''
228	  if [ "${DIR_USER}" != '' ]; then
229	     OPTIONS="${OPTIONS} -u ${DIR_USER}"
230	  fi
231
232	  if [ "${DIR_GROUP}" != '' ]; then
233	     OPTIONS="${OPTIONS} -g ${DIR_GROUP}"
234	  fi
235
236	  if [ "${VALGRIND}" != '' ]; then
237	     valgrind --leak-check=full ${BACDIRBIN}/bacula-dir $2 ${OPTIONS} -v -c ${BACDIRCFG}/dird.conf
238	  else
239	     ${BACDIRBIN}/bacula-dir $2 ${OPTIONS} -v -c ${BACDIRCFG}/dird.conf
240	  fi
241       }
242       ;;
243
244    stop)
245       # Stop the FD first so that SD will fail jobs and update catalog
246       [ -x ${BACFDBIN}/bacula-fd ] && {
247	  echo "Stopping the File daemon"
248	  killproc ${BACFDBIN}/bacula-fd ${FD_PORT}
249       }
250
251       [ -x ${BACSDBIN}/bacula-sd ] && {
252	  echo "Stopping the Storage daemon"
253	  killproc ${BACSDBIN}/bacula-sd ${SD_PORT}
254       }
255
256       [ -x ${BACDIRBIN}/bacula-dir ] && {
257	  echo "Stopping the Director daemon"
258	  killproc ${BACDIRBIN}/bacula-dir ${DIR_PORT}
259       }
260       echo
261       ;;
262
263    restart)
264       $0 stop
265       sleep 5
266       $0 start
267       ;;
268
269    status)
270       [ -x ${BACSDBIN}/bacula-sd   ] && status ${BACSDBIN}/bacula-sd  ${SD_PORT}
271       [ -x ${BACFDBIN}/bacula-fd   ] && status ${BACFDBIN}/bacula-fd  ${FD_PORT}
272       [ -x ${BACDIRBIN}/bacula-dir ] && status ${BACDIRBIN}/bacula-dir ${DIR_PORT}
273       ;;
274
275    *)
276       echo "Usage: $0 {start|stop|restart|status}"
277       exit 1
278       ;;
279esac
280exit 0
281