Activities

December 2011
M T W T F S S
« Nov   Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Automating sFTP download and email alert upon file changes

Here I got a requirement to automate the download which using sftp account with custom ports. This account doesn’t not have any ssh access enabled.

This script can have the following features,

1. Synchronize the remote sftp location to local path
2. Can configure sftp custom port
3. An email alert will send to the specified users when a new file placed in the ftp location.

#!/bin/bash
lftp -e "mirror --delete --only-newer --verbose /FROM/  /var/RKFeeds/;quit" -u username,mypass sftp://secureftp.domain.com:10022

cat /dev/null > /tmp/maildata.log
MAIL_BODY=/tmp/maildata.log
FILES=/var/RKFeeds/FROM/*
for f in $FILES
do

### Searching new file presence
if grep -Fxq "$f" file_list.log
then
 touch /tmp/asd.log
else
  echo "New file" $f " has arrived " >> $MAIL_BODY
fi
done

## Rebuilding file list
FILES=/var/RKFeeds/FROM/*
cat /dev/null > file_list.log
for f in $FILES
do
 # take action on each file. $f store current file name
echo $f  >> file_list.log
done

##### sending mail if mail data file having content
if [[ -s $MAIL_BODY ]] ; then
echo "$MAIL_BODY has data.and mail sending"
sendEmail -f bkp-admins@mydomain.com  -t user2@mydomain.com,lijumathewliju@gmail.com -cc support@mydomain.com, user@mydomain.com  -u "New Feeds has arrived on" $(date +%d-%m-%Y)  -l /var/log/sendEmail  -o message-content-type=auto  message-file=$MAIL_BODY   -s smtpout.mydomain.net:25 -xu info@mydomain.com -xp mysmtppass
else
touch /tmp/asd.log
fi ;

Pls note that the location “/var/RKFeeds/FROM” is the local path where lftp sync the remote folder ” FROM” folder.
sendEmail : Is another smtp email sending program that help you to send email from the command line which using smtp account. So that these emails are not been marked as spam anywhere.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>