Skip to content

Instantly share code, notes, and snippets.

@drywall
Last active August 29, 2015 14:03
Show Gist options
  • Save drywall/0ddc21660d5eca9cfc1d to your computer and use it in GitHub Desktop.
Save drywall/0ddc21660d5eca9cfc1d to your computer and use it in GitHub Desktop.
Watch what's in git
#/bin/bash
# /home/mazonorg/public_html/cshp_bin/git_monitor.sh
#
# Purpose:
# Monitor mazon.org public HTML directory structure for local changes.
# This will alert Cornershop Creative of any potential hacked files.
#
# Usage:
# Install to the server's crontab with the following command, as root.
# ln -s /home/mazonorg/public_html/cshp_bin/git_monitor.sh /etc/cron.daily/git_monitor.sh
#
# Configuration
# Feel free to modify these values
DIRS=("/home/matalb13/non-live-site-youthpromiseaction.org")
DIRS+=("/home/matalb13/peacealliance.org")
DIRS+=("/home/matalb13/studentpeacealliance.org")
DIRS+=("/home/matalb13/youthpromiseaction.org")
FROM="[email protected]"
TO="[email protected]"
#
# Preparation
# DO NOT MODIFY
# GIT_DIR: Git path settings, for cron execution
for DIR in "${DIRS[@]}"
do
GIT_DIR="--git-dir=${DIR}/.git --work-tree=${DIR}"
# GIT_LS: Determine if any files are modified or new in the working area. --error_unmatch requires that the pathspec be specified, so provide $DIR.
GIT_LS="${GIT_DIR} ls-files --full-name --modified --other --error-unmatch --exclude-standard ${DIR}"
# GIT_STATUS: Status file output for email notification
GIT_STATUS="${GIT_DIR} status --short"
#
# Monitor and notify
#
# git ls-files structure from
# http://stackoverflow.com/questions/2657935/checking-for-a-dirty-index-or-untracked-files-with-git
#
git ${GIT_LS} >/dev/null 2>&1
ec=$?
if test "$ec" = 0; then
# Dirty local working area
# Send notification email
echo -e "Working area unclean. \n$(hostname):${DIR} contains local modifications. \n----------\n$(git ${GIT_STATUS})" | /usr/bin/mail -s "ATTN: $(hostname):${DIR} contains local modifications" -r ${FROM} ${TO} >/tmp/git_monitor.out 2>&1
elif test "$ec" = 1; then
# Do nothing. Local working area is clean.
# echo "${DIR} clean. Nothing to do."
echo >/dev/null 2>&1
else
# Git command failed
# Send notification email
echo -e "Working area could not be monitored. \n$(hostname):${DIR} could not be monitored by user $(whoami), due to git ls-dir error code ${ec}. \n----------\n$(git ${GIT_LS})" | /usr/bin/mail -s "ATTN: $(hostname):${DIR} monitor failed" -r ${FROM} ${TO} >/tmp/git_monitor.out 2>&1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment