File: bacula-ctl-sd.in

package info (click to toggle)
bacula 15.0.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,780 kB
  • sloc: ansic: 194,276; cpp: 41,177; sh: 28,258; python: 6,669; makefile: 5,275; perl: 3,666; sql: 1,371; java: 345; xml: 196; awk: 51; sed: 25
file content (260 lines) | stat: -rw-r--r-- 5,846 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#! /bin/sh
#
#   Bacula(R) - The Network Backup Solution
#
#   Copyright (C) 2000-2023 Kern Sibbald
#
#   The original author of Bacula is Kern Sibbald, with contributions
#   from many others, a complete list can be found in the file AUTHORS.
#
#   You may use this file and others of this release according to the
#   license defined in the LICENSE file, which includes the Affero General
#   Public License, v3.0 ("AGPLv3") and some additional permissions and
#   terms pursuant to its AGPLv3 Section 7.
#
#   This notice must be preserved when any source code is 
#   conveyed and/or propagated.
#
#   Bacula(R) is a registered trademark of Kern Sibbald.
#
# bacula-ctl-sd This shell script takes care of starting and stopping
#		the bacula Storage daemon
#
#   This is pretty much watered down version of the RedHat script
#   that works on Solaris as well as Linux, but it won't work everywhere.
#
# description: The Leading Open Source Backup Solution.
#

PSCMD="@PSCMD@"
PS="ps"

#
# On Solaris, you may need to use nawk, or alternatively,
#  add the GNU binaries to your path, such as /usr/xpg4/bin
#
AWK=@AWK@

# All these are not *really* needed but it makes it
#  easier to "steal" this code for the development 
#  environment where they are different.
#  
BACSDBIN=@sbindir@
BACSDCFG=@sysconfdir@
PIDDIR=@piddir@
SUBSYSDIR=@subsysdir@

SD_PORT=@sd_port@

SD_USER=@sd_user@
SD_GROUP=@sd_group@
Bacula="@BACULA@"
PIDOF=@PIDOF@	

# A function to stop a program.
killproc() {
   RC=0
   # Test syntax.
   if [ $# = 0 ]; then
      echo "Usage: killproc {program} {port} [signal]"
      return 1
   fi

   notset=0
   # check for third arg to be kill level
   if [ "$3" != "" ] ; then
      killlevel=$3
   else
      notset=1
      killlevel="-9"
   fi

   # Get base program name
   base=`basename $1`

   # Find pid.
   pid=`pidofproc $base $2`

   # Kill it.
   if [ "$pid" != "" ] ; then
      if [ "$notset" = "1" ] ; then
	 if ${PS} -p "$pid">/dev/null 2>&1; then
	     # TERM first, then KILL if not dead
	     kill -TERM $pid 2>/dev/null
	     sleep 1
	     if ${PS} -p "$pid" >/dev/null 2>&1 ; then
		 sleep 1
		 if ${PS} -p "$pid" >/dev/null 2>&1 ; then
		     sleep 3
		     if ${PS} -p "$pid" >/dev/null 2>&1 ; then
			 kill -KILL $pid 2>/dev/null
		     fi
		 fi
	     fi
	  fi
	  ${PS} -p "$pid" >/dev/null 2>&1
	  RC=$?
	  [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
      #    RC=$((! $RC))
      # use specified level only
      else
	 if ${PS} -p "$pid" >/dev/null 2>&1; then
	    kill $killlevel $pid 2>/dev/null
	    RC=$?
	    [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
	 fi
      fi
   else
      failure "$base shutdown"
   fi
   # Remove pid file if any.
   if [ "$notset" = "1" ]; then
      rm -f ${PIDDIR}/$base.$2.pid
   fi
   return $RC
}

# A function to find the pid of a program.
pidofproc() {
   pid=""
   # Test syntax.
   if [ $# = 0 ] ; then
      echo "Usage: pidofproc {program}"
      return 1
   fi

   # Get base program name
   base=`basename $1`

   # First try PID file
   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
      if [ "$pid" != "" ] ; then
	 echo $pid
	 return 0
      fi
   fi

   # Next try "pidof"
   if [ -x ${PIDOF} ] ; then
      pid=`${PIDOF} $1`
   fi
   if [ "$pid" != "" ] ; then
      echo $pid
      return 0
   fi

   # Finally try to extract it from ps
   pid=`${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`
   echo $pid
   return 0
}

status() {
   pid=""
   # Test syntax.
   if [ $# = 0 ] ; then
       echo "Usage: status {program} {port}"
       return 1
   fi

   # Get base program name
   base=`basename $1`

   # First try "pidof"
   if [ -x ${PIDOF} ] ; then
      pid=`${PIDOF} $1`
   fi
   if [ "$pid" != "" ] ; then
      echo "$base (pid $pid) is running..."
      return 0
   else
      pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 } 
	    { if ((prog == $2) || (("(" prog ")") == $2) ||
		 (("[" prog "]") == $2) ||
		 ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
      if [ "$pid" != "" ] ; then
	 echo "$base (pid $pid) is running..."
	 return 0
      fi
   fi

   # Next try the PID files
   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
      if [ "$pid" != "" ] ; then
	 echo "$base dead but pid file exists"
	 return 1
      fi
   fi
   # See if the subsys lock exists
   if [ -f ${SUBSYSDIR}/$base ] ; then
      echo "$base dead but subsys locked"
      return 2
   fi
   echo "$base is stopped"
   return 3
}

success() {
   return 0
}

failure() {
   rc=$?
   return $rc
}

OS=`uname -s`

# if /lib/tls exists, force Bacula to use the glibc pthreads instead
if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
   export LD_ASSUME_KERNEL=2.4.19
fi

case "$1" in
   start)
      [ -x ${BACSDBIN}/bacula-sd ] && {
	 echo "Starting the $Bacula Storage daemon"
	 OPTIONS=''
	 if [ "${SD_USER}" != '' ]; then
	    OPTIONS="${OPTIONS} -u ${SD_USER}"
	 fi

	 if [ "${SD_GROUP}" != '' ]; then
	    OPTIONS="${OPTIONS} -g ${SD_GROUP}"
	 fi

	 ulimit -l unlimited > /dev/null 2> /dev/null || true
	 if [ "x${VALGRIND_SD}" = "x1" ]; then
	    valgrind --leak-check=full ${BACSDBIN}/bacula-sd $2 $3 ${OPTIONS} -v -c ${BACSDCFG}/bacula-sd.conf
	 else
	    ${BACSDBIN}/bacula-sd $2 $3 ${OPTIONS} -v -c ${BACSDCFG}/bacula-sd.conf
	 fi
      }
      ;;


   stop)
      [ -x ${BACSDBIN}/bacula-sd ] && {
	 echo "Stopping the $Bacula Storage daemon"
	 killproc ${BACSDBIN}/bacula-sd ${SD_PORT} $2
      }
      ;;

   restart)
      $0 stop
      sleep 5
      $0 start
      ;;

   status)
      [ -x ${BACSDBIN}/bacula-sd   ] && status ${BACSDBIN}/bacula-sd  ${SD_PORT}
      ;;

   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac
exit 0