In another threat I promised to submit a script for transferring files via FTP, here it comes. I use it at work for transferring files to customers and I have about ten of them running each night. It can be easily adapted for SFTP transfers.
There is no email messaging included because I have a control job running each morning which collects error logs (Written in VB) and sent an email to my account.
What does it?
Once configured and called by a cronjob by putting it into a cron. - directory (Don't forget to set chmod 0755 and chown root:root if you want to call it by cron) or included in the crontab of an user it looks into the specified local directory, counts the files and loops until all files are transferred. It opens a connection for each file, queries the transfer status and if successful it deletes the file. All actions are logged.
Prerequisites: This script uses ncftpput, a part of the ncftp-suite. You have to install ncftp with the command apt-get install ncftp
./edit: Found an error, while cleaning the script and commenting the actions I have moved the date part to a wrong position. It has to be called before the logfiles are generated, otherwise the date variable has no content and the logfile will be created without a date stamp.
#!/bin/sh
#
# The legal stuff: Use it at your own risk ;)
# Test this script before you use it in production environments, it deletes successful transferred files!
# Developed by datadigger in 2012
#
# set time and date
DATUM=$(date "+%d%m%Y")
ZEIT=$(date "+%d.%m.%Y %H:%M:%S")
UHR=$(date "+%H:%M:%S")
#
# This part contains variables you have to set
# name of remote FTP host
HOST='<ftp.host.name>'
# username on remote host
USER='<username>'
# your passwd
PASSWD='<password>'
# local path with wildcard for all files
LOCALPATH1='</path/local>/*' #Same path as No.2 - don't forget the trailing /*
# local path
LOCALPATH2='<path/local>' #Same path as No.1 without trailing /*
# target directory on remote host
RDIR="</path/on/remote/server/>"
# set the logfiles, the paths are just suggestions
LOGNAME="/var/ftp/pub/log/${DATUM}_ftplog.txt"
ERRLOG="/var/ftp/pub/errorlog/${DATUM}_ftp.txt"
TRANSFERLOGNAME="/var/ftp/pub/log/${DATUM}_ftperrorlog.txt"
# End of configurable variables
#
# initialize variables
status=-1
statuserr=0
number_of_files=0
files_found=0
FNAME=''
# check for files in local directory
echo >> ${LOGNAME}
echo "START_TRANSFER ${ZEIT} by ftp-Client" >> ${LOGNAME}
echo >> ${LOGNAME}
if [ "$(ls -A "$LOCALPATH2")" == "" ]; then
echo "$UHR $LOCALPATH2 is empty" >> ${LOGNAME}
echo "$UHR End of script, will stop now" >> ${LOGNAME}
break
exit 0
# exit if local directory is empty
else
files_found=$(find "$LOCALPATH2" -type f | wc -l)
echo "$UHR Found $files_found files, starting transfer" >> ${LOGNAME}
fi
# start to loop through all files
for FILE in $LOCALPATH1
do
#
UHR=$(date "+%H:%M:%S")
status=-1
fname=$(basename $FILE)
# the transfer call
/usr/bin/ncftpput -t 25 -o useFEAT=0,useCLNT=0,useMDTM=0 -d $LOGNAME -u $USER -p $PASSWD $HOST $RDIR $FILE 2>> $TRANSFERLOGNAME
# query the transfer status
status=$?
echo "return code=$status" >> ${LOGNAME}
UHR=$(date "+%H:%M:%S")
# Analyze the status and write to log
if [[ $status -ne 0 ]] ; then
echo "$ZEIT $FILE TRANSFER FAILED" >> ${TRANSFERLOGNAME}
echo "$ZEIT $FILE TRANSFER FAILED" >> ${ERRLOG}
echo "-------------" >> ${LOGNAME}
# add 1 to statuserr if transfer failed
let statuserr=$statuserr+1
else
echo "$UHR $FILE sent ok" >> ${LOGNAME}
# transfer ok, file will be deleted
rm $FILE
echo "$UHR $FILE deleted" >> ${LOGNAME}
echo "-------------" >> ${LOGNAME}
let number_of_files=$number_of_files+1
fi
# Log the status types reported by status query. There are seven ncftp can give back, I have listed only the important ones
if [[ $status -eq 1 ]] ; then
echo "$UHR Error connecting to FTP-server!" >> ${LOGNAME}
fi
if [[ $status -eq 2 ]] ; then
echo "$UHR Timeout while connecting to FTP-server" >> ${LOGNAME}
fi
if [[ $status -eq 3 ]] ; then
echo "$UHR Error while transferring file! $fname was not transferred." >> ${LOGNAME}
fi
if [[ $status -eq 4 ]] ; then
echo "$UHR Timeout while transferring file $fname to FTP-server" >> ${LOGNAME}
fi
if [[ $status -gt 4 ]] ; then
echo "$UHR Other error while transferring file $fname to FTP-server" >> ${LOGNAME}
fi
done
# end of loop
UHR=$(date "+%H:%M:%S")
# Summarize the transfers
if [[ $statuserr -eq 0 ]] ; then
echo "Error status: $statuserr" >> ${LOGNAME}
echo "$UHR Found: $files_found - Transferred successful: $number_of_files" >> ${LOGNAME}
echo "$UHR Finished transfer, script will end now" >> ${LOGNAME}
echo "######################################" >> ${LOGNAME}
else
echo "Error status $statuserr" >> ${LOGNAME}
echo "$UHR Found: $files_found - Transferred: $number_of_files" >> ${LOGNAME}
echo "$UHR $statuserr error(s) found! Check errorlog!" >> ${LOGNAME}
echo "$UHR Finished transfer, script will end now" >> ${LOGNAME}
echo "######################################" >> ${LOGNAME}
fi
exit 0
Alles anzeigen