PageRenderTime 111ms CodeModel.GetById 101ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/init.d/rsync

http://github.com/brinkman83/bashrc
Shell | 153 lines | 123 code | 15 blank | 15 comment | 21 complexity | 0f5750c26613a7b3064f356e4e112326 MD5 | raw file
  1#! /bin/sh
  2
  3### BEGIN INIT INFO
  4# Provides:          rsyncd
  5# Required-Start:    $remote_fs $syslog
  6# Required-Stop:     $remote_fs $syslog
  7# Should-Start:      $named
  8# Default-Start:     2 3 4 5
  9# Default-Stop:      1
 10# Short-Description: fast remote file copy program daemon
 11# Description:       rsync is a program that allows files to be copied to and
 12#                    from remote machines in much the same way as rcp.
 13#                    This provides rsyncd daemon functionality.
 14### END INIT INFO
 15
 16set -e
 17
 18# /etc/init.d/rsync: start and stop the rsync daemon
 19
 20DAEMON=/usr/bin/rsync
 21RSYNC_ENABLE=false
 22RSYNC_OPTS=''
 23RSYNC_DEFAULTS_FILE=/etc/default/rsync
 24RSYNC_CONFIG_FILE=/etc/rsyncd.conf
 25RSYNC_PID_FILE=/var/run/rsync.pid
 26RSYNC_NICE_PARM=''
 27RSYNC_IONICE_PARM=''
 28
 29test -x $DAEMON || exit 0
 30
 31. /lib/lsb/init-functions
 32
 33if [ -s $RSYNC_DEFAULTS_FILE ]; then
 34    . $RSYNC_DEFAULTS_FILE
 35    case "x$RSYNC_ENABLE" in
 36	xtrue|xfalse)	;;
 37	xinetd)		exit 0
 38			;;
 39	*)		log_failure_msg "Value of RSYNC_ENABLE in $RSYNC_DEFAULTS_FILE must be either 'true' or 'false';"
 40			log_failure_msg "not starting rsync daemon."
 41			exit 1
 42			;;
 43    esac
 44    case "x$RSYNC_NICE" in
 45	x[0-9])		RSYNC_NICE_PARM="--nicelevel $RSYNC_NICE";;
 46	x[1-9][0-9])	RSYNC_NICE_PARM="--nicelevel $RSYNC_NICE";;
 47	x)		;;
 48	*)		log_warning_msg "Value of RSYNC_NICE in $RSYNC_DEFAULTS_FILE must be a value between 0 and 19 (inclusive);"
 49			log_warning_msg "ignoring RSYNC_NICE now."
 50			;;
 51    esac
 52    case "x$RSYNC_IONICE" in
 53	x-c[123]*)	RSYNC_IONICE_PARM="$RSYNC_IONICE";;
 54	x)		;;
 55	*)		log_warning_msg "Value of RSYNC_IONICE in $RSYNC_DEFAULTS_FILE must be -c1, -c2 or -c3;"
 56			log_warning_msg "ignoring RSYNC_IONICE now."
 57			;;
 58    esac
 59fi
 60
 61export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
 62
 63rsync_start() {
 64    if [ ! -s "$RSYNC_CONFIG_FILE" ]; then
 65        log_failure_msg "missing or empty config file $RSYNC_CONFIG_FILE"
 66        log_end_msg 1
 67        exit 1
 68    fi
 69    # See ionice(1)
 70    if [ -n "$RSYNC_IONICE_PARM" ] && [ -x /usr/bin/ionice ] &&
 71        /usr/bin/ionice "$RSYNC_IONICE_PARM" true 2>/dev/null; then
 72        /usr/bin/ionice "$RSYNC_IONICE_PARM" -p$$ > /dev/null 2>&1
 73    fi
 74    if start-stop-daemon --start --quiet --background \
 75        --pidfile $RSYNC_PID_FILE --make-pidfile \
 76        $RSYNC_NICE_PARM --exec $DAEMON \
 77        -- --no-detach --daemon --config "$RSYNC_CONFIG_FILE" $RSYNC_OPTS
 78    then
 79        rc=0
 80        sleep 1
 81        if ! kill -0 $(cat $RSYNC_PID_FILE) >/dev/null 2>&1; then
 82            log_failure_msg "rsync daemon failed to start"
 83            rc=1
 84        fi
 85    else
 86        rc=1
 87    fi
 88    if [ $rc -eq 0 ]; then
 89        log_end_msg 0
 90    else
 91        log_end_msg 1
 92        rm -f $RSYNC_PID_FILE
 93    fi
 94} # rsync_start
 95
 96
 97case "$1" in
 98  start)
 99	if "$RSYNC_ENABLE"; then
100	    log_daemon_msg "Starting rsync daemon" "rsync"
101	    if [ -s $RSYNC_PID_FILE ] && kill -0 $(cat $RSYNC_PID_FILE) >/dev/null 2>&1; then
102		log_progress_msg "apparently already running"
103		log_end_msg 0
104		exit 0
105	    fi
106            rsync_start
107        else
108            if [ -s "$RSYNC_CONFIG_FILE" ]; then
109                [ "$VERBOSE" != no ] && log_warning_msg "rsync daemon not enabled in $RSYNC_DEFAULTS_FILE, not starting..."
110            fi
111	fi
112	;;
113  stop)
114	log_daemon_msg "Stopping rsync daemon" "rsync"
115	start-stop-daemon --stop --quiet --oknodo --pidfile $RSYNC_PID_FILE
116	log_end_msg $?
117	rm -f $RSYNC_PID_FILE
118	;;
119
120  reload|force-reload)
121	log_warning_msg "Reloading rsync daemon: not needed, as the daemon"
122	log_warning_msg "re-reads the config file whenever a client connects."
123	;;
124
125  restart)
126	set +e
127	if $RSYNC_ENABLE; then
128	    log_daemon_msg "Restarting rsync daemon" "rsync"
129	    if [ -s $RSYNC_PID_FILE ] && kill -0 $(cat $RSYNC_PID_FILE) >/dev/null 2>&1; then
130		start-stop-daemon --stop --quiet --oknodo --pidfile $RSYNC_PID_FILE || true
131		sleep 1
132	    else
133		log_warning_msg "rsync daemon not running, attempting to start."
134	    	rm -f $RSYNC_PID_FILE
135	    fi
136            rsync_start
137        else
138            if [ -s "$RSYNC_CONFIG_FILE" ]; then
139                [ "$VERBOSE" != no ] && log_warning_msg "rsync daemon not enabled in $RSYNC_DEFAULTS_FILE, not starting..."
140            fi
141	fi
142	;;
143
144  status)
145	status_of_proc -p $RSYNC_PID_FILE "$DAEMON" rsync
146	exit $?	# notreached due to set -e
147	;;
148  *)
149	echo "Usage: /etc/init.d/rsync {start|stop|reload|force-reload|restart|status}"
150	exit 1
151esac
152
153exit 0