Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - bflsync


#!/usr/bin/sh 
# $Id: bflsync,v 1.50 2017/03/05 12:09:40 fulford Exp $
# For support  call: Clifford W Fulford on 0793 572 8612 
# 	      email: fulford@citylinux.com
#         log issue: https://www.citylinux.com/belpark
#############################################################
cmd=$0
awk=/usr/bin/awk
ver=`echo "$Id: bflsync,v 1.50 2017/03/05 12:09:40 fulford Exp $"|awk '{print $3,$4,$5}'`
config=/usr/local/etc/bflsync.cf
email=fulford@citylinux.com
syntax="$cmd [[-c] [-d] [-e <recipient address>] [-q]]|[-V]|[-s]"
tmp=/tmp/`basename $cmd`-$$ 
>$tmp
trap "end=true" 15
trap "reconfig=true" 1
verbose=: # verbose output by default
while [ $# -gt 0 ] ;do
        case $1 in
		-c) config=$2;shift 2;;
                -d) set -x;debug=0;shift;;
		-e) email=$1;shift 2;;
		-q) unset verbose ; shift;; #reduce output
                -V) echo "$cmd $Revision: 1.50 $ $Date: 2017/03/05 12:09:40 $"|awk '{print $1,$3,$6}';exit;;
		-s) showconfig=:;shift;;
		 *) echo $syntax>&2 ;exit 1;;
        esac
done

lock=${lock:="/var/run/bflsync"}
if [ -f $lock ];then
	echo "$cmd: lock file $lock already exists" >&2
	pid=`cat $lock`
	ps -ef|awk -v pid=$pid '$2 == pid'
	exit 1
else
	echo $$ >$lock
	[ $verbose ] && echo "lock $lock set" >&2
fi

_getconfig () {
	[ -r $config ] &&{
		cat $config |sed -ne '/^[^#]/p' >$tmp	
		. $tmp
	}
	logdir=${logdir:="/var/log"}
	opst=${opst:-21} # Off peak period start time
	opet=${opet:-9}  # Off peak period end time
	opwt=${opwt:-45} # Off peak period wait time in minutes
	pwt=${pwt:-5}   # Peak period wait time in minutes
	synclst=${synclst:="reports,data,datab,datac,applic,bacbin"}
	synclst=`echo $synclst|sed -ne 's/,/ /gp'`
	[ $showconfig ] || rm $tmp
}

_chklogfile () {
	log=$logdir/bflsync-`date "+%w"`
	date=`date "+%b %e"`
	ls -l $log |grep -q "$date" ||{
		>$log
		[ $verbose ] && echo "new log $log opened" >&2
	}
}
_showconfig () {
	_chklogfile
	echo "config file = $config"
	echo "log file = $log"
	echo "log file status = `ls -l $log`"
	echo "tmp file = $tmp"
	echo "Off peak start time = $opst"
	echo "Off peak end time = $opet"
	echo "Off peak wait time = $opwt"
	echo "Peak wait time = $pwt"
	echo "Sync list = $synclst"
	cat $tmp && rm $tmp
}

_run () {
   echo "\nSTART `date`" >>$log
   for d in $synclst;do
      case $d in
	data|datab|datac|datad)
	      mount|grep "belpark/$d"||{
		if mount -o ro belpark:/$d /belfield/$d 2>/dev/null;then
			[ $verbose ] && {
				echo "/belpark/$d mounted" >&2
			}
		else
			echo "$cmd: Can't mount /belpark/$d" >&2
			exit 1
		fi
	      }
	      echo "rsync -az --exclude=lost+found /belpark/$d/ /bfldata/$d\c" >>$log
	      [ $verbose ] && {
	      	echo "rsync -az --exclude=lost+found /belpark/$d/ /bfldata/$d\c" >&2 
	      }
	      rsync -az --exclude=lost+found /belpark/$d/ /bfldata/$d
	      [ $? -eq 0 ] && result=completed || result=failed
      	      time=`date +%H:%M`
      	      echo " $result $time" >>$log
      	      [ $verbose ] && echo "$cmd: $result $time" >&2
	      if umount /belpark/$d 2>/dev/null ;then
		[ $verbose ] && echo "/belpark/$d unmounted" >&2
	      else 
	        [ $verbose ] && echo "umount of $d failed" >&2
	      fi 
	;;
	applic) 
	   if mount -o ro belpark:/$d /belfield/$d 2>/dev/null; then
	      [ $verbose ] &&{
		echo "/belpark/$d mounted" >&2
	      }
	    else
		echo "$cmd: mount of $d failed, terminating `$date`">&2
		exit 1
	    fi
	    echo "rsync -az --exclude=lost+found /belpark/$d/ /$d\c" >&2
	    echo "rsync -az --exclude=lost+found /belpark/$d/ /$d\c" >>$log
	    rsync -az --exclude=lost+found /belpark/$d/ /$d
	    [ $? -eq 0 ] && result=completed || result=failed
      	    time=`date +%H:%M`
      	    echo " $result $time" >>$log
      	    [ $verbose ] &&  echo "$result $time" >&2
	    if umount /belpark/$d 2>/dev/null ;then
		[ $verbose ] && echo "/belpark/$d unmounted"
	     else
		[ $verbose ] && echo "umount of  /belpark/$d failed">&2
	     fi
	;;
	bacbin) 
	   if mount -o ro belpark:/usr/$d /belfield/$d 2>/dev/null; then
	      [ $verbose ] && echo "/belpark/$d mounted" >&2
	   else
	      echo "$cmd: mount of /belpark/$d failed - terminated `date`" >&2
	      exit 1
	   fi
	   echo "rsync -az --exclude=lost+found /belpark/$d/ /usr/$d\c" >>$log
	   [ $verbose ] && {
	      echo "rsync -az --exclude=lost+found /belpark/$d/ /usr/$d\c">&2
	   }
	   rsync -az --exclude=lost+found /belpark/$d/ /usr/$d
	   [ $? -eq 0 ] && result=completed || result=failed
      	   time=`date +%H:%M`
      	   echo " $result $time" >>$log
      	   [ $verbose ] && echo " $result $time" >&2
	   if umount /belpark/$d 2>/dev/null;then
	      [ $verbose ] && echo "/belpark/$d unmounted" >&2
	   else
	      [ $verbose ] && echo "umount of /belpark/$d failed" >&2
   	   fi
	;;
	reports)
	   if mount -o ro belpark:/usr/$d /belfield/$d 2>/dev/null; then
	      [ $verbose ] && echo "/belpark/$d mounted" >&2
	   else
		echo "$cmd: mount of $d failed - terminating `date`" >&2
		exit 1
	   fi
	   echo "rsync -az /belpark/$d /usr/$d\c" >>$log
	   [ $verbose ] && {
	   	echo "rsync -az /belpark/$d /usr/$d\c" >&2
	   }
	   rsync -az /belpark/$d /usr/$d
	   [ $? -eq 0 ] && result=completed || result=failed
      	   time=`date +%H:%M`
      	   echo " $result $time" >>$log
	   [ $verbose ] && echo " $result $time" >&2
	   if umount /belpark/$d 2>/dev/null ;then
	      	[ $verbose ] && echo "/belpark/$d unmounted" >&2
	   else
		echo "$cmd: umount of /belpark/$d failed" >&2
		exit 1
	   fi
	;;
      esac
      [ $end ] &&{
	echo "$cmd: received signal 15 - terminating"
	echo "TERMINATED `date`" >>$log
	rm $lock
	exit 2
      }
      [ $reconfig ] && {
	_getconfig
	unset reconfig
	echo "$cmd: reconfigured on SIGHUP" >&2
	echo "\nReconfigured on SIGHUP `date`" >>$log
      }
   done
   echo "FINISH `date`" >>$log
   [ $verbose ] && echo "FINISH `date`" >&2
}

_getconfig
[ $debug ] && _showconfig
[ $showconfig ] &&{
	_showconfig
	rm $lock
	exit 0
}
while :;do
      _chklogfile
      _run 
      if grep -q failed $log ;then
   		[ -n "$email" ] &&{
			grep failed $log|
			mailx -s "URGENT - bflsync failure `date`" $email
		}
		exit
      else
		hour=`date +%H`
		if [ $hour -ge $opet -a $hour -le $opst ];then
			waittime=$pwt
		else
			waittime=$opwt
		fi
		[ $verbose ] &&{
			echo "hour = $hour">&2
			echo "off peak time = $opst - $opet">&2
			echo "wait = $waittime">&2
			echo "$waittime minute count starting `date`" >&2
		}
		count=0
		while [ $count -lt  $waittime ];do
      			[ $end ] &&{
				echo "$cmd received signal 15\c"
				echo " - terminating"
				echo "\nTERMINATED `date`" >>$log
				rm $lock
				exit 2
      			}
			sleep 60
			count=`expr $count + 1`
			if [ $count -lt $waittime ] ;then
				echo "*\c" >&2
			else
				echo "*" >&2
			fi
		done
		[ $verbose ] && echo "count = $count `date`" >&2
      fi
done
######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 of  #
# the License, or (at your option) any later version.                #
#                                                                    #
# This program is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #    
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.