SFTP Email When New File Uploaded

I had setup vsftp for SSL (FTPES), but needed a way to send an alert to customer support whenever a file had been uploaded. Otherwise the support staff would need to manually check each customer’s folders to see if anything new had arrived.

Install Dnotify
apt-get install dnotify

Create your startup script
nano /dnotify.sh

dnotify -b -p 1 -r -C /home/ -e /email.sh {}

This will run dnotify in the -b background, no more than one -p process, -r recursive folder (subfolders), -C for file creations only, -e execute the following script.

Create your email script
nano /email.sh

#!/bin/bash
DIR=”$1″
rm /upload.txt
echo “Dear User,”>>/upload.txt
echo “A new file has been uploaded to the $DIR directory”>>/upload.txt
cat /upload.txt | mailx -s “New FTP File Upload” customersupport@domain.tld

Make both scripts run-able
chmod +x email.sh
chmod +x dnotify.sh

Run the script and test
./dnotify.sh
Upload a file using FTP/WinSCP/WGET or another method to any of the folders you’re searching (my script searches all of /home and subfolders).

I added this as a startup script.

I got most of my help from nixcraft

One thought on “SFTP Email When New File Uploaded”

  1. Thanks for the tips — found it via google. I modified it slightly to show new files in the email body and send to an external SMTP server via mailx and think folks might find this useful:

    #!/bin/bash
    DIR=”$1″
    logfile=”/root/email-upload.txt”
    rm $logfile
    echo “Hey All,”>>$logfile
    echo “A new file has been uploaded to the $DIR directory”>>$logfile
    echo “” >>$logfile
    echo “Here’s a directory listing of files changed in the last hour:”>>$logfile
    find $DIR -mmin -60 >>$logfile

    cat $logfile | mailx -v -r $FROM_EMAIL -s “New File Uploaded to $DIR” -S smtp=smtp://my.smtp.server:25 $EMAIL

    exit 1
    }

Leave a Reply

Your email address will not be published. Required fields are marked *