This is a short script which allows an automatic update of a subversion repository on Linux :

Create a log-directory into your Subversion repository

$mkdir logs

and save the following script into the repository as svn_cron.sh


#!/bin/bash
#
# Variable REPOS is the path to the repository on the server

REPOS=.

echo ""
date +"%B %d %Y"

test $# = 0 || ( echo "Please enter the path to the local copy"; exit 1 )

cd $1 2>&1 >/dev/null || ( echo "$1 does not exist"; exit 2 )

/usr/local/bin/svn info 2>&1 >/dev/null|| ( echo "$1 is not a working copy"; exit 3 )

if [ `/usr/local/bin/svn update $1 | sed -n "/^C/p" | wc -l` -ne 0 ]
then

# Collects all files and directorys which are to add on the repository
/usr/local/bin/svn status | sed -n "/^\??*/p" | sed "s/^\? *//g" > $REPOS/logs/toadd

# Collects all files and directorys which are to delete on the repository
/usr/local/bin/svn status | sed -n "/^\!?*/p" | sed "s/^\! *//g" > $REPOS/logs/todelete*//g" > $REPOS/logs/todelete

# Add and delete Subversion actions
cat $REPOS/logs/todelete | while read line; do /usr/local/bin/svn delete "${line}"; done
cat $REPOS/logs/toadd | while read line; do /usr/local/bin/svn add "${line}"; done

# SVN commit in order to save the changes with a log message
if !(/usr/local/bin/svn commit -m "Daily commit for changes done directly on the server")
then echo "A problem has occured during the commit"
exit 3
fi


else
echo "Problem with the daily update, there may be a conflict within the following files \! "

# Displays the files which are currently in conflict
svn status $1
exit 4
fi
exit 0

Then edit the crontab

$crontab -e

or if you want the uploads to be done by another user

$crontab -e -u username

Of course, the user must have enough rights to write on the logs-file

when using vi, put the following lines at the end of te file

##Automatic update of the Subversion repository every day at 0:00
0 0 * * * /path_to_your_repository/svn_cron.sh /path_to_your_local_copy >> /path_to_your_repository/logs/script_cron


Leave a Reply