'Replicate with all Servers: 

Option Public
Option Declare

Sub Initialize
	Dim db As NotesDatabase
	Dim nab As NotesDatabase
	Dim nLog As NotesLog	
	
	Dim s As New NotesSession
	Set nab = s.CurrentDatabase
	
	'The error log, not essential but handy --- use your own mechanism to get one
	'Suggestion: The managers of the DB get a mail log
	Set nlog = New NotesLog("Replicate with all servers")
	Call nlog.OpenAgentLog
	nlog.LogErrors = True
	nlog.LogActions = True
	
	'Here we set the database to be replicated -- for now it is the NAB, but it is just one statement.
	Set db = s.CurrentDatabase
	
	'Call the main function
	Call ReplicateLocalDB(nab, db, nlog)
	
	'Close our log
	Call nlog.Close
	
End Sub








Function getAdminServer(db As NotesDatabase, nlog As NotesLog) As String
	
	Dim acl As NotesACL
	
	On Error Goto Err_getAdminServer
	
	Set acl = db.ACL
	getAdminServer = acl.AdministrationServer
	
Exit_getAdminServer:
	Exit Function
	
Err_getAdminServer:
	Call nlog.LogError(Err,Error$ & " in getAdminServer in line " & Cstr(Erl))
	getAdminServer = ""
	Resume Exit_getAdminServer
	
End Function
Sub ReplicateWithOneServer(db As NotesDatabase, serverName As String,nlog As Noteslog)
	
	On Error Goto Err_ReplicateWithOneServer
	
	Call db.Replicate(serverName)
	
Exit_ReplicateWithOneServer:
	Exit Sub
	
Err_ReplicateWithOneServer:
	Call nlog.LogError(Err,Error$ & " in ReplicateWithOneServer line " & Cstr(Erl) & ", Server: " & serverName)
	Print Error$ 'Mostly: Timeout or no replica found
	Resume Exit_ReplicateWithOneServer
End Sub
Sub ReplicateLocalDB(nab As NotesDatabase, db As NotesDatabase, nlog As NotesLog)
	
	Dim v As NotesView	
	Dim adminServer As String
	Dim curServer As String
	Dim doc As NotesDocument
	
	On Error Goto Err_ReplicateLocalDB
	
	If db.Server <> "" Then
		Msgbox "Please run this from a local replica!"
		Exit Sub
	End If
	
	'Find the admin server to give the first shot to it
	adminServer = getAdminServer(db, nlog)
	
	'Now Replicate the admin server
	Call ReplicateWithOneServer(db, adminServer,nlog)
	
	'Get the view with all servers,
	Set v = nab.GetView("($Servers)")
	Set doc = v.GetFirstDocument
	
	Do Until doc Is Nothing
		If doc.HasItem("ServerName") Then
			curServer = doc.GetItemValue("ServerName")(0)
			Call ReplicateWithOneServer(db, curServer,nlog)
		End If
		Set doc = v.GetNextDocument(doc)
	Loop
	
Exit_ReplicateLocalDB:
	Exit Sub
	
Err_ReplicateLocalDB:
	Call nlog.LogError(Err,Error$ & " in ReplicateLocalDB line " & Cstr(Erl))
	Resume Exit_ReplicateLocalDB
	
End Sub

