#!/bin/bash
# file: get-files-listener.sh v.1.1.2 docs at the end
umask 000 ;
# print the commands
# set -x
# print each input line as well
set -v
# exit the script if any statement returns a non-true return value. gotcha !!!
# set -e
# set the variables from the $0.hostname.conf file which has ini like syntax
doSetVars(){
export PATH=/cygdrive/c/cygwin/bin/:$PATH
MyDir=`dirname $(readlink -f $0)`
Tmp="$MyDir/tmp/tmp.$$"
mkdir -p $Tmp #create the tmp dir if it does not exist
( set -o posix ; set ) >$Tmp/variables.before
MyName=`basename $0`
IniFile="$MyDir/$MyName.`hostname`.conf"
IniSection=MainSection
LogFile="$MyDir/$MyName.log"
# get the machine / host specific configuration
doParseIniFile
#source $0
( set -o posix ; set ) >$Tmp/variables.after
doLog " Using the following vars :"
cmd="comm --nocheck-order -3 $Tmp/variables.before $Tmp/variables.after"
doRunCmdAndLog $cmd
rm -fr $Tmp
}
#eof function doSetVars
# parse the ini like $0.hostname.conf and set the variables
doParseIniFile(){
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/;.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $IniFile \
| sed -n -e "/^\[$IniSection\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
}
#eof function parseIniFile
# run a command and log the call and its output to the LogFile
# usage: doRunCmdAndLog $cmd
doRunCmdAndLog(){
cmd="$*" ;
doLog " DEBUG running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
[ $? -eq 0 ] || doLog "ERROR : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
doLog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function doRunCmdAndLog
# run a command on failure exit with message
# usage: doRunCmdOrExit $cmd
doRunCmdOrExit(){
cmd="$*" ;
doLog " DEBUG running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
# if error occured during the execution exit with error
[ $? -eq 0 ] || doExit "ERROR : FATAL : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
#if no error occured just log the message
doLog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function doRunCmdOrExit
# echo pass params and print them to a log file
doLog(){
# check terminal if exists echo
test -t 1 && echo "`date +%Y.%m.%d-%H:%M:%S` [$$] $*"
# check LogFile and
test -z $LogFile || {
echo "`date +%Y.%m.%d-%H:%M:%S` [$$] $*" >> $LogFile
} #eof test
}
# eof function doLog
# exit with passed status and message
doExit(){
ExitStatus=0
case $1 in
[0-9]) ExitStatus="$1"; shift 1;;
esac
Msg="$*"
test "$ExitStatus" = "0" || Msg=" ERROR: $Msg"
doLog " $Msg"
exit $ExitStatus
}
#eof function Exit
#
# waits for the loop to occur
doWaitLoop(){
count=0
while [[ $count -lt $NUM_OF_SECONDS ]] ; do
#debug echo $count ;
count=$((count + 1)) ;
sleep 1;
# go to the server and copy the file flag
doScpFlagFile
# if the file flag exist locally
test -f "$FLAG_FILE_LOCAL" && {
# do copy the whole server
doScpFiles
doRemoveFilesDir
doRecreateFilesDir
# set the time flag
flag_file_time=`cat $FLAG_FILE_LOCAL`
mv $FLAG_FILE_LOCAL $FLAG_FILE_LOCAL.$flag_file_time
continue
}
done ;
#eof while
#debug echo "Value of count after loop is: $count"
}
#eof doWaitLoop
doScpFlagFile(){
doLog "START doScpFlagFile"
cmd="scp $USER@$SERVER:$FLAG_FILE_SERVER $LOCAL_DIR"
doRunCmdAndLog "$cmd"
doLog "STOP doScpFlagFile"
}
#eof func doScp
doScpFiles(){
doLog "START doScpFiles"
scp -rv $USER@$SERVER:$SERVER_DIR $LOCAL_DIR
doLog "STOP doScpFiles"
}
#eof func doScp
doRemoveFilesDir(){
doLog "START doRemoveFilesDir"
#scp -v user@server:/dir/data/*.txt .
cmd="ssh $USER@$SERVER nohup bash -c \"rm -fR $SERVER_DIR\""
doRunCmdAndLog "$cmd"
doLog "STOP doRemoveFilesDir"
}
#eof func doRemoveFilesDir
doRecreateFilesDir(){
doLog "START doRecreateFilesDir"
#scp -v user@server.domain.fi:/dir/data/*.txt .
cmd="ssh $USER@$SERVER nohup bash -c \"mkdir -p $SERVER_DIR\""
doRunCmdAndLog $cmd
cmd="ssh $USER@$SERVER nohup bash -c \"chmod -R 777 $SERVER_DIR\""
doRunCmdAndLog $cmd
doLog "STOP doRecreateFilesDir"
}
#eof func doRecreateFilesDir
# the main function called
main(){
doSetVars
doWaitLoop
Exit 0 "MAIN END"
}
#eof function main
# Action !!!
main
# Purpose:
# to listen for file flag on an ssh server and copy all the files
# from a predifined dir on the server to a pre-defined dir locally
# to provide an easy starting template for writing bash and sh scripts
# Prerequisites: ppk authentication between the ssh client and the server
# with the following features:
# - prints the set in the script variables
# - separation of host specific vars into $0.`hostname`.conf file
# - doLog function for both xterm and log file printing
# - for loop examples with head removal and inline find and replace
#
# EOF File:get-files-listener.sh
# ==========================================================
# VersionHistory
# 1.0.0 --- ysg --- 2012-09-14 13:39:24 --- Initial creation
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!