Posts Tagged ‘subversion’

Automated backup via subversion

Tuesday, October 14th, 2008

[EDIT: this script was not fully working, now corrected. see details in this post]
We are using Subversion as resource control for our (flash) projects, and we recently though we could use it to automate our backups.

The subversion server is in our local network, and the backup machine outside.

To make things easy, we ve got two ISPs, that we switch every 2 weeks (the ISPs being not terrible reliable here in New Zealand)

So I had to:

- configure the router in our local network so that the subversion server get always attributed the same ip
- configure the router in our local network so that incoming request on the svn port get redirected to the subversion server (identified by its IP, hence the first step)
- checkout all our projects in the backup server
- install a command-line subversion client on the backup server
- write a dos script on the backup server (a windows machine) to update all projects

As I did not know much about dos scripting, it took me a little while.
I had to
- check what is the current IP of our local router from outside (depeding on which ISP we’re plug at the moment of the update)
- if required, relocate the subversion url of all projects
- update all projects
- log some error in case of failure

So below is the corresponding script.

@echo off
REM set the two possible urls for fracture
SET URL1=https://1.2.3.4:8443/svn/
SET URL2=https://5.6.7.8:8443/svn/

REM get the current location: parse the file currentUrl.txt (which contains only one line) and set CURRENT_LOCATION
FOR /f %%a IN (currentUrl.txt) DO SET CURRENT_LOCATION=%%a
REM Current location set to... %CURRENT_LOCATION%
REM try updating a project
CD project1
svn update
REM ERRORLEVEL is changed by most of the commands upon execution, usually to 0 if success, 1 or more if failure
IF ERRORLEVEL 1 (
 cd ..
 REM Update the log file
 echo %DATE% Notice: changing url to %CURRENT_LOCATION% >> error.log
 REM Reset CURRENT_LOCATION
 SET OLD_LOCATION=%CURRENT_LOCATION%
 IF %CURRENT_LOCATION%==%URL1% (
  SET CURRENT_LOCATION=%URL2%
 ) ELSE (
  SET CURRENT_LOCATION=%URL1%
 )
 REM Update currentUrl file
 echo %CURRENT_LOCATION%> currentUrl.txt
 REM Relocate all folders
 FOR /D %%b in (*) DO (
  cd %%b svn switch --relocate %OLD_LOCATION%/%%b/trunk %CURRENT_LOCATION%/%%b/trunk
  IF ERRORLEVEL 1 echo %DATE% WARNING: project %%b could not be relocated >> ../error.log
  cd ..
 )
) ELSE (
 cd ..
)

REM Update all projects
FOR /D %%b in (*) DO (
 cd %%b
 svn update
 IF ERRORLEVEL 1 echo %DATE% ERROR: project %%b could not be updated >> ../error.log
 cd ..
)

@echo off
Setlocal ENABLEDELAYEDEXPANSION
REM set the two possible urls for fracture
Set URL1=https://125.236.208.239:8443/svn/
Set URL2=https://203.109.198.29:8443/svn/

REM get the current location
For /f %%a IN (currentUrl.txt) DO Set CURRENT_LOCATION=%%a

REM Current location set to... %CURRENT_LOCATION%

REM try updating a project
CD Debug
svn update
If ERRORLEVEL 1 (
CD ..
echo %DATE% Notice: changing url from %CURRENT_LOCATION% >> error.log
REM Reset CURRENT_LOCATION
Set OLD_LOCATION=%CURRENT_LOCATION%

If %CURRENT_LOCATION%==%URL1% (
Set CURRENT_LOCATION=%URL2%
) ELSE (
Set CURRENT_LOCATION=%URL1%
)

REM Update currentUrl file
echo !CURRENT_LOCATION! > currentUrl.txt
REM Relocate all folders

For /D %%b IN (*) DO (
CD %%b
svn switch --relocate !OLD_LOCATION!/%%b/trunk !CURRENT_LOCATION!/%%b/trunk
If ERRORLEVEL 1 echo %DATE% WARNING: project %%b could not be relocated >> ../error.log
CD ..
)
) ELSE (
CD ..
)

REM Update all

For /D %%b IN (*) DO (
CD %%b
svn update
If ERRORLEVEL 1 echo %DATE% ERROR: project %%b could not be updated >> ../error.log
CD ..
)