Email notification about new file upload (Shared Folder)?

Discuss with the community any ideas you'd love to see in future DiskStations and DSM updates! We do our best to monitor and forward all of them, but we recommend to also use this form as our team will systematically see your suggestion:
https://www.synology.com/form/inquiry/feature
Forum rules
We do our best to monitor and forward your ideas to our team, but due to the large amount, we may not see every single one and recommend to also use this form as our team will systematically see your suggestion:
https://www.synology.com/form/inquiry/feature
Zaxynas
I'm New!
I'm New!
Posts: 5
Joined: Tue Jul 04, 2017 12:21 pm

Re: Email notification about new file upload (Shared Folder)?

Postby Zaxynas » Tue Jul 04, 2017 12:45 pm

Based on mzn script, I wrote this one.
It sends email to some defind users when defined folder has been modified (see USERS_MAIL_LIST parameter).
If the defined folder has not been modified, an email can be sent to the admin by setting SEND_UNCHANGED to 1.
Comparison lists can be recursive or not, depending on RECURSIVE_LIST parameter value.
During the first run, it creates lists for comparison and store them in '/homes/admin/_nasData_scripts' folder (see REP_DATA parameter)

Code: Select all

##### Sends email to users when defined folder has been modified.
##### If the defined folder has not been modified, an email can be sent to the admin by setting SEND_UNCHANGED to 1.
##### Comparison lists can be recursive or not, depending on RECURSIVE_LIST parameter value.
##### Comparison is only performed on file names, and not contents.
##### During the first run, creates lists for comparison and store them in '/homes/admin/_nasData_scripts' folder (see REP_DATA parameter)
##### TODO: send diffence list as attachment, as too large string sent in mail body can cause empty mail!
##### ------------------------------------ user's parameters
EXPLORE_FOLDER="/volume1/video/myMovies"
# Separate multiple users by comma in USERS_MAIL_LIST. Example: "user1@domain1.com,user2@domain2.com"
USERS_MAIL_LIST="user1@domain1.com,user2@domain2.com"
ADM_MAIL="mymail@domain.com"
FROM_MAIL="nasadm@domain.com"
RECURSIVE_LIST=1
SEND_UNCHANGED=1
##### ------------------------------------ advanced parameters
REP_DATA="/volume1/homes/admin/_nasData_scripts"
FOLDER_SHORT_NAME=$(echo $(basename "$EXPLORE_FOLDER"))
CURR_FILE="_"${FOLDER_SHORT_NAME//[,; :*+&#~\/\"\'\(\)\\]/}"_curr_list.txt"
PRIOR_FILE="_"${FOLDER_SHORT_NAME//[,; :*+&#~\/\"\'\(\)\\]/}"_prior_list.txt"
EMAIL_TITLE="NAS - Folder \'$FOLDER_SHORT_NAME\' analysis"
EMAIL_INTRO="[ This is an automatic email sent from the NAS to all users ]

"
##### ------------------------------------
USERS_MAIL_LIST="$ADM_MAIL,$USERS_MAIL_LIST"
RESULT_ALL=""
RESULT_ADM=""
# --------- collecte des paramètre pour retour admin
PARAMS="Parameters:
   EXPLORE_FOLDER: $EXPLORE_FOLDER
   FOLDER_SHORT_NAME: $FOLDER_SHORT_NAME
   REP_DATA: $REP_DATA
   CURR_FILE: $CURR_FILE
   PRIOR_FILE: $PRIOR_FILE
   RECURSIVE_LIST: $RECURSIVE_LIST
   SEND_UNCHANGED: $SEND_UNCHANGED"
# --------- creation REP_DATA pour fichiers CURR_FILE & PRIOR_FILE
mkdir -p "$REP_DATA"
if [ ! -d "$REP_DATA" ]; then
   /usr/bin/php -r "mail('$ADM_MAIL', '$EMAIL_TITLE: error', 'Error : unable to create $REP_DATA folder.', 'From: $FROM_MAIL');";
   exit
fi
if [ ! -f "$EXPLORE_FOLDER" ]; then
   RESULT_ADM="NAS folder \'$FOLDER_SHORT_NAME\' error.
Unable to locate folder \'$EXPLORE_FOLDER\'

$PARAMS"
   /usr/bin/php -r "mail('$ADM_MAIL', '$EMAIL_TITLE: error', '$RESULT_ADM', 'From: $FROM_MAIL');";
   exit
fi
# --------- écriture du contenu du dossier passé dans CURR_FILE
if [ "$RECURSIVE_LIST" != 0 ]; then
   ls --ignore-backups --ignore="*Thumbs.db*" --ignore="@eaDir" --ignore="*@SynoResource" --recursive "$EXPLORE_FOLDER"> "$REP_DATA/$CURR_FILE"
else
   ls --ignore-backups --ignore="*Thumbs.db*" --ignore="@eaDir" --ignore="*@SynoResource" --recursive "$EXPLORE_FOLDER"> "$REP_DATA/$CURR_FILE"
fi
# --------- comparaison PRIOR_FILE et CURR_FILE
if [ -f "$REP_DATA/$PRIOR_FILE" ]; then
   # --------- le fichier 'prior' existe : comparaison possible
   DIFF_LIST=$(diff --ignore-file-name-case --unchanged-group-format="" --old-group-format="
-------- %dn file%(n=1?:s) added:
%<" --new-group-format="
-------- %dN file%(N=1?:s) deleted:
%>" --exclude="*.db" "$REP_DATA/$CURR_FILE" "$REP_DATA/$PRIOR_FILE")
   if [ "$DIFF_LIST" != "" ]; then
      DATA_TO_SEND=${DIFF_LIST//\'/\\\'}
      DATA_TO_SEND=${DATA_TO_SEND//\"/\\\"}
      RESULT_ALL="NAS folder \'$FOLDER_SHORT_NAME\' has been modified:
-------------------------------------------------------
$DATA_TO_SEND"
   else
      if [ "$SEND_UNCHANGED" != 0 ]; then
         RESULT_ADM="NAS folder \'$FOLDER_SHORT_NAME\' is unchanged.

$PARAMS"
      fi
   fi
else
   # --------- alerte de création des fichiers
   RESULT_ADM="NAS folder \'$FOLDER_SHORT_NAME\' analysis:

File \'$REP_DATA/$CURR_FILE\' has been created.

$PARAMS"
fi
# ----------- copie de liste actuelle (CURR_FILE) pour comparaison future
cp "$REP_DATA/$CURR_FILE" "$REP_DATA/$PRIOR_FILE"
# ----------- envoi email a l'admin ou aux utilisateurs
if [ "$RESULT_ALL" != "" ]; then
   /usr/bin/php -r "mail('$USERS_MAIL_LIST', '$EMAIL_TITLE', '$EMAIL_INTRO $RESULT_ALL', 'From: $FROM_MAIL');";
else
   /usr/bin/php -r "mail('$ADM_MAIL', '$EMAIL_TITLE', '$RESULT_ADM', 'From: $FROM_MAIL');";
fi


This script has to be enter under Task Settings.
I'm a total newbie in bash things, so this script can for sure be simplified or improved. It wotks for me with DSM 6.x, but please don't ask me if it doesn't work for you, because I certainly don't have the answer! I only made this script (with copy/paste pieces of code) for my personal use.
phreaky
I'm New!
I'm New!
Posts: 3
Joined: Mon Feb 03, 2014 8:52 pm

Re: Email notification about new file upload (Shared Folder)?

Postby phreaky » Wed Jul 12, 2017 9:01 am

tried all variantions of the script provided here I do see 2 files created on the folder i'm scanning with info about the folder but none of them seem to send an email...

any idea why this is ?

when i try the version of Zaxynas i get the error "Unable to locate folder"
Zaxynas
I'm New!
I'm New!
Posts: 5
Joined: Tue Jul 04, 2017 12:21 pm

Re: Email notification about new file upload (Shared Folder)?

Postby Zaxynas » Wed Jul 12, 2017 10:37 am

phreaky wrote:when i try the version of Zaxynas i get the error "Unable to locate folder"

Be sure to define a valid NAS folder with the following parameter. For instance:

Code: Select all

EXPLORE_FOLDER="/volume1/video"
eronde
I'm New!
I'm New!
Posts: 2
Joined: Fri Oct 16, 2015 3:09 pm

Re: Email notification about new file upload (Shared Folder)?

Postby eronde » Sat Sep 02, 2017 1:03 pm

Thanks for your script.

The scripts might work better with the following modification:

Code: Select all

 if [ ! -d "$EXPLORE_FOLDER" ]; then instead of if [ ! -f "$EXPLORE_FOLDER" ]; then
gregre
I'm New!
I'm New!
Posts: 1
Joined: Mon Oct 30, 2017 6:29 pm

Re: Email notification about new file upload (Shared Folder)?

Postby gregre » Mon Oct 30, 2017 6:37 pm

How to check if there are new files for an external FTP server?

I tried it in place EXPLORE_FOLDER=
enter ftp://user:password@host
but it does not work.

What am I doing wrong?
maravac
Rookie
Rookie
Posts: 34
Joined: Tue Dec 20, 2011 1:28 pm

Re: Email notification about new file upload (Shared Folder)?

Postby maravac » Tue Oct 31, 2017 11:46 am

Hi, I also have the problem using this script. It seems that it is running correctly, but (maybe my) problem is if I want monitor some folder using some non-standard ASCII character in its name (typically characters like "á", "é", "ž"). Is it possible to cover somehow this issue, f.e. defining the UTF-8 as the default charset, so the script (or system?) will use it?

EDIT:

well, it seems that non-ASCII characters is probably not a problem. I tested the script with another folder and I get this error (..... means rest of the path to my folder "testik"):

NAS folder 'testik' error.
Unable to locate folder '/volume1/homes/...../testik'

Parameters:
EXPLORE_FOLDER: /volume1/homes/...../testik
FOLDER_SHORT_NAME: testik
REP_DATA: /volume1/homes/admin/_nasData_scripts
CURR_FILE: _testik_curr_list.txt
PRIOR_FILE: _testik_prior_list.txt
RECURSIVE_LIST: 1
SEND_UNCHANGED: 0


In DSM I use my own user account with admin privileges, but this should not be a problem, right? Should I change some more parameters or values in the script besides the email addres and the folder path?
Zaxynas
I'm New!
I'm New!
Posts: 5
Joined: Tue Jul 04, 2017 12:21 pm

Re: Email notification about new file upload (Shared Folder)?

Postby Zaxynas » Wed Nov 01, 2017 10:14 am

Hello,

As I said:
I'm a total newbie in bash things, so this script can for sure be simplified or improved. It wotks for me with DSM 6.x, but please don't ask me if it doesn't work for you, because I certainly don't have the answer! I only made this script (with copy/paste pieces of code) for my personal use.


However:
- don't forget to quote the EXPLORE_FOLDER (should be "/volume1/homes/...../testik")
- try to run the scripts with standard built-in folders, like:
"/volume1/video"
"/volume1/music"
If it works, try to create your own folder and then run again the script.

For a simple use, only these parameters have to be modified:
EXPLORE_FOLDER
USERS_MAIL_LIST
ADM_MAIL
FROM_MAIL
dadaniel
I'm New!
I'm New!
Posts: 2
Joined: Wed Feb 08, 2017 11:59 am

Re: Email notification about new file upload (Shared Folder)?

Postby dadaniel » Wed Nov 08, 2017 9:43 pm

replace the last lines of the script with:

Code: Select all

if [ "$RESULT_ADM" != "" ]; then
   /usr/bin/php -r "mail('$ADM_MAIL', '$EMAIL_TITLE', '$RESULT_ADM', 'From: $FROM_MAIL');";
fi
fi


This solves the issue that everytime the script runs an empty mail is sent to the admin address when nothing is changed.
Zaxynas
I'm New!
I'm New!
Posts: 5
Joined: Tue Jul 04, 2017 12:21 pm

Re: Email notification about new file upload (Shared Folder)?

Postby Zaxynas » Tue Nov 21, 2017 9:28 am

dadaniel wrote:replace the last lines of the script with:

Code: Select all

if [ "$RESULT_ADM" != "" ]; then
   /usr/bin/php -r "mail('$ADM_MAIL', '$EMAIL_TITLE', '$RESULT_ADM', 'From: $FROM_MAIL');";
fi
fi


This solves the issue that everytime the script runs an empty mail is sent to the admin address when nothing is changed.

Or set SEND_UNCHANGED parameter to 0
;)
flo74700
I'm New!
I'm New!
Posts: 1
Joined: Tue Nov 21, 2017 2:52 pm

Re: Email notification about new file upload (Shared Folder)?

Postby flo74700 » Tue Nov 21, 2017 3:11 pm

Hello everybody,
i'm new user for this script.
I run this script everyday for compare the folder state with the day-1 state and send e-mail if an file was upload but i have a problem
Everyday, thumbfile was create on this folder and generate new mail.

Do you have an idea for remove this thumbfile?
Name of this files is: Synofile_THUMB_M.jpg or Synofile_Thumb_XL.jpg.

Do you know if it's possible to exclude this check?

Thanks in advance for you help and thanks you for this script.
clicpomme
I'm New!
I'm New!
Posts: 6
Joined: Thu Jun 12, 2014 1:50 am

Re: Email notification about new file upload (Shared Folder)?

Postby clicpomme » Mon Nov 27, 2017 9:38 pm

Hi,

I'm not able to make this script work for me, always got this error message:

AS folder 'test' error.
Unable to locate folder '/volume3/test'

Parameters:
EXPLORE_FOLDER: /volume3/test
FOLDER_SHORT_NAME: test
REP_DATA: /volume1/homes/admin/_nasData_scripts
CURR_FILE: _test_curr_list.txt
PRIOR_FILE: _test_prior_list.txt
RECURSIVE_LIST: 1
SEND_UNCHANGED: 1


Here are a copy of my scripts settings:

##### ------------------------------------ user's parameters
EXPLORE_FOLDER="/volume3/test"
# Separate multiple users by comma in USERS_MAIL_LIST. Example: "user1@domain1.com,user2@domain2.com"
USERS_MAIL_LIST="email@domain.com"
ADM_MAIL="email@domain.com"
FROM_MAIL="email@domain.com"
RECURSIVE_LIST=1
SEND_UNCHANGED=1
##### ------------------------------------ advanced parameters
REP_DATA="/volume1/homes/admin/_nasData_scripts"
FOLDER_SHORT_NAME=$(echo $(basename "$EXPLORE_FOLDER"))
CURR_FILE="_"${FOLDER_SHORT_NAME//[,; :*+&#~\/\"\'\(\)\\]/}"_curr_list.txt"
PRIOR_FILE="_"${FOLDER_SHORT_NAME//[,; :*+&#~\/\"\'\(\)\\]/}"_prior_list.txt"
EMAIL_TITLE="NAS - Folder \'$FOLDER_SHORT_NAME\' analysis"
EMAIL_INTRO="[ This is an automatic email sent from the NAS to all users ]

"
##### ------------------------------------

In ssh a can access the folder without any issue

admin@DiskStation:/volume3/test$ pwd
/volume3/test

I'm running the script as root

Any idea why this is not working for me ?

Thanks to all !!
clicpomme
I'm New!
I'm New!
Posts: 6
Joined: Thu Jun 12, 2014 1:50 am

Re: Email notification about new file upload (Shared Folder)?

Postby clicpomme » Tue Nov 28, 2017 4:36 pm

Looks like changing this line as fix the issue:

Code: Select all

 if [ ! -d "$EXPLORE_FOLDER" ]; then instead of if [ ! -f "$EXPLORE_FOLDER" ]; then


Great job thanks for that script !

have also remove the email send to email when nothing have change ... don't want to know rhat nothingas change


Code: Select all

# ----------- copie de liste actuelle (CURR_FILE) pour comparaison future
cp "$REP_DATA/$CURR_FILE" "$REP_DATA/$PRIOR_FILE"
# ----------- envoi email a l'admin ou aux utilisateurs
if [ "$RESULT_ALL" != "" ]; then
   /usr/bin/php -r "mail('$USERS_MAIL_LIST', '$EMAIL_TITLE', '$EMAIL_INTRO $RESULT_ALL', 'From: $FROM_MAIL');";
else
   #/usr/bin/php -r "mail('$ADM_MAIL', '$EMAIL_TITLE', '$RESULT_ADM', 'From: $FROM_MAIL');";
exit
fi
Zaxynas
I'm New!
I'm New!
Posts: 5
Joined: Tue Jul 04, 2017 12:21 pm

Re: Email notification about new file upload (Shared Folder)?

Postby Zaxynas » Wed Nov 29, 2017 9:13 am

Thanks for the modification.

clicpomme wrote:have also remove the email send to email when nothing have change ... don't want to know rhat nothingas change

As I previously wrote, just set SEND_UNCHANGED parameter to 0 if you don't want to receive an email when nothing has changed to the scanned folder.
maravac
Rookie
Rookie
Posts: 34
Joined: Tue Dec 20, 2011 1:28 pm

Re: Email notification about new file upload (Shared Folder)?

Postby maravac » Wed Nov 29, 2017 12:01 pm

Hi, that script now works for me as well! Thanks!

But does know anyone how to optimize it in terms of charset used? If I have any file that uses other that standard ASCII chars in its filename, then they are changed/damaged in the received email.
And the same problem is also if folder name contains non-ASCII characters.

Is it possible somehow to force the script to use let's say UTF-8, or finetune some DSM setting to get it work correctly?

Return to “Feature Requests & Product Improvement Suggestions”

Who is online

Users browsing this forum: No registered users and 2 guests