I have the requirement to create replicas from a workstation on a server if the database has a replica on the primary server. So I coded this:
Sub Initialize
Dim SourceServerName As String
Dim TargetServerName As String
Dim db As NotesDatabase
Dim curDB As NotesDatabase
Dim nlog As NotesLog
Dim dbDir As NotesDbDirectory
Dim doc As NotesDocument
On Error Goto Err_ReplicateAll
Dim s As New NotesSession
Dim ws As New NotesUIWorkspace
Set db = s.CurrentDatabase
Set nlog = New NotesLog("ReplicateToNewServer")
nlog.LogActions = True
nlog.LogErrors = True
Call nlog.OpenNotesLog(db.Server,db.FilePath) 'We use ourselfes for the log
Set doc = New NotesDocument(db)
If Not ws.DialogBox("SelectServers",True,True,False,False,False,False,"Select Servers",doc,True,False,True) Then
Goto Exit_ReplicateAll
End If
SourceServerName = doc.SourceServer(0)
TargetServerName = doc.TargetServer(0)
If SourceServerName = "" Or TargetServerName = "" Then
Msgbox "You need two server names!"
Goto Exit_ReplicateAll
End If
Set dbDir = New NotesDbDirectory("")
Set curDB = dbDir.GetFirstDatabase(REPLICA_CANDIDATE)
Do Until curDB Is Nothing
Call CheckAndCreateReplica(curDB,SourceServerName,TargetServerName,nLog)
Set curDB = dbDir.GetNextDatabase
Loop
Exit_ReplicateAll:
On Error Resume Next
If Not nlog Is Nothing Then
Call nlog.Close
End If
Exit Sub
Err_ReplicateAll:
If Not nlog Is Nothing Then
Call nlog.LogError(Err,Error$+" inReplicate to new Server")
End If
Resume Exit_ReplicateAll
End Sub
Sub CheckAndCreateReplica(curDB As NotesDatabase, SourceServerName As String, TargetServerName As String, nLog As NotesLog)
Dim sourceDB As NotesDatabase
Dim targetDB As NotesDatabase
On Error Goto Err_CheckAndCreateReplica
If Not curDB.IsOpen Then
Call curDB.Open("","")
End If
'Check for the file
Set sourceDB = New NotesDatabase("","")
If Not sourceDB.OpenByReplicaID(SourceServerName,curDB.ReplicaID) Then
Call Nlog.LogAction("No replica found for "+curDB.FilePath)
Goto Exit_CheckAndCreateReplica
End If
'We now replicate with the source server. If that fails the routine bails out already!
'This also makes sure we have the latest data
Print "Replicating "+curDB.FilePath
Call curDB.Replicate(SourceServerName)
'Now we got the path... step one is to check for a replicaID already existing
Set targetDB = New NotesDatabase("","")
If targetDB.OpenByReplicaID(SourceServerName,curDB.ReplicaID) Then
'We already have a replica, so we just replicate!
Call curDB.Replicate(TargetServerName)
Call nLog.LogAction("Replica updated "+curDB.FilePath)
Print "Replica updated "+curDB.FilePath
Else
'We need to check for the file name
If targetDB.Open(TargetServerName,sourceDB.FilePath) Then
'There is a DB on that path but not the replica... VERY BAD!!!
Call nlog.LogAction("DB "+curDB.FilePath+" has conflicting filepath on "+TargetServerName)
Print "DB "+curDB.FilePath+" has conflicting filepath on "+TargetServerName
Goto Exit_CheckAndCreateReplica
Else
'Hurray...
'We need to create a replica!!!
Call curDB.CreateReplica(TargetServerName,sourceDB.FilePath)
Call nLog.LogAction("Replica created "+sourceDB.FilePath+" for "+curDB.FilePath)
'Just to be sure -- a replicaction
Call curDB.Replicate(TargetServerName)
Call nLog.LogAction("Replication for new db completed "+sourceDB.FilePath+" for "+curDB.FilePath)
End If
End If
'If we got here it worked
Exit_CheckAndCreateReplica:
Exit Sub
Err_CheckAndCreateReplica:
Call Nlog.LogError(Err,Error$+" with "+curDB.FilePath+" in CheckAndCreateReplica")
Print Error$+" with "+curDB.FilePath+" in CheckAndCreateReplica"
Resume Exit_CheckAndCreateReplica
End Sub
Looks great… only…
Call curDB.CreateReplica(TargetServerName,sourceDB.FilePath)
doesn’t throw an error but also doesn’t create the replica, so the following replicate command fails.
It is a timing issue?
stw