Aaron Saray

open source programmer,
web developer

entrepreneur, author
and musician

My Blog

contains PHP, Web and business/entrepreneurial related content. Please join in the conversation!

Automatic Backup with SVN on Windows

A while ago, I decided that I needed to have a better backup solution for my file server. After doing some research on various systems, I let my inner programmer take over – in addition to my desire to NEVER LOSE ANYTHING – and I defaulted to use SVN.

I was using a Windows machine as my file server – so I wrote some batch files. I also had SVN installed on the machine. The final touch was adding scheduled tasks.

The setup includes a computer that is always on with windows, svn command line, and 5 directories to monitor for backups.

First thing’s first, do an SVN Checkout

The very first thing I did was make an SVN checkout in all of the five parent directories. This way I can continue to use SVN add, svn commit without any other interaction. Don’t worry, we’ll use recursion!

Create the full list of backups

So, first thing’s first: Create the list of directories that need to be monitored. I made them in this txt file named ‘svndirectories.txt’:

1
D:\pictures D:\storage\videos\misc D:\storage\files\art D:\storage\files\NeverAgain D:\storage\files\Therapee

Note, all of them are separated by a space. This becomes important in our next batch script.

Schedule the SVN Add

I added an SVN Add batch script at Midnight on sundays. Actually, there are two batch files. I made them separately so that I could invoke a scheduled task – but also run the “add” by hand if need be.

The first file, addsvn.bat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@echo off

REM ------------------------------------------------------------------

REM - forces adds on all svn files
REM ------------------------------------------------------------------


:START
REM - Get file to process
set direct=%1
echo %direct%
echo.

:SVNADD
svn add --force %direct%\*

:NEXTFILE
shift
if "%1"=="" goto END
goto START


:END

That will force an add of each file passed in on the command line. Then, the batch file that I made to be ran from the scheduler will read in the folders from the text file, and run this script. Here is ‘scheduled_addsvn.bat’:

1
2
3
4
5
6
7
8
@echo off
REM - This is what should be scheduled to add files to svn repos

REM - read in svndirectories.txt

for /f "tokens=*" %%a in ('type svndirectories.txt 2^>NUL') do set value=%%a

REM - call the addsvn program with all the directories

addsvn %value%

Theoretically, I could have called it with the entire line of files after it, but I wanted to call them separately to handle errors better.

After all of these have been added, lets move on…

Schedule SVN Commit

Just in case I made a huge addition of files, I let an hour pass between scheduled add and scheduled commits. Additionally, I ran the commit every day instead of every week. I figured I’d make more changes than I would make additions.

So first, read in all of the directories again and run the commit. Then, the file to schedule. These are pretty much similar, just different commands:

commitsvn.bat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@echo off

REM ------------------------------------------------------------------

REM - commits all SVN changes
REM ------------------------------------------------------------------


:START
REM - Get file to process
set direct=%1
echo %direct%
echo.

:SVNCOMMIT
svn commit --message="Auto Backup" %direct%\*

:NEXTFILE
shift
if "%1"=="" goto END
goto START


:END

scheduled_commitsvn.bat:

1
2
3
4
5
6
7
8
@echo off
REM - This is what should be scheduled to commit files to svn repos

REM - read in svndirectories.txt

for /f "tokens=*" %%a in ('type svndirectories.txt 2^>NUL') do set value=%%a

REM - call the addsvn program with all the directories

commitsvn %value%

This has worked out pretty well for me. If you see anything I could do better, please let me know!

This entry was posted in scripting, svn, windows and tagged , , . Bookmark the permalink.

9 Responses to Automatic Backup with SVN on Windows

  1. john.reeves says:

    aww, cute little batch files *snicker*.

    good idea. I thought of this exact thing, never tried it though. I was thinking it’d be awesome to make a file system in linux that uses svn (using FUSE, similar to how the GmailFS works). My main concern was binary files, as I don’t think there’s an efficient way to deal with those. Awesome that you did it though.

  2. Jill says:

    I know this is an old entry, but I was just wondering (if you remember) if you set this to be a scheduled task on your windows machine by chance?

    I did something similar–backing up SVN code through a Windows machine and into TFS. The batch runs great when i execute manually, but when I’m checking my history in TFS, it indicates that the check-in doesn’t happen when I’m not logged into the box.

    Just wondering–i’m trying to find the reason & don’t know if it’s the SVN export or the TFS check-in that isn’t happening.

    Thanks.

    • Aaron says:

      @Jill: I didn’t run into that problem before… but when you create a scheduled task, one of the advanced options is choosing a login profile. You may want to see if you can specify an account that way – just in case…

    • Aaron says:

      @Jill: Yah I set it up as a scheduled task – running as my user account.

  3. linux backup says:

    File backups do not need to be a task. A thoughtful backup cron will run for years.

  4. Sourabh Shah says:

    Can i get my destination automatically updated through scheduler when the source is on another pc witihin a network.If possible plz rply me on email mentioned Thnx in Advamce

  5. bhavya gupta says:

    how can i take back up of only the files that have been updated rather than taking backup of whole data?

  6. Vincent says:

    svn 1.7 has broken your script, would you please shed some light?

    svn: E200009: Commit failed (details follow):
    svn: E200009: ‘D:\test_auto_sync\*’ is not under version control

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>