Page 1 of 1

TS-421 file change information mail

Posted: Fri Nov 28, 2014 8:51 pm
by oahiskali
Is there a way to send mail if a file added, deleted or changed in a folder to user which have access that folder.

Re: TS-421 file change information mail

Posted: Sat Nov 29, 2014 6:16 am
by pwilson
oahiskali wrote:Is there a way to send mail if a file added, deleted or changed in a folder to user which have access that folder.


As long as your Notifications feature is working on your NAS, you can use the following script to watch a users $HOME directory for changes. Please feel free to modify it to your requirements.

/share/homes/admin/bin/watchdir.sh

Code: Select all

#!/bin/sh
#
#  watchdir.sh [username]
#
# This script will watch the $HOME for [username] and send an e-mail if any files change within it.
# This script requires that the "Home Folders" feature is enabled.  See: http://docs.qnap.com/nas/4.1/Home/en/users.htm
#
if [ "$1x" = "x" ]; then
   echo "Usage: $0 [username]"
   echo " "
   echo "This script will watch the \$HOME for [username]"
else
   user=$1
   dir=$(grep $user /etc/config/passwd | cut -d: -f6)
fi
echo "done"

[ ! -d $dir ] && echo "$dir does not seem to exist"

SendTo="me@somewhere.com"
SendFrom="watchdir.sh@XXXXXXXX.myqnapcloud.com"
Subject="\$HOME ($dir) directory for $user changed."
#
#
rm -f /tmp/watchdir.email
touch /tmp/watchdir.email
echo "From: ${SendFrom}"     >>/tmp/watchdir.email
echo "To: ${SendTo}"         >>/tmp/watchdir.email
echo "Subject: ${Subject}"   >>/tmp/watchdir.email
echo " "                     >>/tmp/watchdir.email

while true #run indefinitely
do
inotifywait -r -e modify,attrib,close_write,move,create,delete $dir >>/tmp/watchdir.email \
&& cat /tmp/watchdir.email | sendmail -t
done
rm -f /tmp/watchdir.email
#done



Don't forget to make this script executable:

Code: Select all

chmod +x /share/homes/admin/bin/watchdir.sh


Note: This script is a continuous loop, so "spawn" it in the background....

Code: Select all

/share/homes/admin/bin/watchdir.sh admin &


Will watch for file changes within "admin's" $HOME directory

Also please Note that some E-mail providers do not allow "fake" From lines in e-mails. You might have to change the SendFrom variable in this script to a valid e-mail address that your E-mail provider recognizes as valid.

Re: TS-421 file change information mail

Posted: Wed Dec 10, 2014 7:15 pm
by oahiskali
Thanks Patrick,

But I dont know how to use a script.I would be thankful if you can record a video for it.

Regards
Onur

Re: TS-421 file change information mail

Posted: Thu Dec 11, 2014 2:34 am
by pwilson
oahiskali wrote:Thanks Patrick,

But I dont know how to use a script.I would be thankful if you can record a video for it.

Regards
Onur


Scripts work the same way under Windows, OS X, and Linux. I am happy to write scripts for anyone, but I'm not into Video production. I have provided a script that works, and that I have tested to ensure that it works. You have not educated what Operating System you are using on your PC, so I wouldn't know where to start when it comes to implementing this on your NAS.

Re: TS-421 file change information mail

Posted: Sun Mar 31, 2019 10:24 pm
by neoxhc
thanks for the script, appreciate this is a pretty old discussion but that would do what I need. I am editing the script but not an expert here, so I am running into ./watchdir.sh: line 13:  : command not found. Probably because I am not sure how to change it.
In other words, let's assume the folder I want to keep controlled for any file modification is Guido's home, would this be right:

#!/bin/sh
#
#  watchdir.sh [username]
#
# This script will watch the $HOME for [username] and send an e-mail if any files change within it.
# This script requires that the "Home Folders" feature is enabled.  See: http://docs.qnap.com/nas/4.1/Home/en/users.htm
#
if [ "$1x" = "Guido" ]; then
   echo "Usage: $0 Guido"
   echo " "
   echo "This script will watch the \$HOME for Guido"
else
   user=$1
   dir=$(grep $user /etc/config/passwd | cut -d: -f6)
fi
echo "done"

[ ! -d $dir ] && echo "$dir does not seem to exist"

SendTo="xxx@gmail.com"
SendFrom="watchdir.sh@xxx.myqnapcloud.com"
Subject="\$HOME ($dir) directory for $user changed."
#
#
rm -f /tmp/watchdir.email
touch /tmp/watchdir.email
echo "From: ${SendFrom}"     >>/tmp/watchdir.email
echo "To: ${SendTo}"         >>/tmp/watchdir.email
echo "Subject: ${Subject}"   >>/tmp/watchdir.email
echo " "                     >>/tmp/watchdir.email

while true #run indefinitely
do
inotifywait -r -e modify,attrib,close_write,move,create,delete $dir >>/tmp/watchdir.email \
&& cat /tmp/watchdir.email | sendmail -t
done
rm -f /tmp/watchdir.email
#done