sftp batch mode
#!/bin/bash
#written by Me 25/07/2011 onwards
#This script is intended to be run from cron hourly
#It will ftp all the files it finds to remote ftp server
#turn on debugging, -x starts, +x stops
set +x
BASE=/wworks_ftp/bar_ia
SCRIPTS=${BASE}/scripts
UPLOADS=${BASE}/uploads
ARCHIVE=${BASE}/archive
EMAIL=${SCRIPTS}/emailtmp-$$
MAILTO=user@company.com
SENDEMAIL=no
DATENUM=`date +%d`
#Test to force 1st of the month
#DATENUM=01
if [ $DATENUM == 01 ]
then
MONTHTEST=" - 1st of the month test email."
SENDEMAIL=yes
fi
SFTP=/usr/bin/sftp
IAFTP=123.123.123.123
IAUSER=username
IAPASSWD=password
PUBKEY=${SCRIPTS}/access.pub
PRIVKEY=${SCRIPTS}/access.key
OPTIONS="-oIdentityFile=${PRIVKEY}"
#add trailing / to IABASEPATH if set, this copes when base path is just /
IABASEPATH=
REG=${IABASEPATH}downloads
SALES=${IABASEPATH}sales
#write out start of email log file
echo "From /works_ftp/bar_ia_ftp/scripts/ia_ftpupload.sh on xx-cms01 at `date +%H:%M:%S` UTC." > ${EMAIL}
echo "" >> ${EMAIL}
echo "File upload to Incomes Access" >> ${EMAIL}
echo "=============================" >> ${EMAIL}
echo "" >> ${EMAIL}
#Outer loop controls Registration and Sales loops
for FILETYPE in REG SALES
do
#Test to see if files are present to ftp.
#If files exist (rc=0), carry on.
#If not, continue to next part of loop
echo "Filetype to test is ${FILETYPE}"
ls ${UPLOADS}/*${FILETYPE}* > /dev/null 2>&1
FILESEXIST=$?
#echo "filesexist is $FILESEXIST"
if [[ $FILESEXIST != 0 ]]
then
echo "No files of type ${FILETYPE} to send to IA" >> ${EMAIL}
echo " " >> ${EMAIL}
echo "------- " >> ${EMAIL}
continue
fi
#Inner loop actually sends files
for FILE in `ls -1 ${UPLOADS}/*${FILETYPE}*`
do
if [[ $FILE =~ csv$ ]]
then
#echo "File ends .csv"
DUMMY=true
else
echo "${FILE} does not end .csv, so adding extension." >> ${EMAIL}
mv ${FILE} ${FILE}.csv
FILE=${FILE}.csv
#echo "new filename is ${FILE}"
fi
echo "FTP'ing `basename ${FILE}` " >> ${EMAIL}
echo " " >> ${EMAIL}
eval IAPATH=\$$FILETYPE
#echo "IAPATH is eval result is ${IAPATH}"
#echo "sftp command is:- ${SFTP} ${OPTIONS} ${IAUSER}@${IAFTP}"
${SFTP} ${OPTIONS} ${IAUSER}@${IAFTP} << EOF
cd ${IAPATH}
lcd ${UPLOADS}
put ${FILE}
bye
EOF
SFTPRESULT=$?
#Force result code for testing
#SFTPRESULT=1
if [ ${SFTPRESULT} -eq 0 ]
then
echo "Returncode is 0, so moving ${FILE} to archive." >> ${EMAIL}
echo " " >> ${EMAIL}
mv ${FILE} ${ARCHIVE}
else
echo "sftp copy of ${FILE} was not successful, return code was ${SFTPRESULT}." >> ${EMAIL}
echo " " >> ${EMAIL}
SENDEMAIL=yes
fi
echo "------- " >> ${EMAIL}
#end of FILE loop
done
#end of FILETYPE do
done
#Mail Out
if [ ${SENDEMAIL} == yes ]
then
#echo ${SENDEMAIL} >> ${EMAIL}
echo "Sending notification mail at `date +%H:%M:%S` UTC." >> ${EMAIL}
/bin/mail ${MAILTO} -s "Upload to IAccess failed.${MONTHTEST}" < ${EMAIL}
else
#don't send mail, so mail command is commented out. Left in for testing
#DONTSEND is a dummy statement to prevent an error when everything else is commented out.
DONTSEND=true
echo "Sending notification mail at `date +%H:%M:%S` UTC." >> ${EMAIL}
/bin/mail ${MAILTO} -s "Upload to Incomes Access succeeded.${MONTHTEST}" < ${EMAIL}
fi
#cleanup
rm ${EMAIL}
exit 0