Useful FTP Bash Script

by Geoff on July 1, 2008

I needed to be able to automate the download of specifically named files from an FTP server and then delete them upon successful download. The need arose when the old script I was using was downloading some zero byte files (likely due to poor network conditions, workstation issues or remote server issues) then deleting the source file! D’oh!!

This script checks that the file is not a zero byte file before deleting from the remote server. Not great error/consistency checking, but better than I had before. It also copies the successfully downloaded files to an IMPORT directory and then archives the originally downloaded files to an ARCHIVE directory.

Hopefully you find it useful.

Note: I updated the post to include logging.

#!/bin/bash<br />
################################################<br />
# Set some of the variables that will be needed<br />
# during the execution of this script<br />
################################################<br />
# set current date<br />
now=`date +%Y-%m-%d-%H%M-%S`<br />
#<br />
# set the root dir<br />
rootdir=&quot;/ftp_files&quot;<br />
#<br />
# set the download directory<br />
dldir=&quot;$rootdir/DOWNLOADS&quot;<br />
#<br />
# set session download dir<br />
sessdldir=&quot;$dldir/$now&quot;<br />
#<br />
# set the import dir<br />
importdir=&quot;$rootdir/IMPORT&quot;<br />
#<br />
# set the archive dir<br />
archivedir=&quot;$rootdir/ARCHIVE&quot;<br />
#<br />
# set up logging<br />
LOGFILE=&quot;$rootdir/LOGS/ftplog.txt&quot;<br />
#<br />
################################################<br />
# Lets get to work<br />
#################################################<br />
# create a directory based on todays date<br />
mkdir $sessdldir<br />
#<br />
# change to the new directory prior to FTP connection<br />
cd $sessdldir<br />
#<br />
if [ -f $LOGFILE ]<br />
then<br />
echo &quot;****** FTP PROCESS STARTED AT: $now ******&quot; &amp;gt;&amp;gt; $LOGFILE<br />
else<br />
echo &quot;****** NEW LOG FILE CREATED AT: $now ******&quot; &amp;gt; $LOGFILE<br />
echo &quot;****** FTP PROCESS STARTING AT: $now ******&quot; &amp;gt;&amp;gt; $LOGFILE<br />
fi</p>
<p>## FTP Connection parameters<br />
ftpserver=server.com<br />
username=youruser<br />
password=yourpass<br />
remotedir=/directoryname</p>
<p>## Connect to FTP server and download all TXT files<br />
ftp -inv $ftpserver &amp;gt;&amp;gt; $LOGFILE &amp;lt;&amp;lt;MYEND<br />
user $username $password<br />
cd $remotedir<br />
bin<br />
mget *.TXT<br />
bye<br />
MYEND</p>
<p>## Test for existence of files in the session download dir<br />
## (we cd&#8217;d here earlier)<br />
if [ `ls | wc -l` -gt 0 ]<br />
then<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&amp;gt;&amp;gt; Files have been downloaded&quot; &amp;gt;&amp;gt; $LOGFILE<br />
dlfiles=`ls`<br />
echo &quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Downloaded files:&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;$dlfiles&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE</p>
<p>## Create the FTP delete script in order to remove the downloaded<br />
## files from the FTP server. We will be sure to check for empty<br />
## files below. The FTP delete script will be created in the session<br />
## download directory and named ftp.$PID<br />
echo &quot;open $ftpserver<br />
user $username $password<br />
binary<br />
cd $remotedir&quot; &amp;gt; $sessdldir/ftp.$$<br />
echo &quot;&amp;gt;&amp;gt; Creating delete script to delete non-zero files from remote server&quot; &amp;gt;&amp;gt; $LOGFILE</p>
<p>## Iterate through the downloaded files one by one<br />
for i in $( ls *.TXT ); do<br />
## If the file exists and is larger than 0 bytes, THEN add<br />
## a delete command to the FTP delete script. This will allow<br />
## us to delete all non-zero byte files from the FTP server.<br />
## Non-zero byte files could be a sign that the file didn&#8217;t<br />
## download properly due to an FTP timeout or other network<br />
## issues. By not deleting the non-zero byte file we can attempt<br />
## to download it the next time the script is run.<br />
if [ -s $i ]<br />
then<br />
echo &quot;del $i&quot; &amp;gt;&amp;gt; $sessdldir/ftp.$$<br />
echo &quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; $i will be added to delete script&quot; &amp;gt;&amp;gt; $LOGFILE<br />
fi<br />
done</p>
<p>## Finish off the FTP delete script<br />
echo &quot;quit&quot; &amp;gt;&amp;gt; $sessdldir/ftp.$$</p>
<p>echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&amp;gt;&amp;gt; Here is the delete script&quot;&amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE<br />
cat $sessdldir/ftp.$$ &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&amp;gt;&amp;gt; Running the delete script&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE</p>
<p>## Run the FTP script<br />
ftp -ivn &amp;lt; $sessdldir/ftp.$$ &amp;gt;&amp;gt; $LOGFILE</p>
<p>## Copy all the non-zero byte TXT files to the import folder<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&amp;gt;&amp;gt; Copying non-zero size files to IMPORT&quot; &amp;gt;&amp;gt; $LOGFILE</p>
<p>for i in $( ls *.TXT ); do<br />
if [ -s $i ]<br />
then<br />
cp $i $importdir<br />
echo &quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Copying $i to IMPORT directory&quot; &amp;gt;&amp;gt; $LOGFILE<br />
fi<br />
done<br />
fi</p>
<p>## Copy session specific download dir to the ARCHIVE folder<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&amp;gt;&amp;gt; Copying entire $sessdldir to $archivedir&quot; &amp;gt;&amp;gt; $LOGFILE</p>
<p>cp -R $sessdldir $archivedir/</p>
<p>## Delete the session specific download dir<br />
rm -rf $sessdldir<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&amp;gt;&amp;gt; Deleting $sessdldir&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE</p>
<p>## ENDING LOG FILE FOR SESSION<br />
echo &quot;****** FTP PROCESS ENDED AT: $now ******&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;******************************************************&quot; &amp;gt;&amp;gt; $LOGFILE<br />
echo &quot;&quot; &amp;gt;&amp;gt; $LOGFILE

Please consider subscribing to GeoffManning.com to receive new posts in your RSS Reader or by Email.

No related posts.

Leave a Comment

Previous post:

Next post: