Install and Setup Subversion Server on Ubuntu 12.10 Server with Multiple Repositories

Install and Setup Subversion Server on Ubuntu 12.10 Server with Multiple Repositories
By Shay Anderson on January 2013
In this article I explain how to install and setup subversion on a Ubuntu server (in this example Ubuntu 12.10 server).

Install Subversion
First, install subversion: >> apt-get install subversionOr, you can check what version of subversion is already installed: >> svn –version
svn, version 1.7.5 (r1336830)
compiled Sep 28 2012, 11:22:04
[...]
Setup Subversion Repositories
Next, I want to setup a location for the subversion repositories: >> mkdir /home/reposNow this is where I can create subversion repositories. I am going to create two example repositories to use for this example: >> svnadmin create /home/repos/project1>> svnadmin create /home/repos/project2Next, I want to setup the correct permissions for the repositories: >> chmod -R g+rws /home/reposNow I can setup a group that will use the subversion repositories: >> sudo groupadd svnNext I can change the group ownership of the repository directories: >> chgrp -R svn /home/reposI need to add my user to the ‘svn’ group, to do this I do (replace [username] with the correct user): >> usermod -a -G svn [username]Then I can check if my user has been added to the group: >> groups
adm cdrom sudo dip plugdev sambashare lpadmin svn
Test New Repositories
The next step is to test the new repositories that have be setup: >> mkdir /home/tmp
>> svn checkout file:///home/repos/project1 /home/tmp
Checked out revision 0.Now I create a test file that can be used to commit to the new repository: >> echo ‘this is test’ > /home/tmp/test.txtAdd the file: >> svn add /home/tmp/test.txt
A tmp/test.txtTest commit the file: >> svn commit -m “initial test commit” /home/tmp
Adding tmp/test.txt
Transmitting file data .
Committed revision 1.Success!

Remote Access Subversion Repositories Using SVN Protocol
This section is the most important section where we setup remote accessing to our new subversion repositories using the svn protocol. We can to allow access to all repositories, so instead of configuring each separate repository for access, we can setup a central configuration so all repositories can be accessed. First, let’s setup a user/password file for access our repositories, create a new file ‘passwd-users’ using your text editor: >> nano /home/repos/passwd-usersAnd a users and passwords: [users]
shay = mypass
user2 = secret
mike = goodpasswordSave file and exit. Add correct permissions to protect users/passwords file: >> sudo chmod 600 /home/repos/passwd-users
Now we need to edit the ‘conf/svnserve.conf’ files for each repository: >> nano /home/repos/project1/conf/svnserve.confNow replace all the content in the file with: [general]
anon-access = none
auth-access = write
password-db = /home/repos/passwd-users
realm = Development TeamThis needs to be done for all repositories that you want to allow remote access for.

Now I can start the subversion server ‘svnserve’: >> sudo svnserve -d -r /home/reposThis will allow access to our repositories located in ‘/home/repos’.

Test the svn protocol checkout: >> mkdir /home/tmp2
>> svn checkout svn://[server IP address]/project1 –username [username]This should checkout the ‘project1′ repository files.

Note: While the above ‘svnserve.conf’ setup will work, it might be easier for you to use a global/common ‘svnserve.conf’ file. You can accomplish a global/comman ‘svnserve.conf’ file by using symlinks. First, create a copy of the ‘svnserve.conf’ file you created for the ‘project1′ repository in ‘/home/repos’: >> cp -v /home/repos/project1/conf/svnserve.conf /home/reposThen move both ‘svnserve.conf’ files for repositories ‘project1′ and ‘project2′ to backup files: >> mv -v /home/repos/project1/conf/svnserve.conf /home/repos/project1/conf/svnserve.conf.BAK>> mv -v /home/repos/project2/conf/svnserve.conf /home/repos/project2/conf/svnserve.conf.BAKThen create symlinks to global/common ‘svnserve.conf’ file for both repositories: >> ln -s /home/repos/svnserve.conf /home/repos/project1/conf/svnserve.conf>> ln -s /home/repos/svnserve.conf /home/repos/project2/conf/svnserve.confFinally, restart the subversion server: >> sudo killall svnserve
>> sudo svnserve -d -r /home/reposNow you have an easy-to-use ‘svnserve.conf’ file.

Setup Script for Subversion Server Auto-Startup
Another important part of setting up a subversion server is forcing the subversion server service to auto start when the server boots, as well as giving us a convenient way to start/stop/restart the subversion server service. To do this create the file: >> nano /etc/init.d/svnserveThen add a simple script like: #! /bin/sh
#
# svnserve {start|stop|restart|force-reload} script
#
# @author Shay Anderson 01.13
#

# set repositories path
REPOS=/home/repos

# set svnserve daemon
DAEMON=/usr/bin/svnserve

# configuration settings
PATH=/sbin:/bin:/usr/sbin:/usr/bin
SVN=svnserve
SVN_DESC=”Subversion Server Daemon ($SVN)”

case “$1″ in
start)
echo “Starting $SVN_DESC…”
start-stop-daemon –start –quiet –oknodo –exec $DAEMON — -d -r $REPOS >/dev/null 2>&1
echo
;;
stop)
echo “Stopping $SVN_DESC…”
start-stop-daemon –stop –quiet –oknodo –exec $DAEMON
echo
;;
restart|force-reload)
$0 stop
sleep 1
$0 start
;;
*)
echo “Usage: $0 {start|stop|restart|force-reload}”
echo
exit 1
;;
esac

exit 0Make the script executable: >> chmod +x /etc/init.d/svnserveRegister the script with the Ubuntu OS: >> update-rc.d svnserve defaults
update-rc.d: warning: /etc/init.d/svnserve missing LSB information
update-rc.d: see
Adding system startup for /etc/init.d/svnserve …
/etc/rc0.d/K20svnserve -> ../init.d/svnserve
/etc/rc1.d/K20svnserve -> ../init.d/svnserve
/etc/rc6.d/K20svnserve -> ../init.d/svnserve
/etc/rc2.d/S20svnserve -> ../init.d/svnserve
/etc/rc3.d/S20svnserve -> ../init.d/svnserve
/etc/rc4.d/S20svnserve -> ../init.d/svnserve
/etc/rc5.d/S20svnserve -> ../init.d/svnserveNow the subversion server daemon should start when the server boots.

Update Project Directory When Project is Committed to Subversion Repository
Often times it is help to update a project directory after a commit has been done to the project repository. For example, say we makes changes to a file in our ‘project1′ repository and after we commit the changes we want the Website directory for the project to up auto updated. For this example, create a Website directory for the ‘project1′ project: >> mkdir /var/www/project1Now checkout the ‘project1′ repository: >> svn checkout svn://[server IP address]/project1 –username [user] /var/www/project1Now we need to setup a post-commit script that will update this project Website directory when someone commits to the ‘project1′ repository. Copy the post-commit hook template to a post-commit script file: >> cp -v /home/repos/project1/hooks/post-commit.tmpl /home/repos/project1/hooks/post-commitMake the new file executable: >> chmod +x /home/repos/project1/hooks/post-commitEdit the post-commit script: >> nano /home/repos/project1/hooks/post-commitAnd add a couple lines that will update the Website directory: #!/bin/sh
sudo -u [shell username] /usr/bin/svn update /var/www/project1 –username [svn repo username] –password [svn repo user password]Save and close the file. Now when a commit is executed on the ‘project1′ repository, the post-commit script will auto update the ‘/var/www/project1′ directory. If any errors occur when committing make sure that all permissions and credentials are correct.

http://www.shayanderson.com/linux/install-and-setup-subversion-server-on-ubuntu-1210-server-with-multiple-repositories.htm