I am currently working on a solution, that would notify me whenever my mail-server isn't able to transfer files to my ftp-server. (Right now my logs get rotated once per night and immediately uploaded to a separate ftp-server)
The current config is as following:
HOST="..."
USER="..."
PASS="..."
DIR="/var/log/maillogs/"
LATEST="$(ls -t $DIR | head -n 2 | tail-n 1)"
FILE=$(basename $LATEST)
error=$(ftp -n $HOST <<EOF
quote USER $USER
quote PASS $PASS
prompt
lcd /var/log/maillogs/
cd /home/MailLog
put $FILE
quit
EOF
)
if [ $(echo $error | grep "failed")=="failed" ]
then
*Sending Mail via sendmail*
fi
For some reason, the if condition always returns true, so I receive a mail, no matter if the files were able to be transported or not.
Does anybody know, how I would have to change the if statement to actually only get the mail, when the it's failed to transfer the log?
I also already tried the method of
if [ $? -ne 0 ]
, but I am not too sure on how to test if it actually works. I tried giving a false IP or faulty user-logins, but by doing so, I only receive different errors.