Database backup, restore, compact

Note: 'These codes work if the update of the database is immediate: if the primary key is a counter field see http://firebird-vbnet.blogspot.com/2007/02/solved-issue-of-immediate-update-in.html

Imports FirebirdSql.Data.FirebirdClient
Imports FirebirdSql.Data.Services

Backup of a database: code

Dim n As FirebirdSql.Data.Services.FbBackup = New FirebirdSql.Data.Services.FbBackup()
Dim fl As FirebirdSql.Data.Services.FbBackupFile = New FirebirdSql.Data.Services.FbBackupFile(My.Application.Info.DirectoryPath & "\mydb.gdk", FileLen(My.Application.Info.DirectoryPath & "\mydb.gdb"))
n.BackupFiles.Add(fl)
n.ConnectionString = "servertype=0;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\mydb.gdb"
n.Execute()

Restore a database: code

Dim n1 As FirebirdSql.Data.Services.FbRestore = New FirebirdSql.Data.Services.FbRestore()
Dim fl1 As FirebirdSql.Data.Services.FbBackupFile = New

'These statements are necessary because on restart of the computer a runtime exception appear
'The try-catch-finally block, otherwise, seems not work

On Error GoTo 1
On Error Resume Next


Dim cs As FbConnectionStringBuilder = New FbConnectionStringBuilder
Dim restoreSvc As FbRestore = New FbRestore
cs.UserID = "SYSDBA"
cs.Password = "masterkey"
cs.Database = My.Application.Info.DirectoryPath & "\mydb.gdb"
restoreSvc.ConnectionString() = cs.ToString
restoreSvc.BackupFiles.Add(New _
FbBackupFile(My.Application.Info.DirectoryPath & "\mydb.gdk",FileLen(My.Application.Info.DirectoryPath & "\mydb.gdb")))
restoreSvc.Verbose = True
restoreSvc.PageSize = 4096
restoreSvc.PageBuffers = 2048
restoreSvc.Options = FbRestoreFlags.Replace
FbConnection.ClearAllPools()
restoreSvc.Execute()
exit sub
1:
restoreSvc.Execute()


Compact a database: code

Dim n As FirebirdSql.Data.Services.FbBackup = New FirebirdSql.Data.Services.FbBackup()
Dim fl As FirebirdSql.Data.Services.FbBackupFile = New FirebirdSql.Data.Services.FbBackupFile(My.Application.Info.DirectoryPath & "\mydb.gdk", FileLen(My.Application.Info.DirectoryPath & "\mydb.gdb"))
n.BackupFiles.Add(fl)
n.ConnectionString = "servertype=0;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\mydb.gdb"
n.Execute()
'These statements are necessary because on restart of the computer a runtime exception appear
'The try-catch-finally block, otherwise, seems not work
On Error GoTo 1
On Error Resume Next

Dim cs As FbConnectionStringBuilder = New FbConnectionStringBuilder
Dim restoreSvc As FbRestore = New FbRestore
cs.UserID = "SYSDBA"
cs.Password = "masterkey"
cs.Database = My.Application.Info.DirectoryPath & "\mydb.gdb"
restoreSvc.ConnectionString() = cs.ToString
restoreSvc.BackupFiles.Add(New _
FbBackupFile(My.Application.Info.DirectoryPath & "\mydb.gdk", FileLen(My.Application.Info.DirectoryPath & "\mydb.gdb")))
restoreSvc.Verbose = True
restoreSvc.PageSize = 4096
restoreSvc.PageBuffers = 2048
restoreSvc.Options = FbRestoreFlags.Replace
FbConnection.ClearAllPools()
restoreSvc.Execute()
exit sub
1:
restoreSvc.Execute()

2 comments:

Anonymous said...

Where is the FileLen function?

Giovanni S said...

Filelenght parameter is required by
FbBackupFile method

Newer Post Older Post