Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Forked from renekreijveld/jnewfiles
Created April 27, 2019 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save molotovbliss/ae36546bfa80f2eb9f58e70a0ddc6344 to your computer and use it in GitHub Desktop.
Save molotovbliss/ae36546bfa80f2eb9f58e70a0ddc6344 to your computer and use it in GitHub Desktop.
Bash script to detect new and changed php and html files last 3 hours. Skips cache directories.
#!/bin/sh
# jnewfiles
# Detects new and changed php and html files last xxx minutes
# (C) 2014 Rene Kreijveld, enail [at] renekreijveld [dot] nl
# Update 31-12-2013: only send email when changes are found
# Update 04-02-2014: check for new files last three hours
# Update 05-05-2014: check for new html files also
# Update 07-05-2014: filter out ju_chached and DirectAdmin stats folders in html files
# Update 04-06-2014: added duration, subject_prefix, script_fullname and sendnotfound variables.
# added option to send email of nothing found
# All these improvements by Christophe Avonture
# Update 21-10-2014: code rewrite and added auto update feature
# Update 24-10-2014: added -n (no e-mail) option
# Update 29-10-2014: correction in the instructions
# Update 26-11-2014: changed test from -mmin to -cmin, added check for change in .htaccess files.
# Update 26-11-2014: added List generated by message at end of list
# Save this file as /usr/local/sbin/newfiles
# Modify the homedir and tmpdir variables to your needs.
#
# Then add to your local cron to run every 2 hours:
# 5 0,2,4,6,8,10,12,14,16,18,20,22 * * * /usr/local/sbin/jnewfiles -n you@yourdomain.com
# Replace you@yourdomain.com with your own email address
# To run cron every hour:
# 5 * * * * /usr/local/sbin/jnewfiles -n you@yourdomain.com
# Setup variables
myname=$(basename ${0})
version=3.8
server="$(hostname)"
tmpdir=/tmp
homedir=/home
emailmsg="${tmpdir}/new_changed.txt"
changes="${tmpdir}/new_changes.txt"
# 60 minutes -> find any files that have been changed during the last hour
duration=60
# Extra subject prefix for the email message
subject_prefix="Joomla file monitoring agent - server ${server} - `date` "
# Retrieve the name of this script
script_fullname=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)/$(basename "${BASH_SOURCE[0]}")
# Send email if no new files were found? Enter value "yes" or "no"
sendnotfound=no
# Find mail client
if [ -s /bin/mail ]
then
mailprog=/bin/mail
elif [ -s /usr/bin/mail ]
then
mailprog=/usr/bin/mail
fi
# display usage information
usage() {
echo ""
echo "${myname} version ${version}, written by Rene Kreijveld."
echo ""
echo "Usage: ${myname} [-n <email>] [-h]"
echo ""
echo "-n Send notification to <email> (comma-separated list of email addresses, without spaces)"
echo "-h Help. Display this info."
echo ""
exit 0
}
# process the arguments
sendmail="yes"
while getopts n:h opt
do
case "${opt}" in
n)
notify="yes"
receivers=${OPTARG}
;;
h) usage;;
\?) usage;;
esac
done
# Find all changed .php files last <duration> minutes, skip files with "cache" and "php-mail" in filename
find ${homedir} -type f -cmin -${duration} -name "*.php" -exec ls -l {} \; | grep -v cache | grep -v php-mail | grep -v "error.php" > ${changes}
# Find all changed .html files last <duration> minutes
find ${homedir} -type f -cmin -${duration} -name "*.html" -exec ls -l {} \; | grep -v cache | grep -v ju_cached | grep -v "/stats/" >> ${changes}
# Find all changed .js files last <duration> minutes
find ${homedir} -type f -cmin -${duration} -name "*.js" -exec ls -l {} \; | grep -v cache >> ${changes}
# Find all changed .htaccess files last <duration> minutes
find ${homedir} -type f -cmin -${duration} -name ".htaccess" -exec ls -l {} \; >> ${changes}
# Find all changed .php files last <duration> minutes in the temp directory
find ${tmpdir} -type f -cmin -${duration} -name "*.php" -exec ls -l {} \; >> ${changes}
# Only if changes are found, send email
if [ -s ${changes} ]
then
echo "${subject_prefix}" > ${emailmsg}
echo "New and changed files last ${duration} minuntes server ${server}" >> ${emailmsg}
echo "================================================================================" >> ${emailmsg}
cat ${changes} >> ${emailmsg}
echo "================================================================================" >> ${emailmsg}
echo "List generated by ${script_fullname}" >> ${emailmsg}
if [ "${notify}" == "yes" ]
then
${mailprog} -s "New/changed files ${server}" "${receivers}" < ${emailmsg}
else
cat ${emailmsg}
fi
else
if [ ${sendnotfound} == "yes" ]
then
echo "${subject_prefix} No changes found during the last ${duration} minutes" > ${emailmsg}
echo "List generated by ${script_fullname}" >> ${emailmsg}
if [ "${notify}" == "yes" ]
then
${mailprog} -s "${subject_prefix} - No changes found" "${receivers}" < ${emailmsg}
else
cat ${emailmsg}
fi
fi
fi
# Cleanup temporary files
rm -f ${emailmsg} ${changes}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment