#!/bin/bash
# XGE 2005-09-05 Script to verify the internal state of our subversion
# repositories nightly. This script should be called from a crontab and
# any errors sent by crontab to interested parties. Redirect STDOUT to
# /dev/null if you don't want to be emailed every night with this script
# telling you that it ran successfully! :)

# The inspiration to create this script came from a post by John Szakmeister
# to users@subversion.tigris.org:-
# "One piece of advice I tell everyone when they setup their repository, is to
#  at least run 'svnadmin verify' over it every night.  That will ensure
#  things like this don't go unnoticed for a long period of time."

REPOS_BASE=/home/svnrepos

SVNADMIN=/usr/bin/svnadmin
FIND=/usr/bin/find
EXPR=/usr/bin/expr
PYTHON=/usr/bin/python

# The svnadmin verify command (in SVN 1.2.1 at least) uses the format
# subdirectory to tell if a directory is a repository. E.g.:
#   [root@lisa svn-repositories]# svnadmin verify websites
#   svnadmin: Can't open file 'websites/format': No such file or directory
# I will use the same technique to work out which directories to run
# svnadmin verify on nightly so that new repositories do not have to be
# added to this script.

for MATCH in `$FIND "$REPOS_BASE" -name format -type f -not -path "*/db/*"`
do
  # match now contains something like './repos_dir/format'
  # I want just the ./repos_dir/ part so I remove the tail.
  REPOS_DIR=`$EXPR match "$MATCH" '\(.*\)/format'`
  if [ $? -ne 0 ]; then
    echo "Failed to determine repository from path $MATCH!" >&2
  elif [[ ! -e "$REPOS_DIR" || ! -d "$REPOS_DIR" ]]; then
    echo "$REPOS_DIR is not a valid repository!" >&2
  else
    echo -n "Verifying repository $REPOS_DIR ... "
    if $SVNADMIN verify "$REPOS_DIR" 2>/dev/null ;then
        echo " -> SUCCESS: all is okay!"
    else
        logger -p user.err "[$0] : Error while verifying repository '$REPOS_DIR'. Check manually!"
        echo " -> ERROR!" >&2
    fi
  fi
done

