TS-421 file change information mail

Post your questions about myQNAPcloud service here.
Post Reply
oahiskali
New here
Posts: 3
Joined: Fri Nov 28, 2014 8:46 pm

TS-421 file change information mail

Post 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.
User avatar
pwilson
Guru
Posts: 22533
Joined: Fri Mar 06, 2009 11:20 am
Location: Victoria, BC, Canada (UTC-08:00)

Re: TS-421 file change information mail

Post 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.

Patrick M. Wilson
Victoria, BC Canada
QNAP TS-470 Pro w/ 4 * Western Digital WD30EFRX WD Reds (RAID5) - - Single 8.1TB Storage Pool FW: QTS 4.2.0 Build 20151023 - Kali Linux v1.06 (64bit)
Forums: View My Profile - Search My Posts - View My Photo - View My Location - Top Community Posters
QNAP: Turbo NAS User Manual - QNAP Wiki - QNAP Tutorials - QNAP FAQs

Please review: When you're asking a question, please include the following.
oahiskali
New here
Posts: 3
Joined: Fri Nov 28, 2014 8:46 pm

Re: TS-421 file change information mail

Post 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
User avatar
pwilson
Guru
Posts: 22533
Joined: Fri Mar 06, 2009 11:20 am
Location: Victoria, BC, Canada (UTC-08:00)

Re: TS-421 file change information mail

Post 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.

Patrick M. Wilson
Victoria, BC Canada
QNAP TS-470 Pro w/ 4 * Western Digital WD30EFRX WD Reds (RAID5) - - Single 8.1TB Storage Pool FW: QTS 4.2.0 Build 20151023 - Kali Linux v1.06 (64bit)
Forums: View My Profile - Search My Posts - View My Photo - View My Location - Top Community Posters
QNAP: Turbo NAS User Manual - QNAP Wiki - QNAP Tutorials - QNAP FAQs

Please review: When you're asking a question, please include the following.
neoxhc
First post
Posts: 1
Joined: Sun Mar 31, 2019 10:19 pm

Re: TS-421 file change information mail

Post 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
Post Reply

Return to “myQNAPcloud service”