#!/bin/bash
# ## set messages, variables
##################################################################################
DATE=`date +%Y%m%d`
TIME=`date +%H%M%S`
MESSAGE1="BACKUP: OK"
MESSAGE2="BACKUP: Fail"
MESSAGE3="DELETE: OK"
MESSAGE4="DELETE: Fail"
SUM=0
set -x
#################################################################################
# ## Read paramlist.txt and look for files to be trasferred
##################################################################################
file='/name1/name2/paramlist.txt'
while read line
do
#
key=`echo $line | awk -F\| '{ print $1 }'`
ip=`echo $line | awk -F\| '{ print $2 }'`
fuser=`echo $line | awk -F\| '{ print $3 }'`
ftppass=`echo $line | awk -F\| '{ print $4 }'`
lpath=`echo $line | awk -F\| '{ print $5 $6 $7 $8 $9 }'`
fpath=`echo $line | awk -F\| '{ print $10 $11 $12 $13 $14}'`
subject=`echo $line | awk -F\| '{ print $15 }'`
email=`echo $line | awk -F\| '{ print $16 }'`
#
echo "step1-$key"
#################################################################################
# ## Create log for each folder (/name1/name2/)
# ## xrf$key$(date '+%y%m%d'.%H%M%S).log - xrfba11090729.101010.log
# ## error code > 0 mean problem of find, no files in the folder
##################################################################################
LOGFILE="xrf$key$(date '+%y%m%d').log"
mFile="$lpath*txt"
find $mFile >> $LOGFILE
if [ $? -gt 0 ];
then
file_ctr=0
else
file_ctr=`egrep -cv '#|^$' /name1/name2/$LOGFILE`
fi
SUM=$(echo "$SUM + $file_ctr"|bc)
echo "total files:$SUM"
#
if [ $SUM -eq 0 ]; then
mailx -s "no FTP &subject" "$email"
else
echo "step2-ftp"
#################################################################################
# Upload to ftp; Create ftp log and trasfer log (/name1/name2/)
# ftp$(date '+%y%m%d'.%H%M%S).log - ftp1090729.101010.log
# DIRS # Name of source directory
# FILENAME # Name of file without directory
#################################################################################
vFTPName="ftp$key$(date '+%y%m%d').log"
cat $LOGFILE | while IFS= read line
do
DIRS=${line%/*}
FILENAME=${line#$DIRS/}
ftp -v -n $ip >>/name1/name2/$vFTPName <<EOF
quote user $fuser
quote pass $ftppass
lcd ${DIRS}
cd $fpath
bin
put ${FILENAME}
bye
EOF
done
fi
#
echo "step3-logs"
############################################## ###################################
# Check for (226 Transfer completed)
#################################################################################
ftp_ctr=`cat /name1/name2/$vFTPName | grep 226 | wc -l`
if [ "$ftp_ctr" = "$SUM" ];
then
echo "No Error"
mailx -s "$subject" "$email" < "$LOGFILE"
else
echo "Error has occured"
mailx -s "FTP failure $subject" "
email@name.com" < "$LOGFILE"
exit
fi
#
SUM=0
done </impact/conv/paramlist.txt
#