End-to-End Analyze Script for Keeping a VSS Database Healthy

One of the most frequently asked questions about VSS is: 'I can't run Analyze on a weekly basis as recommended because inevitably, there's at least one user who forgets to checkin their files on Friday afternoon. Is there any way to disconnect users from a database?'  The short answer is a qualified 'no'.  VSS provides no way to forcibly disconnect users from a database.  The long answer is YES, but you have to do it in Windows.  My team uses the following PERL script which:

1) Forcibly disconnects all users from a VSS database (net stop server /y);
2) Locks the VSS database (copy \"$db\\data\\loggedin\\locked.lk\" \"$db\\data\\loggedin\\admin.lk\");
3) Runs Analyze.exe (system("\"$db\\win32\\analyze.exe\"","-b\"$db\\backup\"","-i-","-v4","-f","-c","-d","\"$db\\data\"");

As you'll see, our script does much more than that, but those are the basics.  Please feel free to post your own scripts in the comments section of this post. As always, your comments and questions are greatly appreciated.  For an alternative to this script, see http://www3.primushost.com/~ckollars/backup.html#force.

Special thanks to Justin Russell for offering to bare his code to the world.  Justin, you're a scholar and a gentleman.  Thank you.

# MaintainVSS
# Justin Russell
# Microsoft Corporation
# May 2003
# Performs various support services for a VSS database

use Getopt::Long;

# set defaults
my $db = "H:\\DB\\Anacortes";
my $bAnalyze = '';
my $bBackup = '';
my $bShadow = '';
my $logroot = "G:\\MantainVSSLogs";
my $help = '';
my $shadowpath = " \\\\Fidalgo\\shadow\\Anacortes1";
my $backuppath = "D:\\backups\\Anacortes";

GetOptions( "database=s"    => \$db,        # required (=) string path to db
            "analyze"       => \$bAnalyze,  # bool flag: should we analyze the db?
            "backup"        => \$bBackup,   # bool flag: should we backup the db?
            "shadow"        => \$bShadow,   # bool flag: should we recreate the shadow?
            "log=s"         => \$logroot,   # required string path to log
            "shadowpath=s"  => \$shadowpath,# where the database should be shadowed to
            "backuppath=s"  => \$backuppath,# where the database should be backuped to
            "help|?"        => \$help)      # bool flag: should we display help?
            or die "Couldn't get options. $!\n"; 
           
if ($help || !($bAnalyze || $bBackup || $bShadow))
{
    print "\nMaintainVSS.pl \nFor limited support contact Justin Russell \n\nUsage: MaintainVSS.pl [--database=\"path\"] [--log=\"path\"] [--analyze] [--backup[:\"path\"]] [--shadow[:\"path\"] \nAll options are optional (though if you don't provide a command nothing will happen :) ), can be called in the short form (e.g. \"-a\" instead of \"--analyze\"), and equal signs are optional.\n\n";
    exit;
}

my @daynum = localtime;
$logpath = "$logroot\\$daynum[7]\.log";

my $user = getlogin;
$now = scalar localtime;

open LOG, ">$logpath" or die "Couldn't open or create the log at $logpath: $!\n";
print LOG "MaintainVSS started at $now by $user. \n\nToday we'll be running the following commands on $db: \nAnalyze: $bAnalyze \nBackup: $bBackup \nRecreate Shadow: $bShadow \n\n";

if ($bAnalyze)
{
    $now = scalar localtime;
    print LOG "Starting Analyze routine at $now.\n\n";
    analyze();
}

if ($bBackup)
{
    $now = scalar localtime;
    print LOG "Starting Backup routine at $now.\n\n";
    backup();
}

if ($bShadow)
{
    $now = scalar localtime;
    print LOG "Starting Shadow routine at $now.\n\n";
    shadow();
}

$now = scalar localtime;
print LOG "\nScript compete at $now\n";
# PAU

# use this to run the analyze routine
sub analyze
{
 print "Contacting users.\n";
 system("net send /users \\Fidalgo going offline in 0 minutes for routine VSS maintenance \(Analyze.exe\). Back in a few hours."); # inform users of shutdown
 print "Stopping file sharing.\n";
 system("net stop server /y"); # shut down file sharing
 print "Locking database.\n";
 `copy \"$db\\data\\loggedin\\locked.lk\" \"$db\\data\\loggedin\\admin.lk\"`; # lock the db
 print "Deleting temporary files.\n";
 `del \"$db\\temp\\*.*\" /f /q`; # delete old temp files
 print "Saving backup.\n";
 `copy \"$db\\backup\" \"$db\\old\"`; # backup the backup
 print "Setting up new backup.\n";
 `del \"$db\\backup\" /f /q`; # delete the existing backup
 print "Checking for old Analyze log.\n";
 `del \"$db\\backup\\analyze.log\" /f /q`; # delete analyze log, if any
 # run analyze with: -b backup, -i- non-interactive, -v4 verbose, -f fix errors, -c compact db, -d delete unused files
 print "Starting Analyze.\n";
 system("\"$db\\win32\\analyze.exe\"","-b\"$db\\backup\"","-i-","-v4","-f","-c","-d","\"$db\\data\""); # note: no space after b switch
 print "Unlocking database.\n";
 `del \"$db\\data\\loggedin\\admin.lck\"`; # unlock the db
 print "Moving and renaming the Analyze log.\n";
 `move \"$db\\backup\\analyze.log\" \"$logroot\\analyze$daynum[7].log\"`;
 print "Starting file sharing.\n";
 system("net start server"); # restart file sharing
 print "Staring \"Computer Browser\"\n";
 system("net start \"Computer Browser\"\n");
 print "Starting \"Distributed File System\"\n";
 system("net start \"Distributed File System\"\n");
 print "Starting \"Backup Exec Remote Agent for Windows NT\/2000\"\n";
 system("net start \"Backup Exec Remote Agent for Windows NT\/2000\"");
}

# use this to run the backup routine
sub backup
{
    get($backuppath);
}

# use this to run the shadow recreation routine
sub shadow
{
    # Delete and recreate old Get path to avoid keeping old files
    `rd $shadowpath \/s \/q`; `md $shadowpath`;
    get($shadowpath);
}

sub get
{
    # ensure path passed exists
    my $path = $_[0];
   
    # -r- means don't include comments, -r means work recursively
    # -i- disables prompts
    # -gl specifies the destination of the Get, -gtu sets the timestamp to the modified time,
    # -gf- disables force_dir initialization, -gwr replaces read-only files, -y sets the username
   
    system ("$db\\win32\\ss.exe","get","\$\/","-R","-I-","-GL$path","-GTU","-GF-","-GWR","-Y$user") == 0 or die "Couldn't run the VSS Get command: $!\n";
}

This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft kann für die Richtigkeit und Vollständigkeit der Inhalte in dieser Newsgroup keine Haftung übernehmen. Este mensaje se proporciona "como está" sin garantías de ninguna clase, y no otorga ningún derecho.

Published 16 July 03 04:50 by KorbyP

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Jesse Tilly said on September 8, 2003 6:10 PM:
Per MSDN's "best Visual Source Safe practices", you should probably run the 'ss physical' command on each repository. This will create a mapping file between every database file and real file in the projects. That way, when analyze says 'aaaaaaa' is corrupt, you know what real file might be impacted.
# Ashwin said on October 15, 2003 7:18 AM:
In VSS some of the files get extension as .vss.tmp for example if the file name is format.asp the file name will be renamed to format.asp.vss.tmp. If I rename the file to format.asp it say that the file is alerady exits, but no file will be there with that name. Can you give me a solution for this. Wating for your responces. Ashwin. You can contact me at ashwinchandra_k@hotmail.com
# Korby Parnell's WebLog said on January 22, 2004 4:07 PM:
# TrackBack said on February 25, 2004 2:15 PM:
Welcome to my web wanderings :: End-to-End Analyze Script for Keeping a VSS Database Healthy
# Greif said on February 27, 2004 11:53 AM:
VBScript to do something similar -

Option Explicit

WScript.Echo("Start - " & strDateStamp & " " & Time)

const VSS_PATH = "C:\sourcesafe\" 'All VSS Databases reside under this folder
const ANALYZE_PATH = """C:\Program Files\Microsoft Visual Studio\Common\VSS\win32\ANALYZE.EXE"""

dim intReturn, strDateStamp, WshShell, FileSys, strDailyBackupFolder

strDateStamp = getDateStamp
set WshShell = WScript.CreateObject("WScript.Shell")
set FileSys = CreateObject ("Scripting.FileSystemObject")

writeVSSDatabases
analyzeDatabases

set WshShell = nothing
set FileSys = nothing

WScript.Echo("End - " & strDateStamp & " " & Time)

Sub writeVSSDatabases
'retreive a directory listing of all VSSDBs (folders containing 'srcsafe.ini')
'dir " + VSS_PATH + "srcsafe.ini /b /s > " + VSS_PATH + "VSS_Databases.txt"
End Sub

Sub analyzeDatabases
WScript.Echo("Enter analyzeDatabases - " & Time)
dim vssfile, txtstream, line, VSSDBPath
set vssfile = FileSys.GetFile(VSS_PATH + "VSS_Databases.txt")
set txtstream = vssfile.OpenAsTextStream (1, -2)

do while txtstream.AtEndOfStream <> true
line = txtstream.ReadLine
VSSDBPath = trim(Left(line, InStrRev(line, "\"))) & "data\"

WScript.Echo("Processing - " & VSSDBPath)
WScript.Echo("Verify backup folders.")
createDailyBackupFolder VSSDBPath

WScript.Echo("Locking database.")
lockVSSDB VSSDBPath

WScript.Echo("Disconnecting all sessions.")
disconnectSessions

WScript.Echo("Analyzing database.")
analyzeVSSDB VSSDBPath

WScript.Echo("Unlocking database.")
unlockVSSDB VSSDBPath
loop

txtstream.Close

set vssfile = nothing
set txtstream = nothing
WScript.Echo("Exit readVSSDatabases - " & Time)
End Sub

Sub createDailyBackupFolder(strVSSDBPath)
'create a daily backup folder
End Sub

Sub lockVSSDB(strVSSDBPath)
'create admin.lck file in VSSDBPath's data\loggedin directory
End Sub

Sub unlockVSSDB(strVSSDBPath)
'admin admin.lck file in VSSDBPath's data\loggedin directory
End Sub

Sub disconnectSessions
'Disconnect all network sessions to the server
End Sub

Sub analyzeVSSDB(strVSSDBPath)
'Execute the analyze utitliy against VSSDB
End Sub
# Joey said on March 28, 2004 5:20 AM:
Thanks alot to Greif for the excellent VBScript - you'v helped me alot!
Another question to you all that might not be directly connected to the Blog's main idea but is very important as well...
Can I make an archive in VSS by a ranged date?
In example: I want to archive only the files that were last accessed/updated in the past month, is that possible???
Thanks ahead for you'r tremendouse contribution!

-- Joey
# mvm said on April 11, 2004 7:06 AM:
I think you can. ssarc.exe has the option "-v", the same as for ss.exe. "-v" option can specify a range of date.

see more in SSUSEXP.CHM, the user manual.
# Steve said on March 14, 2006 9:09 AM:
I'm not expert on scripts, and know almost nothing about Source Safe, but I think the above may help me with a small problme I'm encountering.

I've consistently got users staying logged in to Source Safe, or leaving files open, through my Backup window. This causes the backup to consistently skip the SourceSafe files in use.

With tweaking, may I use the above script to disconnect users from the database so that I may back it up properly?

Thanks,
Steve
# Judd said on June 28, 2006 9:29 PM:
Any one out there knows why it would take 14 hrs to run analyze on a DB with the -f (rebuild cache) and without the -f it run in 2 min?
Thanks again for all helpfull contributuion!!!!
# Poll Pitt said on August 26, 2006 5:02 PM:
I am so [url=http://access.2surf.eu]lucky[/url] on having what I have! And good luck in yours [url=http://2access.2surf.eu]search[/url].
Just visit [url=http://access.122mb.com]my site[/url].









# Poll Pitt said on August 26, 2006 7:09 PM:
I am so [url=http://access.2surf.eu]lucky[/url] on having what I have! And good luck in yours [url=http://2access.2surf.eu]search[/url].
Just visit [url=http://access.122mb.com]my site[/url].









# Poll Pitt said on August 27, 2006 1:11 AM:
I am so [url=http://access.2surf.eu]lucky[/url] on having what I have! And good luck in yours [url=http://2access.2surf.eu]search[/url].
Just visit [url=http://access.122mb.com]my site[/url].









# Poll Pitt said on August 27, 2006 4:56 AM:
I am so [url=http://access.2surf.eu]lucky[/url] on having what I have! And good luck in yours [url=http://2access.2surf.eu]search[/url].
Just visit [url=http://access.122mb.com]my site[/url].









# Poll Pitt said on August 27, 2006 1:07 PM:
I am so [url=http://access.2surf.eu]lucky[/url] on having what I have! And good luck in yours [url=http://2access.2surf.eu]search[/url].
Just visit [url=http://access.122mb.com]my site[/url].









# Dick said on August 27, 2006 7:09 PM:
good site
wellcome to our
http://bmw.dkblog.org
# management project software said on September 1, 2006 5:31 PM:
Thank you so much for this great post  about <a href="http://eteamz.active.com/moneymanagement/files/management-project-software.html"">http://eteamz.active.com/moneymanagement/files/management-project-software.html" title="management project software">management project software</a> and [URL=http://eteamz.active.com/moneymanagement/files/management-project-software.html]management project software[/URL]
# mortgage payment said on September 5, 2006 5:02 PM:
Very interesting and good point about <a href="http://eteamz.active.com/moneymanagement/files/mortgage-payment.html"">http://eteamz.active.com/moneymanagement/files/mortgage-payment.html" title="mortgage payment">mortgage payment</a> and [URL=http://eteamz.active.com/moneymanagement/files/mortgage-payment.html]mortgage payment[/URL]
# hair loss said on September 8, 2006 2:40 AM:
Thanks for the great tips about <a href="http://paulsmall.bravehost.com/hair-loss.html"">http://paulsmall.bravehost.com/hair-loss.html" title="hair loss">hair loss</a> and [URL=http://paulsmall.bravehost.com/hair-loss.html]hair loss[/URL]
# hair loss said on September 8, 2006 2:40 AM:
Thanks for the great tips about <a href="http://paulsmall.bravehost.com/hair-loss.html"">http://paulsmall.bravehost.com/hair-loss.html" title="hair loss">hair loss</a> and [URL=http://paulsmall.bravehost.com/hair-loss.html]hair loss[/URL]
# hair loss said on September 8, 2006 2:40 AM:
Thanks for the great tips about <a href="http://paulsmall.bravehost.com/hair-loss.html"">http://paulsmall.bravehost.com/hair-loss.html" title="hair loss">hair loss</a> and [URL=http://paulsmall.bravehost.com/hair-loss.html]hair loss[/URL]
# hair loss said on September 8, 2006 2:40 AM:
Thanks for the great tips about <a href="http://paulsmall.bravehost.com/hair-loss.html"">http://paulsmall.bravehost.com/hair-loss.html" title="hair loss">hair loss</a> and [URL=http://paulsmall.bravehost.com/hair-loss.html]hair loss[/URL]
# Poll Pitt said on September 16, 2006 4:14 AM:
I am so [url=http://access.2surf.eu]lucky[/url] on having what I have! And good luck in yours [url=http://2access.2surf.eu]search[/url].
Just visit [url=http://access.122mb.com]my site[/url].









# Poll Pitt said on September 16, 2006 7:09 AM:
I am so [url=http://access.2surf.eu]lucky[/url] on having what I have! And good luck in yours [url=http://2access.2surf.eu]search[/url].
Just visit [url=http://access.122mb.com]my site[/url].









# replicarolex said on September 16, 2006 12:36 PM:
[URL=http://http://replicarolexwatch.ir.pl]replica-rolex-watch[/URL]<a href="http://http://replicarolexwatch.ir.pl">replica rolex watch</a>
# Lance's Whiteboard said on September 28, 2006 12:11 PM:
After years of automating Visual SourceSafe(VSS), I am beginning to post and organize all my knowledge
# Kevin said on April 17, 2007 2:40 PM:

Here is an alternative script that I am working on.  This VSS maintenance script uses PowerShell.

# Korby Parnell s Social Software Wunderkammer End to End Analyze | Indoor Grills said on June 1, 2009 3:26 PM:

PingBack from http://indoorgrillsrecipes.info/story.php?id=4147

# Korby Parnell s Social Software Wunderkammer End to End Analyze | Green Tea Fat Burner said on June 7, 2009 10:19 PM:

PingBack from http://greenteafatburner.info/story.php?id=4412

# Korby Parnell s Social Software Wunderkammer End to End Analyze | Weak Bladder said on June 9, 2009 2:53 PM:

PingBack from http://weakbladder.info/story.php?id=4669

# Korby Parnell s Social Software Wunderkammer End to End Analyze | Cast Iron Cookware said on June 11, 2009 10:53 PM:

PingBack from http://castironbakeware.info/story.php?title=korby-parnell-s-social-software-wunderkammer-end-to-end-analyze

# Korby Parnell s Social Software Wunderkammer End to End Analyze | Hair Growth Products said on June 13, 2009 8:24 AM:

PingBack from http://hairgrowthproducts.info/story.php?id=233

# Korby Parnell s Social Software Wunderkammer End to End Analyze | debt consolidator said on June 19, 2009 10:54 AM:

PingBack from http://mydebtconsolidator.info/story.php?id=14313

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker