Inotifywait Script

Inotifywait Script mit csync2 Synchronisation.

root@localhost:~# vi /etc/systemd/system/inotifywait\@.service
[Unit]
Description=Inotifywait Daemon
 
[Service]
Type=oneshot
ExecStart=/opt/inotifywait/bin/inotifywait.sh %i.cfg start
ExecStop=/opt/inotifywait/bin/inotifywait.sh %i.cfg stop
RemainAfterExit=yes
 
[Install]
WantedBy=multi-user.target
root@localhost:~# mkdir -p /opt/inotifywait/bin/
root@localhost:~# mkdir -p /opt/inotifywait/var/run
root@localhost:~# mkdir /opt/inotifywait/etc/
root@localhost:~# vi /opt/inotifywait/bin/inotifywait.sh
#!/bin/bash
 
# init script
SCRIPTNAME=$0
SCRIPTPATH=/opt/inotifywait
CONFIG=$1
ACTION=$2
LOGTAG="inotifywait"
LOGFACILITY="local5.notice"
 
# check arguments
if [ $# -ne 2 ]; then
	echo "Illegal number of arguments, usage $0 {CONFIGFILE} {start|stop|status}"
	exit 1
fi
 
if ! [[ $2 =~ ^(start|stop|status)$ ]]; then
	echo "Illegal value $2 for action argument, usage $0 {CONFIGFILE} {start|stop|status}"
	exit 1
fi
 
# functions
function stopProcess {
	if [ -f $SCRIPTPATH/var/run/$CONFIG.pid ]; then
		MYPID=$(cat $SCRIPTPATH/var/run/$CONFIG.pid)
		MYPPID=$(pgrep -o -P $MYPID)
		kill $MYPPID
		if [ $? -eq 0 ]; then
			logger -p $LOGFACILITY -t $LOGTAG "process $MYPPID for config $CONFIG successfully terminated"
			rm -f $SCRIPTPATH/var/run/$CONFIG.pid
			exit 0
		else
			logger -p $LOGFACILITY -t $LOGTAG "failed to terminate process $MYPPID for config $CONFIG"
			exit 1
		fi
	fi
 
	logger -p $LOGFACILITY -t $LOGTAG "no process found for config $CONFIG"
	exit 1
}
 
function statusProcess {
	if [ -f $SCRIPTPATH/var/run/$CONFIG.pid ]; then
		MYPID=$(cat $SCRIPTPATH/var/run/$CONFIG.pid)
		MYPPID=$(pgrep -o -P $MYPID)
		if [ $? -eq 0 ]; then
			echo "process $MYPPID for config $CONFIG is running"
			exit 0
		else
			echo "failed to terminate process $MYPPID for config $CONFIG"
			exit 1
		fi
	fi
 
	echo "no process found for config $CONFIG"
	exit 1
}
 
function checkStart {
	if [ -f $SCRIPTPATH/var/run/$CONFIG.pid ]; then
		MYPID=$(cat $SCRIPTPATH/var/run/$CONFIG.pid)
		MYPPID=$(pgrep -o -P $MYPID)
		logger -p $LOGFACILITY -t $LOGTAG "process for conifg $CONFIG is already running with $MYPID"
		exit 1
	fi
}
 
function checkRequirement {
	test -x $1 && return $?
	logger -p $LOGFACILITY -t $LOGTAG "$1 not found, exit $SCRIPTNAME"
	exit 1
}
 
function readConfig {
	if [ -f $SCRIPTPATH/etc/$1 ]; then
		logger -p $LOGFACILITY -t $LOGTAG "load config $CONFIG file: $SCRIPTPATH/etc/$1"
		. $SCRIPTPATH/etc/$1
		return 0
	else
		logger -p $LOGFACILITY -t $LOGTAG "can't load config $CONFIG file: $SCRIPTPATH/etc/$1, exit $SCRIPTNAME"
		exit 1
	fi
}
 
function checkVar {
	$(grep -q ^$1"=" $SCRIPTPATH/etc/$CONFIG) && return 0
	logger -p $LOGFACILITY -t $LOGTAG "variable $1 not set in config $CONFIG, exit $SCRIPTNAME"
	exit 1
}
 
function checkCommand {
	$($1 > /dev/null 2>&1) && return 0
	logger -p $LOGFACILITY -t $LOGTAG "command $1 failed with exit code: $?, exit $SCRIPTNAME"
	exit 1
}
 
function checkDir {
	test -d $1 && return 0
	logger -p $LOGFACILITY -t $LOGTAG "directory $1 not found, exit $SCRIPTNAME"
	exit 1
}
 
# stop inotifywait
[ $2 == "stop" ] && stopProcess $1 $2
 
# show status
[ $2 == "status" ] && statusProcess $1 $2
 
# check start
checkStart
 
# start logging
logger -p $LOGFACILITY -t $LOGTAG "starting $SCRIPTNAME ..."
 
# check requirements
checkRequirement "/bin/pidof"
checkRequirement "/usr/sbin/csync2"
checkRequirement "/usr/bin/inotifywait"
 
# load config
readConfig $CONFIG
 
# check variables
checkVar 'DIR'
checkVar 'USER'
checkVar 'GROUP'
checkVar 'CHMOD'
checkVar 'EXCLUDE'
 
# check user and group
checkCommand "getent passwd $USER"
checkCommand "getent group $GROUP"
 
# check directory to monitor
checkDir $DIR
 
# start inotifywait in subshell
(
	# create pid file
	echo $BASHPID > $SCRIPTPATH/var/run/$CONFIG.pid
 
	# start inotify monitor
	logger -p $LOGFACILITY -t $LOGTAG "starting inotify monitor for config $CONFIG directory $DIR..."
	inotifywait -mrq --exclude "$EXCLUDE" --format '%w%f' -e close_write -e delete -e moved_to -e create $DIR | while read file; do
 
		# change owner and mode
		chown $USER:$GROUP -R $file > /dev/null 2>&1
		[ "$CHMOD" != "" ] && chmod $CHMOD -R $file > /dev/null 2>&1
 
		# execute csync2
		csync2 -xv 2>&1 | logger -t "csync2" -p local5.notice > /dev/null 2>&1
 
	done
)&
 
logger -p $LOGFACILITY -t $LOGTAG "exit $SCRIPTNAME"
 
exit 0
root@localhost:~# chmod +x /opt/inotifywait/bin/inotifywait.sh
root@localhost:~# vi /opt/inotifywait/etc/www.cfg
DIR="/var/www"
USER="root"
GROUP="admins"
CHMOD="g+w"
EXCLUDE="(wp-content/uploads)"
  • Zuletzt geändert: 2019/06/29 23:36