Sunday 6 March 2011

Backup Database

Useful code to backup the current database.  It goes on to the current server, into the current directory, into a subfolder called "backup".  The filename is updated to include the current date.  There is an option to copy the documents, or just the design.

Dim s As New NotesSession
Dim newdb As New NotesDatabase ("", "")
Dim db As NotesDatabase
Dim todaydate As New NotesDateTime ("")
Dim strtodaydate As String
Dim filepath As String
Dim newfilepath As String
Dim filename As String
Dim strmsgbox As String
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim server As String
Dim count As String
Dim doccount As String
Dim title As String


Set db = s.CurrentDatabase
title = db.title
server = db.Server
count = "1"
Set todaydate = New NotesDateTime (Today)
' get the date as a string
strtodaydate = todaydate.Dateonly
' replace all slashes "/" in date formatting
strtodaydate = Replace (strtodaydate,"/","")
filepath = db.filepath
' removes the filename from filepath
filepath = Replace(filepath,db.filename,"")
filename = db.filename
' builds the same filepath as original DB, adding an extra folder level called
newfilepath = filepath + "Backup\" + strtodaydate + "_" + filename
' Ask user if they would like to copy all documents from the database
userprompt = MessageBox ("Do you want to copy all documents from this database?", 4 + 32, "Copy Documents?")
' If user selects yes, copy documents, else do not
If userprompt = 6 Then
' run the backup and copy documents
Set newdb = db.CreateCopy (server, newfilepath)
newdb.title = strtodaydate + " " + title + " Backup"

Set dc = db.AllDocuments
Set doc=dc.GetFirstDocument
doccount = dc.count
While Not doc Is Nothing
Call doc.CopyToDatabase(newdb)
Print "Copying document " + count + " of " + doccount + " documents into " + server + "\" + newfilepath
' add 1 to count
count = count + 1
Set doc=dc.GetNextDocument(doc)
Wend
Else
' run the backup without copy
Set newdb = db.CreateCopy (server, newfilepath)
newdb.title = strtodaydate + " " + title + " Backup"
End If

No comments:

Post a Comment