Thursday, 10 March 2011

Find missing databases

When server clusters have not been set up correctly, or you know that two server should have identical database replicas, we as developers often are asked to write a nice piece of Lotusscript to find all the databases on one server and then find the databases that are missing from another.

As I have been asked this at numerous clients, I thought I'd share the code for future reuse. The code below works if the databases are in the same file structure. This may not be the case at all companies and you may want to change the code slightly to use the replica id as the key.
The first part searches the first server and creates a document per database.

Dim s As New NotesSession
Dim currdb As notesdatabase
Dim newdoc As notesdocument

Set currdb = s.CurrentDatabase

Dim dbdir As New NotesDbDirectory(SERVER_NAME")
Dim db As NotesDatabase
Set db = dbdir.GetFirstDatabase(DATABASE)

While Not db Is Nothing
Print db.Title, , db.FileName
Set newdoc = currdb.CreateDocument
newdoc.server = db.Server
newdoc.path = db.FilePath
newdoc.database = db.Title
newdoc.form = "Database"
Call newdoc.Save(True, True)

Set db = dbdir.GetnextDatabase

The second agent runs against the new document collection created from the agent above and tries to find the database on the other server.

Dim s As New NotesSession
Dim db As NotesDatabase
Set db = s.CurrentDatabase

Dim vKey As NotesView
Set vKey = db.GetView("(DatabasesKey)")

Dim vDatabases As NotesView
Set vDatabases = db.GetView("Databases")

Dim mDoc As NotesDocument
Dim kDoc As NotesDocument
Dim dDoc As NotesDocument

Set dDoc = vDatabases.GetFirstDocument
While Not dDoc Is Nothing
If dDoc.Server(0) = SERVER1" Then
svr = "SERVER2"
svr = "SERVER1"
End If

key = svr & dDoc.Path(0)
Set kDoc = vKey.getdocumentbykey(key)
If kDoc Is Nothing Then
Set mDoc = db.CreateDocument
Call dDoc.CopyAllItems( mDoc, True )
mDoc.Form = "Missing"
mDoc.Missing = "Database not found on other server"
Call mDoc.Save(True, True)
End If

Set dDoc = vDatabases.GetNextDocument(dDoc)


No comments:

Post a Comment