Automatic versioning upon file change (modify/create/delete)

1. General purpose method using bazaar & inotify

This is untested by me but I found this write up that makes use of bzr (bazaar) & inotifywait to monitor a directory and version control the files in it using bazaar.

This script does all the work of watching the directory for changes:

#!/bin/bash

# go to checkout repository folder you want to watch
cd path/to/www/parent/www
# start watching the directory for changes recusively, ignoring .bzr dir
# comment is made out of dir/filename
# no output is shown from this, but wrinting a filename instead of /dev/null 
# would allow logging
inotifywait –exclude \.bzr -r -q -m -e CLOSE_WRITE \
    –format=”bzr commit -m ‘autocommit for %w/%f’” ./ | \
    sh  2>/dev/null 1>&2 &
# disown the pid, so the inotify thread will get free from parent process
# and will not be terminated with it
PID=`ps aux | grep inotify | grep CLOSE_WRITE | grep -v grep | awk ‘{print $2}’`
disown $PID

# this is for new files, not modifications, optional
inotifywait –exclude \.bzr -r -q -m -e CREATE \
    –format=”bzr add *; bzr commit -m ‘new file added %w/%f’” ./ | \
    sh  2>/dev/null 1>&2 &
PID=`ps aux | grep inotify | grep CREATE | grep -v grep | awk ‘{print $2}’`
disown $PID

exit 0;

2. Managing /etc

For the special case of managing your system's /etc directory, you can use the app etckeeper.

etckeeper is a collection of tools to let /etc be stored in a git, mercurial, darcs, or bzr repository. It hooks into apt (and other package managers including yum and pacman-g2) to automatically commit changes made to /etc during package upgrades. It tracks file metadata that revison control systems do not normally support, but that is important for /etc, such as the permissions of /etc/shadow. It's quite modular and configurable, while also being simple to use if you understand the basics of working with revision control.

Here's a good tutorial to get you started with it.

3. Using git and incron

This technique makes use of git and incron. For this method you need to do the following:

A. Make a repo

% mkdir $HOME/git
% cd $HOME/git
% git init

B. Create a $HOME/bin/git-autocommit script

#!/bin/bash

REP_DIR="$HOME/git"       # repository directory
NOTIFY_DIR="$HOME/srv"    # directory to version

cd $REP_DIR
GIT_WORK_TREE=$NOTIFY_DIR /usr/bin/git add .
GIT_WORK_TREE=$NOTIFY_DIR /usr/bin/git commit -a -m "auto"

C. Add an entry to incrontab

% sudo incrontab -e $HOME/srv IN_MODIFY,IN_CREATE,IN_MOVED_FROM,IN_MOVED_TO $HOME/bin/git-autocommit

4. Using Flashbake

Another option is to use a tool like Flashbake. Flashbake is the version control system that Cory Doctorow (of BoingBoing fame) uses to write his books.

Flashbake uses git under the hood to track changes but is somewhere between doing automated backups and using a plain version control system yourself.

Cory wanted the version to carry prompts, snapshots of where he was at the time an automated commit occurred and what he was thinking. I quickly sketched out a Python script to pull the contextual information he wanted and started hacking together a shell script to drive git, using the Python script’s output for the commit comment when a cron job invoked the shell wrapper.

Resources

  • Automatic file revision on upload

Immediatly ZFS comes to mind. It can create snapshots - and there are some projects to automatically create snapshots.


I think you're on the right track with inotify. This article details its basic usage in a case similar to yours. I'd suggest using it either directly, or compiling a kernel-level utility like fschange. This is something of a hassle, but you could then bind the detection of changes to a git commit or similar.

Those solutions both have the issue of relying on somewhat imperfect third-party solutions. If you don't mind getting your hands dirty, NodeJS provides an excellent, cross-platform facility (fs.watch) for this exact purpose. A basic tutorial on watching files for changes in NodeJS can be found here. In a few dozen lines or less, you could write something that watches a directory for files, and then shells out (via child_process) and runs a git commit or similar (or even manually increments a version file index, if you like the roll-your-own approach).

fs.watch is backed by inotify on linux, but is a lot more intuitive to use. There are other NodeJS projects that wrap that file-watching functionality in various levels of convenience, like this one or this one.