Restoring Databases

From InterBase

Go Up to Backing up and Restoring Databases

TIBRestoreService contains many properties and methods to allow you to build a restore component into your application. Only the SYSDBA user or the database owner may use the TIBRestoreService to overwrite an existing database.

The username and password used to connect to the TIBRestoreService will be used to connect to the database for restore.

Setting the Database Cache Size

Use the PageBuffers property to set the cache size for the restored database. The default is 2048 buffer pages in the database cache. To change the database cache size, set it in the Object Inspector or in code:

PageBuffers := 3000

Setting the Page Size

InterBase supports database page sizes of 1024, 2048, 4096, and 8192 bytes. By default, the database will be restored with the page size with which it was created. To change the page size, you can set it in the Object Inspector or in code:

PageSize := 4096;

Changing the page size can improve database performance, depending on the data type size, row length, and so forth. For a discussion of how page size affects performance, see Page size in the Operations Guide.

Setting the Restore Options

The Options property allows you to build restore options of type TRestoreOption into your application. The restore options are:

TIBRestoreService options
Option Meaning

DeactivateIndex

Do not build indexes during restore

NoShadow

Do not recreate shadow files during restore

NoValidity

Do not enforce validity conditions (for example, NOT NULL) during restore

OneRelationATime

Commit after completing a restore of each table

Replace

Replace database if one exists

Create

Restore but do not overwrite an existing database

UseAllSpace

Do not reserve 20% of each datapage for future record versions; useful for read-only databases

Displaying Restore Output

Set the Verbose property to True to display the restore output to a data control, such as a Memo component.

Setting up a Restore Component

To set up a simple restore component:

  1. Drop a TIBRestoreService component on a Delphi form.
  2. Drop Button and Memo components on the form.
  3. Enter the name and path of the database to be restored in the DatabaseName field and the name and path of the database backup file in the BackupFile string field of the Object Inspector, or double click on the button and set the properties in code:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    with IBRestoreService1 do
    begin
     DatabaseName.Add('employee.gdb');
     BackupFile.Add('c:\temp\employee1.gbk');
       end;
    
  4. Attach to the service manager as described in Attaching to a Service Manager, or set the properties in code:
    begin
    with IBRestoreService1 do
    begin
     ServerName := 'Poulet';
      LoginPrompt := False;
      Params.Add('user_name=sysdba');
      Params.Add('password=masterkey');
      Active := True;
        end;
    
  5. Set any other options in the Object inspector (or set them in code), and then start the restore service with the ServiceStart method. The final code for a restore application that displays verbose restore output in a Memo component might look like this:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    with IBRestoreService1 do
    begin
    ServerName := 'Poulet';
    LoginPrompt := False;
    Params.Add('user_name=sysdba');
    Params.Add('password=masterkey');
    Active := True;
    try
    Verbose := True;
    Options := [Replace, UseAllSpace];
    PageBuffers := 3000;
    PageSize := 4096;
    DatabaseName.Add('c:\InterBase6\tutorial\tutorial.ib');
          BackupFile.Add('c:\InterBase6\tutorial\backups\tutor5.gbk');
          ServiceStart;
    While not Eof do
    Memo1.Lines.Add(GetNextLine);
    finally
    Active := False;
    end;
    end;
    end;
    

Restoring a Database from Multiple Backup Files

InterBase allows you to restore a database from multiple files. The following code example shows how to do this.

procedure TForm1.Button3Click(Sender: TObject);
begin
with IBRestoreService1 do
begin
ServerName := 'Poulet';
LoginPrompt := False;
Params.Add('user_name=sysdba');
Params.Add('password=masterkey');
Active := True;
try
Verbose := True;
Options := [Replace, UseAllSpace];
PageBuffers := 3000;
PageSize := 4096;
BackupFile.Add('c:\temp\employee1.gbk');
BackupFile.Add('c:\temp\employee2.gbk');
BackupFile.Add('c:\temp\employee3.gbk');
DatabaseName.Add('employee.gdb');
ServiceStart;
While not Eof do
Memo1.Lines.Add(GetNextLine);
finally
Active := False;
end;
end;
end;

Restoring a Database to Multiple Files

You might want to restore a database to multiple files to distribute it among different disks, which provides more flexibility in allocating system resources. The following code example shows how to do this.

procedure TForm1.Button2Click(Sender: TObject);
begin
with IBRestoreService1 do
begin
ServerName := 'Poulet';
LoginPrompt := False;
Params.Add('user_name=sysdba');
Params.Add('password=masterkey');
Active := True;
try
Verbose := True;
Options := [Replace, UseAllSpace];
PageBuffers := 3000;
PageSize := 4096;
BackupFile.Add('c:\temp\employee1.gbk');
DatabaseName.Add('c:\temp\employee2.ib = 2048');
DatabaseName.Add('c:\temp\employee3.ib = 2048');
DatabaseName.Add('c:\temp\employee4.ib');
ServiceStart;
While not Eof do
Memo1.Lines.Add(GetNextLine);
finally
Active := False;
end;
end;
end;

Advance To: