Subject | Using TIBBackup and TIBRestore |
---|---|
Author | Andrew Meyers |
Post date | 2002-10-23T14:54:35Z |
I am looking for a way to effectively use the TIBBackup and
TIBRestore objects to compact my database in a Delphi application. I
often times get an exception after the database has been backed up,
during the restore, saying the backup file is unavailable. I have
provided the code for a recursive function that has worked most of
the time, which traps the exception, an increments a waiting period
before the database is restored. I am, however, seeking a more
fullproof method of accomplishing a backup and restore that would
work all the time. Any input would be greatly appreciated.
//////////////////////////////////////////////////////////////////////
The initial call to this function would be as follows:
CompactDatabase(myDBFileName,5000,0);
Here is the function itself:
//////////////////////////////////////////////////////////////////////
function TDBIMainForm.CompactDatabase(const databasefilename :
String; const WaitTime, RecurseDepth : Integer) : boolean;
var
backupfilename : string;
RestoreComplete : Boolean;
begin
backupfilename := copy(databasefilename,1,length(databasefilename)-
length(extractfileext(databasefilename)))+'.gbk';
IBBackupService1.ServerName := 'Project';
IBBackupService1.LoginPrompt := False;
IBBackupService1.Params.Add('user_name=SYSDBA');
IBBackupService1.Params.Add('password=masterkey');
IBBackupService1.Active := True;
try
IBBackupService1.Verbose := false;
IBBackupService1.Options := [NonTransportable, IgnoreLimbo];
IBBackupService1.DatabaseName := databasefilename;
IBBackupService1.BackupFile.Clear;
IBBackupService1.BackupFile.Add(backupfilename);
IBBackupService1.ServiceStart;
finally
IBBackupService1.Active := False;
end;
IBRestoreService1.ServerName := 'Project';
IBRestoreService1.LoginPrompt := False;
IBRestoreService1.Params.Add('user_name=SYSDBA');
IBRestoreService1.Params.Add('password=masterkey');
IBRestoreService1.Active := True;
try
IBRestoreService1.Verbose := false;
IBRestoreService1.Options := [Replace, UseAllSpace];
IBRestoreService1.PageBuffers := 3000;
IBRestoreService1.PageSize := 4096;
IBRestoreService1.DatabaseName.Clear;
IBRestoreService1.DatabaseName.Add(DatabaseFileName);
IBRestoreService1.BackupFile.Clear;
IBRestoreService1.BackupFile.Add(backupfilename);
Sleep(WaitTime);
RestoreComplete := False;
try
IBRestoreService1.ServiceStart;
RestoreComplete := True;
except on e: exception do
RestoreComplete := False;
end;
finally
IBRestoreService1.Active := False;
end;
if RestoreComplete then
Result := True
else if RecurseDepth < 5 then
Result := CompactDatabase
(databasefilename,WaitTime*2,RecurseDepth + 1)
else
Result := False;
end;
TIBRestore objects to compact my database in a Delphi application. I
often times get an exception after the database has been backed up,
during the restore, saying the backup file is unavailable. I have
provided the code for a recursive function that has worked most of
the time, which traps the exception, an increments a waiting period
before the database is restored. I am, however, seeking a more
fullproof method of accomplishing a backup and restore that would
work all the time. Any input would be greatly appreciated.
//////////////////////////////////////////////////////////////////////
The initial call to this function would be as follows:
CompactDatabase(myDBFileName,5000,0);
Here is the function itself:
//////////////////////////////////////////////////////////////////////
function TDBIMainForm.CompactDatabase(const databasefilename :
String; const WaitTime, RecurseDepth : Integer) : boolean;
var
backupfilename : string;
RestoreComplete : Boolean;
begin
backupfilename := copy(databasefilename,1,length(databasefilename)-
length(extractfileext(databasefilename)))+'.gbk';
IBBackupService1.ServerName := 'Project';
IBBackupService1.LoginPrompt := False;
IBBackupService1.Params.Add('user_name=SYSDBA');
IBBackupService1.Params.Add('password=masterkey');
IBBackupService1.Active := True;
try
IBBackupService1.Verbose := false;
IBBackupService1.Options := [NonTransportable, IgnoreLimbo];
IBBackupService1.DatabaseName := databasefilename;
IBBackupService1.BackupFile.Clear;
IBBackupService1.BackupFile.Add(backupfilename);
IBBackupService1.ServiceStart;
finally
IBBackupService1.Active := False;
end;
IBRestoreService1.ServerName := 'Project';
IBRestoreService1.LoginPrompt := False;
IBRestoreService1.Params.Add('user_name=SYSDBA');
IBRestoreService1.Params.Add('password=masterkey');
IBRestoreService1.Active := True;
try
IBRestoreService1.Verbose := false;
IBRestoreService1.Options := [Replace, UseAllSpace];
IBRestoreService1.PageBuffers := 3000;
IBRestoreService1.PageSize := 4096;
IBRestoreService1.DatabaseName.Clear;
IBRestoreService1.DatabaseName.Add(DatabaseFileName);
IBRestoreService1.BackupFile.Clear;
IBRestoreService1.BackupFile.Add(backupfilename);
Sleep(WaitTime);
RestoreComplete := False;
try
IBRestoreService1.ServiceStart;
RestoreComplete := True;
except on e: exception do
RestoreComplete := False;
end;
finally
IBRestoreService1.Active := False;
end;
if RestoreComplete then
Result := True
else if RecurseDepth < 5 then
Result := CompactDatabase
(databasefilename,WaitTime*2,RecurseDepth + 1)
else
Result := False;
end;