One thing you want to do regularly is analyze your Visual SourceSafe database for errors. You should also back it up periodically. What most people don’t know is that VSS comes with utilities that make repair & backups a snap, as long as you wrap them in a few handy batch/script files. These scripts assume you’re running Norton Antivirus and that your sourcesafe database is in the default “C:Program FilesMicrosoft Visual StudioCommonVSSdata” location.
Here’s an example of a script you can run to analyze your SourceSafe database for errors. It can’t repair the errors, because you have to kick people out & lock the database in order to fix errors or back it up. But it can’t hurt if you’re wondering how badly corrupted your db is.
===interactive_analyze_vss.bat===
@TITLE Analyzing source safe databases
REM set VSS_DATA_DIR to the SourceSafe "data" directory
set VSS_DATA_DIR=C:Program FilesMicrosoft Visual StudioCommonVSSData
FOR /F "tokens=2-4 delims=/ " %%i IN ('date /t') DO SET DATE=%%i-%%j-%%k
for /f "tokens=1-4 delims=: " %%a in ('TIME /T') do set Time=%%a-%%b
REM analyze the db
move "%VSS_DATA_DIR%backup" "%VSS_DATA_DIR%backup-%DATE%-%TIME%"
"C:Program FilesMicrosoft Visual StudioVSSwin32analyze.exe" -x -v4 "%VSS_DATA_DIR%"
pause
Here’s a script that will lock the database and repair any errors. You may have to run it a few times in a row to clean up the last of the errors.
===offline_analyze_and_fix_vss.bat===
@TITLE Analyzing source safe databases
REM set VSS_DATA_DIR to the SourceSafe "data" directory
set VSS_DATA_DIR=C:Program FilesMicrosoft Visual StudioCommonVSSData
FOR /F "tokens=2-4 delims=/ " %%i IN ('date /t') DO SET DATE=%%i-%%j-%%k
for /f "tokens=1-4 delims=: " %%a in ('TIME /T') do set Time=%%a-%%b
REM lock ss database
Copy Nul C:TEMPADMIN.LCK
Copy ADMIN.LCK "%VSS_DATA_DIR%loggedin"
REM clear out backup folder
del "%VSS_DATA_DIR%temp*.*" /f /q
del "%VSS_DATA_DIR%backup*.*" /f /q
REM stop norton or other antivirus clients
net stop "symantec antivirus client"
REM remove network shares (assuming the share is called "VSS_DATA")
NET SHARE VSS_DATA /DELETE /Y
REM analyze the db
"C:Program FilesMicrosoft Visual StudioVSSwin32analyze.exe" -f -v4 -i- "%VSS_DATA_DIR%"
REM back up the analysis backup stuff
move "%VSS_DATA_DIR%backup" "%VSS_DATA_DIR%backup-%DATE%-%TIME%"
REM recreate network shares
NET SHARE VSS_DATA=%VSS_DATA_DIR%
REM start antivirus
net start "symantec antivirus client"
REM unlock ss database
Erase "%VSS_DATA_DIR%loggedinADMIN.LCK"
@ECHO Finished analysis
Here’s a script that will back up your database to a flat file. You can then stick this file onto tape or CD for archiving, or use it to restore your database in case of total corruption. You can also use this file to restore your database onto a different machine in case you want to move your sourcesafe database.
===offline_backup_vss.bat===
@TITLE Backing up source safe databases
REM set VSS_DATA_DIR to the SourceSafe "data" directory
set VSS_DATA_DIR=C:Program FilesMicrosoft Visual StudioCommonVSSData
FOR /F "tokens=2-4 delims=/ " %%i IN ('date /t') DO SET DATE=%%i-%%j-%%k
for /f "tokens=1-4 delims=: " %%a in ('TIME /T') do set Time=%%a-%%b
REM lock ss database
cd "%VSS_DATA_DIR%loggedin"
Copy Nul ADMIN.LCK
REM stop norton/antivirus client
net stop "symantec antivirus client"
"C:Program FilesMicrosoft Visual StudioVSSwin32ssarc.exe" -d- "F:%DATE%-%Time%-vss-backup.ssa" $/
REM unlock ss database
cd "%VSS_DATA_DIR%loggedin"
Erase ADMIN.LCK
REM start norton
net start "symantec antivirus client"
@ECHO Finished backups
Lastly, here’s a script that will tell you the “real” filename for each of the files within the SourceSafe data directory. When your VSS database gets corrupted and you’re having trouble with a certain file, you can use the PHYSICAL.TXT file that this script generates to determine where to look. e.g. myfunctions.cs is corrupted, and PHYSICAL.TXT tells you that “IVHAAAAAA == myfunctions.cs”, then you can look in the VSS data directory for files starting with “IVHAAAAAA”, open them in notepad, & maybe salvage myfunctions.cs from the info within. Conversely, if VSS analysis tells you that IVHAAAAAA is having serious issues, you’ll know that myfunctions.cs is impacted, and take appropriate action (shrug your shoulders, freak out, etc.).
===list_physical_files.vss===
REM list physical files
"C:Program FilesMicrosoft Visual StudioVSSwin32SS" PHYSICAL $/ -R –O@..PHYSICAL.TXT
These scripts came in handy one day when we had problems with our VSS box. A developer went to check in his work and got an error, and found that not only did he lose his local copy, but the version in sourcesafe was corrupt as well. Zero kb, and unable to get previous versions. Days of work potentially lost. We ran the analyze&fix job a few times to no avail. So then we ran list_physical_files & found out which VSS data file contained his code (e.g. “ismaaaaa”), then looked into VSS’s data directory & saw ismaaaaa, ismaaaaa.a, and ismaaaaa.b. We open each of those in notepad & saw that ismaaaaa.a contained his latest code. We then copied the contents of ismaaaaa.a into a new file, renamed that to the source code file he was working on, checked that back into sourcesafe, and the day was saved.
The moral of the story: use the above scripts as a starting point to troubleshooting, repairing, and archiving your SourceSafe databases.
Update: just read something that implies running “net stop server /y” will kick off users. So until I get a chance to update (and test) the scripts, try sticking “net stop server /y” before the “lock vss” step, and “net start server” after the “unlock vss” steps. What’s the worst that can happen? 🙂