It's Not A Bug, It's A Feature Just another Developer weblog

29Jun/100

Backup a VisualSVN Repository with VBScript

Last week I had to make a backup of a VisualSVN Repository and trying backup each site individually would have taken me forever. so after a little digging on Google found this great little VB script file to backup all the repository's on a Windows server.

for some reason I cant find the original forum post were i found the script to give the original author credit for his work. but here is the script for anyone who would like to use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'Save File as : BackupSVN.vbs
Option Explicit

'loop through folders and use svnadmin to dump all repos to a backuplocation
'trying to run this type of command for each repo
'C:\Program Files\VisualSVN Server\bin>svnadmin dump C:\svnrepos\MyProject > \\nas\folder\MyProject.bak

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim scriptPath, argCount, namedArgs
scriptPath = replace(wscript.scriptfullname, wscript.scriptname, "")
argCount= WScript.Arguments.Count
Set namedArgs = WScript.Arguments.Named

Dim fso, svnExePath, repoFolderPath, backupFolderPath

Set fso = CreateObject("Scripting.FileSystemObject")

'************* uncomment or edit for the correct path to svnadmin****************
svnExePath = "D:\SVN_Server\bin\svnadmin.exe"


'************* edit this line to match your backup destination****************
backupFolderPath = "D:\SvnBackup"

'only needs editing if you don't put it into the repos folder
repoFolderPath = scriptPath


If namedArgs.Exists("help") Or namedArgs.Exists("?") Then
 Wscript.Echo "Usage: "
 Wscript.Echo "/svn:<svn> is optional to enter the svnadmin executable full path & name."
 Wscript.Echo " - default is " & svnExePath
 Wscript.Echo
 Wscript.Echo "/repos:<repos> is optional to select the svn repositories folder."
 Wscript.Echo " - default is " & repoFolderPath
 Wscript.Echo
 Wscript.Echo "/backup:<backup> is optional to select the svn backupdestination folder."
 Wscript.Echo " - default is " & backupFolder
 Wscript.Quit
End If

If namedArgs.Exists("svn") Then
  svnExePath = Trim(namedArgs.Item("svn"))
End If

If namedArgs.Exists("repos") Then
 repoFolderPath= Trim(namedArgs.Item("path"))
End If

If namedArgs.Exists("backup") Then
 backupFolderPath= Trim(namedArgs.Item("backup"))
End If


DumpSvnRepos svnExePath, repoFolderPath, backupFolderPath


'create and execute a batch file to dump all of the repos
'for some reason running the dump command directly will not work
Function DumpSvnRepos(svnExePath, rootFolderPath, backupFolderPath)
    Dim rootFolder, subFolders, folder, file
    Dim logFileName, text

    Set rootFolder = fso.GetFolder(rootFolderPath)
    Set subFolders = rootFolder.SubFolders

    logFileName = Replace(wscript.ScriptFullName, ".vbs", ".bat")

    Set file = fso.OpenTextFile (logFileName, ForAppending, True)

    For Each folder in subFolders
      Wscript.Echo folder.Name
      text = Chr(34) & svnExePath & Chr(34) & " dump " & Chr(34) & repoFolderPath & "\" & folder.Name & Chr(34) & " > "  & Chr(34) & backupFolderPath & "\" & folder.Name & ".bak" & Chr(34)
      file.WriteLine(text)

    Next

  file.Close

  Wscript.Echo "Batch file created: " & logFileName
  runCommand logFileName

End Function



Function runCommand(commandLine)

        Dim shell, errorCode
        Set shell = CreateObject("WScript.shell")

        shell.Run commandLine, ,True 'command, WindowType, WaitOnReturn

        errorCode = err.Number
        If errorCode > 0 Then
                wscript.echo "runCommand Error " & err.Number & ": " & VbCrLf & commandLine & VbCrLf & err.Description
                logText "runCommand Error " & err.Number & ": " & vbTab & commandLine & vbTab & err.Description
                Err.Clear
        Else
                logText "runCommand Success : " & vbTab & commandLine
        End If

        runCommand = errorCode


End Function

Sub LogText(text)
    Dim file, logFile

    logFile = Replace(wscript.ScriptFullName, ".vbs", ".txt")

    Set file = fso.OpenTextFile (logFile, ForAppending , True)

    ' Writes Text every time you run this VBScript
    file.WriteLine(text)
    file.Close

End Sub