Sync SVN repository with remote FTP server

19 01 2009

This script was written for the company I work for well over a year ago, but I just decided to release it to the public now.  It has been in use all of this time and is still in use now, so that should tell you that it's a pretty reliable script.  Any bugs we encountered over the course of using this program have been fixed.  Hopefully you'll have the same luck as us.

Basics:

This script is run as a SVN hook (post-commit).  After committing changes to your subversion repository, this script is invoked and synchronization begins.  The script creates a local copy of the repository, and files just committed are sent via FTP.

Usage:

One copy of the script can be used for an unlimited number of repositories, so the script can be placed in a globally-available location.  In our scenario, we placed the script at /usr/local/bin/svn2ftp.sh .  There are four command-line arguments to the script that are required.  The first two arguments can be kept the same as below, as they're passed to the script by subversion.  The third parameter is the path between the repository and the trunk (e.g., if your SVN path is http://<host>/svn/repo/dir/trunk, then you'd put "dir" as the parameter; otherwise, you'd put "." if there's no directory in between).  The fourth parameter is the full path to the config file of the repo.  A sample to put in repo/hooks/post-commit:

CODE:
# Update this repo with remote FTP server
DATE=`date +'%m-%d-%Y'`
/usr/local/bin/svn2ftp.sh "$REPOS" "$REV" "." "/svn2ftp/conf/repo.conf" >> /svn2ftp/logs/repo-$DATE.log

We have our directory structure as follows, but you can change it as you please:

/svn2ftp/conf     #  Where config files are stored

/svn2ftp/logs     #  Where log files are stored

/svn2ftp/repos   #  Where raw repo contents are stored

A sample config file can be found below.

The code:

I initially wrote this program using the Linux 'ftp' program.  It works well and all, but if I encounter server or any other errors, the program still exits with a successful error code.  This was a bummer in our situation, so I wrote an FTP client in PHP that reports errors correctly.  When an error is encountered, files that were not uploaded successfully will be retried upon the next commit.  Links to both versions, as well as the config file, are below:

Version using Linux 'ftp' program

Version using PHP FTP program

Sample config file

PHP FTP Updater



Trackbacks


No Trackbacks

Comments

Display comments as (Linear | Threaded)
18 02 2009
#1 Krishna (Reply)

Hi, thanks for such a wonderful concept. I have installed SVN on my Windows Server. I need a similar script for windows. Can you help with some instructions ??
23 02 2009
#1.1 Ameir Abdeldayem (Reply)

Hi Krishna,

The script I wrote heavily relies on Linux command-line utilities. An equivalent script can surely be written for Windows, but probably not through the standard toolset. You'll likely need to install Cygwin or Powershell to achieve strength similar to that of the Linux commandline. From there, you should be able to create a spin-off of my script by going line by line.

I hope that helps!
03 03 2009
#2 swaps (Reply)

Hi, thanks for your script and concept. could you help me how can i install it on linux host.could you provide me step by step method.
i'm using fedora 8 and svn 1.4.4.

thanx
13 03 2009
#2.1 Ameir Abdeldayem (Reply)

Do you currently have the SVN repository working as it should? If so, updating the variables in the config file should be all that is needed. If you need more assistance, I can write something more elaborate up. Let me know.
03 12 2009
#3 Elwin (Reply)

I found maybe a nice addition to your script. I found that you can easily find out specific changes by revision by the following command:

svn log -r revision -q -v repository
22 01 2010
#4 Stars (Reply)

post-commit should look like this:

# Update this repo with remote FTP server
DATE=`date +'%m-%d-%Y'`
/usr/local/bin/svn2ftp.sh "$1" "$2" "." "/svn2ftp/conf/repo.conf" >> /svn2ftp/logs/repo-$DATE.log

This is because vars $REPOS and $REV doesn't exists, instead post-commit script is executed by svn passing two parametres: 1) repo name 2) rev

I also executed this script with wrong path to repo.conf (my mistake) and it started working in root directory - so there should be validation in the beginning if HOME and SVNHOME vars are not null.

Anyway nice script, thanks!
15 05 2010
#5 n2h (Reply)

Dear Ameir

The script can only upload new commit file to FTP server.
How about the deleted file via commit, the synchronization do not delete files on remote ftp server.
15 05 2010
#6 n2h (Reply)

Sorry I found the deleted files
09 07 2010
#7 Chris (Reply)

Great Idea!
Could you please upload the files again?
Best regards,
Chris
10 07 2010
#7.1 Ameir Abdeldayem (Reply)

I'm in the process of moving my site over from another server, and don't have all the files on-hand at the moment; they should be up in a day or two.
13 07 2010
#7.2 Ameir Abdeldayem (Reply)

The files are now up! Let me know if something doesn't work.

Add Comment


You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
BBCode format allowed

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA