I am syncing my backup server from production server through rsync but now i want to implement the failure notification. When i trying to get status of rsync command it does not give me status =0 on success. please let me know how i will implement this functionality.
failureMailFlag=0
rsync --timeout=600 -e ssh -avzr --delete $sourcePath/weblayout/ $destination:$destinationPath/weblayout/ --stats -i > $fileCreationPath/$tempfile 2>&1
rStatus=$?
if [ $rStatus == "0" ]
then
echo -e "$startdate\t\tweblayout\t\t$starttime\t\t$(date +"%m-%d-%Y %r")\t\tSuccess\t\t" >> $fileCreationPath/RsyncLog.txt
else
echo -e "$startdate\t\tweblayout\t\t$starttime\t\t$(date +"%m-%d-%Y %r")\t\tError\t\t">> $fileCreationPath/RsyncLog.txt
failureMailFlag="1"
cat $failureMail $tempfile >> $fileCreationPath/finalFailureMail.mai
wablayout=weblayout
varFailureMail=${varFailureMail}${wablayout}
fi
if [ $failureMailFlag == "1" ]
then
cat $fileCreationPath/*.mail finalFailureMail.mai $fileCreationPath/$varFailureMail $fileCreationPath/$tempfile | mailx -s "Failure Mail notification for RSYNC" $EmailSent
else
echo "successfully run"
fi
Try:
if [ $rStatus -eq "0" ]
== is for strings not ints I think
Use ; instead of 2>&1 like below
rsync --timeout=600 -e ssh -avzr --delete $sourcePath/weblayout/ $destination:$destinationPath/weblayout/ --stats -i > $fileCreationPath/$tempfile ; rStatus=$?
Related
I am trying to use the following bash script to login to a remote ftp and delete files older than N days old. Script says it is working and does not give an error - but files are not being deleted. What am I missing? Or is there a better way to do this? Keep in mind this is only a remote FTP and not SSH so I can NOT use the mtime function is why I am trying to do this. Can anyone help?
The usage is all commands - here is what I am using via ssh to run the script
./ftprem.sh -s ftp.server.com -u myusername -p mypassword -f /directory -d 3
#!/bin/bash
PROGNAME=$(basename $0)
OUTFILE="/tmp/ftplist.$RANDOM.txt"
CMDFILE="/tmp/ftpcmd.$RANDOM.txt"
ndays=14
print_usage() {
echo ""
echo "$PROGNAME - Delete files older than N days from an FTP server"
echo ""
echo "Usage: $PROGNAME -s -u -p -f (-d)"
echo ""
echo " -s FTP Server name"
echo " -u User Name"
echo " -p Password"
echo " -f Folder"
echo " -d Number of Days (Default: $ndays)"
echo " -h Show this page"
echo ""
echo "Usage: $PROGNAME -h"
echo ""
exit
}
# Parse parameters
options=':hs:u:p:f:d:'
while getopts $options flag
do
case $flag in
s)
FTPSITE=$OPTARG
;;
u)
FTPUSER=$OPTARG
;;
p)
FTPPASS=$OPTARG
;;
f)
FTPDIR=$OPTARG
;;
d)
ndays=$OPTARG
;;
h)
print_usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [[ -z "$FTPSITE" || -z "$FTPUSER" || -z "$FTPPASS" || -z "$FTPDIR" ]];
then
echo "ERROR: Missing parameters"
print_usage
fi
# work out our cutoff date
TDATE=`date --date="$ndays days ago" +%Y%m%d`
echo FTP Site: $FTPSITE
echo FTP User: $FTPUSER
echo FTP Password: $FTPPASS
echo FTP Folder: $FTPDIR
echo Removing files older than $TDATE
# get directory listing from remote source
ftp -i -n $FTPSITE <<EOMYF > /dev/null
user $FTPUSER $FTPPASS
binary
cd $FTPDIR
ls -l $OUTFILE
quit
EOMYF
if [ -f "$OUTFILE" ]
then
# Load the listing file into an array
lista=($(<$OUTFILE))
# Create the FTP command file to delete the files
echo "user $FTPUSER $FTPPASS" > $CMDFILE
echo "binary" >> $CMDFILE
echo "cd $FTPDIR" >> $CMDFILE
COUNT=0
# loop over our files
for ((FNO=0; FNO<${#lista[#]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
FMM=${lista[`expr $FNO+5`]}
FDD=${lista[`expr $FNO+6`]}
FYY=${lista[`expr $FNO+7`]}
if [[ $FYY == *\:* ]]
then
FDATE=`date -d "$FMM $FDD" +'%Y%m%d'`
else
FDATE=`date -d "$FMM $FDD $FYY" +'%Y%m%d'`
fi
# echo $FDATE
# check the date stamp
if [[ $FDATE -lt $TDATE ]];
then
echo "Deleting ${lista[`expr $FNO+8`]}"
echo "delete ${lista[`expr $FNO+8`]}" >> $CMDFILE
COUNT=$[$COUNT + 1]
fi
done
echo "quit" >> $CMDFILE
if [[ $COUNT -gt 0 ]];
then
cat $CMDFILE | tr -d "\r" > $CMDFILE
ftp -i -n $FTPSITE < $CMDFILE > /dev/null
else
echo "Nothing to delete"
fi
rm -f $OUTFILE $CMDFILE
fi
If this helps your debugging...
In the# Parse parameter section of the script, the options variable your have just before the case block has value options=':hs:u:p:f:d:' instead of options=':h:s:u:p:f:d:'
I thought i should point that out.
I want to check for file in directory if there then push it to ssh server checing server connection if file not there then try 3 times with each 1min interval and in between if it comes ( on 2nd attend for example) then try again to connect ssh and push. else check for 3 attempts and exit
Please check my below code it is halting after 1st attempt ( during 2nd attempt I am making file available)
#!/bin/sh
echo "OK, start pushing the Userdetails to COUPA now..."
cd /usr/App/ss/outbound/usrdtl/
n=0
until [ $n -ge 3 ] || [ ! -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ]
do
if [ -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ] ;
then
pushFiles()
else
n=$[$n+1]
sleep 60
echo " trying " $n "times "
fi
done
pushFiles()
{
echo "File present Now try SSH connection"
while [ $? -eq 0 ];
do
echo $(date);
scpg3 -v /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv <sshHost>:/Incoming/Users/
if [ $? -eq 0 ]; then
echo "Successfull"
echo $(date);
echo "Successfull" >> /usr/App/ss/UserApproverDetails.log
exit 1;
else
echo $(date);
echo "Failed" >> /usr/App/ss/UserApproverDetails.log
echo "trying again to push file.."
scpg3 -v /usr/App/sg/outbound/usrdtl/USERS_APPROVERS_*.csv <ssh Host>:/Incoming/Users/
echo $(date);
exit 1;
fi
done
}
I've tried to simplify this code for you. I hope it helps:
#!/bin/bash
outdir="/usr/App/ss/outbound/usrdtl"
logfile="/usr/App/ss/UserApproverDetails.log"
file_prefix="USERS_APPROVERS_"
function push_files() {
echo "File present now try SSH connection"
local attempts=1
local retries=2
date
while [[ ${attempts} -lt ${retries} ]]; do
if scp ${outdir}/${file_prefix}*.csv <sshHost>:/Incoming/Users/ ; then
echo "Successful" | tee -a ${logfile}
date
exit 0
else
echo "Failed" >> ${logfile}
fi
attempts=$((attempts+1))
do
echo "scp failed twice" | tee -a ${logfile}
exit 2
}
echo "OK, start pushing the Userdetails to COUPA now..."
cd ${outdir}
attempts=1
retries=3
while [[ ${attempts} -lt ${retries} ]]; do
echo "looking for files...attempt ${attempts}"
if test -n "$(shopt -s nullglob; echo ${outdir}/${file_prefix}*.csv)"; then
push_files()
fi
attempts=$((attempts+1))
sleep 60
done
echo "Files were never found" | tee -a ${logfile}
exit 1
Look at this code and tell me how it's not doing what you're trying to do. The most complicated part here is the nullglob stuff, which is a handy trick to see if any file in a glob matches
Also, I generally used bashisms.
have some problem with shell script.
In our office we set up only few commands, that available for devs when they are trying ssh to server. It is configured with help of .ssh/authorized_keys file and available command for user there is bash script:
#!/bin/sh
if [[ $1 == "--help" ]]; then
cat <<"EOF"
This script has the purpose to let people remote execute certain commands without logging into the system.
For this they NEED to have a homedir on this system and uploaded their RSA public key to .ssh/authorized_keys (via ssh-copy-id)
Then you can alter that file and add some commands in front of their key eg :
command="/usr/bin/dev.sh",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty
The user will do the following : ssh testuser#server tail testserver.example.com/2017/01/01/user.log
EOF
exit 0;
fi
# set global variable
set $SSH_ORIGINAL_COMMAND
# set the syslog path where the files can be found
PATH="/opt/syslog/logs"
# strip ; or any other unwanted signs out of the command, this prevents them from breaking out of the setup command
if [[ $1 != "" ]]; then
COMMAND=$1
COMMAND=${COMMAND//[;\`]/}
fi
if [[ $2 != "" ]]; then
ARGU1=$2
ARGU1=${ARGU1//[;\`]/}
fi
if [[ $3 != "" ]]; then
ARGU2=$3
ARGU2=${ARGU2//[;\`]/}
fi
if [[ $4 != "" ]]; then
ARGU3=$4
ARGU3=${ARGU3//[;\`]/}
fi
# checking for the commands
case "$COMMAND" in
less)
ARGU2=${ARGU1//\.\./}
FILE=$PATH/$ARGU1
if [ ! -f $FILE ]; then
echo "File doesn't exist"
exit 1;
fi
#echo " --------------------------------- LESS $FILE"
/usr/bin/less $FILE
;;
grep)
if [[ $ARGU2 == "" ]]; then
echo "Pls give a filename"
exit 1
fi
if [[ $ARGU1 == "" ]]; then
echo "Pls give a string to search for"
exit 1
fi
ARGU2=${ARGU2//\.\./}
FILE=$PATH/$ARGU2
/usr/bin/logger -t restricted-command -- "------- $USER Executing grep $ARGU1 \"$ARGU2\" $FILE"
if [ ! -f $FILE ]; then
echo "File doesn't exist"
/usr/bin/logger -t restricted-command -- "$USER Executing $#"
exit 1;
fi
/bin/grep $ARGU1 $FILE
;;
tail)
if [[ $ARGU1 == "" ]]; then
echo "Pls give a filename"
exit 1
fi
ARGU1=${ARGU1//\.\./}
FILE=$PATH/$ARGU1
if [ ! -f $FILE ]; then
echo "File doesn't exist"
/usr/bin/logger -t restricted-command -- "$USER Executing $# ($FILE)"
exit 1;
fi
/usr/bin/tail -f $FILE
;;
cat)
ARGU2=${ARGU1//\.\./}
FILE=$PATH/$ARGU1
if [ ! -f $FILE ]; then
echo "File doesn't exist"
exit 1;
fi
/bin/cat $FILE
;;
help)
/bin/cat <<"EOF"
# less LOGNAME (eg less testserver.example.com/YYYY/MM/DD/logfile.log)
# grep [ARGUMENT] LOGNAME
# tail LOGNAME (eg tail testserver.example.com/YYYY/MM/DD/logfile.log)
# cat LOGNAME (eg cat testserver.example.com/YYYY/MM/DD/logfile.log)
In total the command looks like this : ssh user#testserver.example.com COMMAND [ARGUMENT] LOGFILE
EOF
/usr/bin/logger -t restricted-command -- "$USER HELP requested $#"
exit 1
;;
*)
/usr/bin/logger -s -t restricted-command -- "$USER Invalid command $#"
exit 1
;;
esac
/usr/bin/logger -t restricted-command -- "$USER Executing $#"
The problem is next:
when i try to exec some command, it takes only first argument, if i do recursion in files by using {n,n1,n2} - it doesn't work:
[testuser#local ~]$ ssh testuser#syslog.server less srv1838.example.com/2017/02/10/local1.log |grep 'srv2010' | wc -l
0
[testuser#local ~]$ ssh testuser#syslog.server less srv2010.example.com/2017/02/10/local1.log |grep 'srv2010' | wc -l
11591
[testuser#local ~]$ ssh testuser#syslog.server less srv{1838,2010}.example.com/2017/02/10/local1.log |grep 'srv2010' | wc -l
0
[testuser#local ~]$ ssh testuser#syslog.server less srv{2010,1838}.example.com/2017/02/21/local1.log |grep 'srv2010' | wc -l
11591
Could someone help me, how can i parse\count command arguments to make it work?
Thank you and have a nice day!
The number of arguments for a bash script would be $#. As a quick example:
#!/bin/bash
narg=$#
typeset -i i
i=1
while [ $i -le $narg ] ; do
echo " $# $i: $1"
shift
i=$i+1
done
gives, for bash tst.sh a b {c,d}
4 1: a
3 2: b
2 3: c
1 4: d
In your script, the command to execute (cat, less, ...) gets explicitly only the second argument to the script. If you want to read all arguments, you should do something like this (note: only a hint, removed all sorts of checks etc..)
command="$1"
shift
case $command in
(grep) pattern="$1"
shift
while [ $# -gt 0 ] ; do
grep "$pattern" "$1"
shift
done
;;
esac
note: added some quotes as comment suggested, but, being only a hint, you should carefully look at quoting and your checks in your own script.
Less command working now:
case "$COMMAND" in
less)
if [[ $ARGU1 == "" ]]; then
echo "Pls give a filename"
exit 1
fi
FILES_LIST=${#:2}
FILE=(${FILES_LIST//\.\./})
for v in "${FILE[#]}";do
v=${v//[;\']/}
if [ ! -f $v ]; then
echo "File doesn't exist"
fi
/usr/bin/less $PATH/$v
done;;
tail command works too with 2 and more files, but i can't execute tail -f command on two files unfortunately.
I created .sh file to grep specific words in log and will send a mail to particular emailID (Set Cronjob to run this script every 1hour). Its working as expected, But the Problem is if there is no error also I am getting Empty Mail.
Needs to get Mail, if we caught error and DBError file has content. Kindly please help me on this to resolve this issue.
#!/bin/bash
if [ ! -e DBErrors ] ; then
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > DBErrors
mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com < DBErrors
else
comm -23 <(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log) DBErrors | mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > DBErrors
fi
Thanks
Jayaram
Could you try this;
#!/bin/bash
dbErrors="/tmp/DBErrors"
if [ ! -e "$dbErrors" ]; then
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > $dbErrors
mailbody=$(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log); [[ -n "$mailbody" ]] && mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com
else
mailbody=$(comm -23 <(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log| sort -n) <(sort -n "$dbErrors")); [[ -n "$mailbody" ]] && mail -s "ALERT: sqlException" Jayaram.Ponnusamy#gmail.com
grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > $dbErrors
fi
Ok, I can get this to run and put the file on the remote server. However when it does run, I want it to echo a Transfer failed, Transfer OK, Zero bytes in file and No such file or directory. It just runs down the code and sends an email of the last entry which is no such file or directory when clearly the file transferred to the server. What am I doing wrong ??
Thx in advance, I thank you.
#!/bin/sh
HOST=10.10.1.2
USER='test'
RECIP="user#somemail.com"
cd /home/test
FILE=ARG$(date '+%Y%m%d').txt
BYTES=`stat -c%s $FILE`
CONNECTION=`netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | awk '{printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print ""}'`
RUN_AS=`whoami`
RESULT=`sftp $USER#$HOST <<EOF
FILE=ARG$(date '+%Y%m%d').txt
cd /998979/DES
if [ -e $FILE ]; then
put $FILE
EOF`
echo $CONNECTION "Connection to network is established."
ls | xargs wc -wl for file in *; echo $FILE done
if [ $? -eq 0 ]; then
echo "$RESULT" "Transfer of file failed. `date`"
SBJ="Transfer of file failed `date`"
fi
echo "$RESULT" "Transfer OK"
if [ $? -eq 0 ]; then
SBJ="Transfer OK. `date`"
echo "$RESULT" "Transfer OK"
fi
if \[ ! -s ${FILE} \]; then
echo "File: ${FILE} is not present or a ZERO byte file"
SBJ="Failed to upload. Zero bytes in file. `date`" # zero bytes sent
fi
if [ -a $FILE ]; then
echo "$RESULT" "No such file or directory"
SBJ="No such file or directory. `date`"
fi
BODY="Process Report.......
SRC SERVER:`hostname`
DST SEVER: TESTSERVER
SCRIPT: /usr/local/bin/put.sh
RUN AS: "$RUN_AS"
RESULT: "$RESULT" "$FILE"
CONNECTION: "$CONNECTION"
FILENAME: "$FILE"
BYTES: "$BYTES"
DATE/TIME: `date`"
echo "$BODY" | mail -s "$SBJ" "$RECIP"
You can use /bin/sh -xv yourshellscript.sh to understand what is happening.
And you could log important messages to the system log using logger
count=$?
if [ $count -eq 0 ]; then
echo "$RESULT" "Transfer of file failed.
`date`" SBJ="Transfer of file failed `date`"
else
echo "$RESULT" "Transfer OK"
fi
Assign the result to a variable, Also, try to use exit when you don't need to do more processing.