inotify – A lazy man´s git system

Sharing my inotify script that takes care of backing up puppet configuration when changes are done. Works well if you don´t know how or have the need or diciplin to use git. Can of course be used for something other than puppet configs 😉

Search for other versions of a file with something like:

find /etc/puppet-backups -wholename "*/etc/puppet/modules/banner/templates/motd.erb"

With the found matches you can also do a “diff” and compare the different versions.

Also suggest creating the directory /etc/puppet-backups/thisyear/thismonth/today/initial/etc/puppet and copying your modules and environments directory there, this way you will have a base backup and a base to use when diffing.

#!/bin/bash
 
# inotifywait: Start/Stop inotifywait
#
# chkconfig: - 80 20
# description: inotifywait waits for changes to files using inotify.
#
# processname: inotifywait
 
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
 
LOGFILE=/var/log/inotify.log
LOCK=/var/lock/subsys/inotifywait
 
RETVAL=0
start() {
   echo -n $"Starting inotifywait: "
 
   (while true; do
    # For some reason, VIM creates a file called 4913 which messes things up for inotify
    dirfile=$(inotifywait -r -e create,modify --exclude '(.*\.sw[pox].*|4913)' --format '%w %f' /etc/puppet/modules /etc/puppet/environments)
    dir=$(echo $dirfile | awk '{ print $1 }')
    file=$(echo $dirfile | awk '{ print $2 }')
    year=$(date +%Y)
    month=$(date +%m)
    day=$(date +%d)
    time=$(date +%H%M%S)
    backupbase=/etc/puppet-backups/$year/$month/$day/$time
    mkdir -p $backupbase
    backupdir=${backupbase}${dir}
    mkdir -p $backupdir
    echo "${year}-${month}-${day} ${time} - Backing up new file version of file ${dir}${file} to directory $backupdir" >>$LOGFILE
    cp ${dir}${file} ${backupdir}/ 1>>$LOGFILE 2>&1
   done) &
 
   RETVAL=$?
   echo
   [ $RETVAL -eq 0 ] && touch $LOCK
   return $RETVAL
}
 
stop() {
   echo -n $"Stopping inotifywait: "
   killproc inotifywait
   RETVAL=$?
   echo
   [ $RETVAL -eq 0 ] && rm -f $LOCK
   return $RETVAL
}
 
case "$1" in
   start)
      start
      ;;
   stop)
      stop
      ;;
   status)
      status inotifywait
      ;;
   restart)
      stop
      start
      ;;
   *)
      echo $"Usage: $0 {start|stop|status|restart}"
      exit 1
esac
exit $?

Leave a Reply

Your email address will not be published. Required fields are marked *