Icon Backing Up and Restoring Databases

Introduction
Backing up and restoring databases is accomplished through the TDBISAMDatabase Backup, BackupInfo, and Restore methods. The properties used by the Backup, BackupInfo, and Restore methods include the Connected,
DatabaseName, Directory, and RemoteDatabase properties. The OnBackupProgress, OnBackupLog, OnRestoreProgress, and OnRestoreLog events can be used to track the progress of and log messages about the backup or restore operation. Backing up a database copies all or some of the tables within the database to a compressed or uncompressed backup file. Restoring a database copies all or some of the tables in a compressed or uncompressed backup file into the database, overwriting any tables with the same names that already exist in the database.

Backing Up a Database
To backup a database you must specify the DatabaseName and Directory or RemoteDatabase properties of the TDBISAMDatabase component, set the Connected property to True, and then call the Backup method. If you are backing up a database from a local session then you will specify the Directory property. If you are backing up a database from a remote session then you will specify the RemoteDatabase property. The TDBISAMDatabase component must be open when this method is called. If the TDBISAMDatabase component is closed an exception will be raised.

Information When the backup executes, it obtains a read lock for the entire database that prevents any sessions from performing any writes to any of the tables in the database until the backup completes. However, since the execution of this method is quite fast the time during which the tables cannot be changed is usually pretty small. To ensure that the database is available as much as possible for updating, it is recommended that you backup the tables to a file on a hard drive and then copy the file to a CD, DVD, or other slower backup device outside of the scope of the database being locked.

The following example shows how to backup a local database using the Backup method:

The local database has the following tables:

Table Name
-------------------------------------
Customers
Orders
Items

var
   TablesToBackup: TStrings;
begin
   TablesToBackup:=TStringList.Create;
   try
      with MyDatabase do
         begin
         DatabaseName:='MyDatabase';
         Directory:='d:\temp';
         with TablesToBackup do
            begin
            Add('Customers');
            Add('Orders');
            Add('Items');
            end;
         if Backup('d:\temp\'+
                   StringReplace(DateToStr(Date),
                         '/','',[rfReplaceAll])+'.bkp',
                   'Daily Backup for '+DateToStr(Date),6,
                   TablesToBackup) then
            ShowMessage('Backup was successful')
         else
            ShowMessage('Backup failed');
         end;
   finally
      TablesToBackup.Free;
   end;
end;

Information Remote databases can only reference backup files that are accessible from the database server on which the database resides. You must specify the path to the backup file in a form that the database server can use to open the file.

Tracking the Backup Progress
To take care of tracking the progress of the backup we have provided the OnBackupProgress and OnBackupLog events within the TDBISAMDatabase component. The OnBackupProgress event will report the progress of the backup operation and the OnBackupLog event will report any log messages regarding the backup operation.

Retrieving Information from a Backup File
To retrieve information from a backup file you must specify the DatabaseName and Directory or RemoteDatabase properties of the TDBISAMDatabase component, set the Connected property to True, and then call the BackupInfo method. If you are retrieving information from a backup file from a local session then you will specify the Directory property. If you are retrieving information from a backup file from a remote session then you will specify the RemoteDatabase property. The TDBISAMDatabase component must be open when this method is called. If the TDBISAMDatabase component is closed an exception will be raised.

Information Remote databases can only reference backup files that are accessible from the database server on which the database resides. You must specify the path to the backup file in a form that the database server can use to open the file.

Restoring a Database
To restore tables to a database you must specify the DatabaseName and Directory or RemoteDatabase properties of the TDBISAMDatabase component, set the Connected property to True, and then call the Restore method. If you are restoring tables to a database from a local session then you will specify the Directory property. If you are restoring tables to a database from a remote session then you will specify the RemoteDatabase property.

Information The Restore method overwrites any existing tables with names that are the same as those specified in this parameter. You should be very careful when using this method with an existing database to prevent loss of data.

The TDBISAMDatabase component must be open when this method is called. If the TDBISAMDatabase component is closed an exception will be raised.

Information When the restore executes, it obtains a write lock for the entire database that prevents any sessions from performing any reads or writes from or to any of the tables in the database until the restore completes. However, since the execution of this method is quite fast the time during which the tables cannot be accessed is usually pretty small.

The following example shows how to restore a table to a local database using the Restore method:

The local database has the following tables:

Table Name
-------------------------------------
Customers
Orders
Items

var
   TablesToRestore: TStrings;
begin
   TablesToRestore:=TStringList.Create;
   try
      with MyDatabase do
         begin
         DatabaseName:='MyDatabase';
         Directory:='d:\temp';
         with TablesToRestore do
            Add('Customers');
         if Restore('d:\temp\'+
                   StringReplace(DateToStr(Date),
                         '/','',[rfReplaceAll])+'.bkp',
                   TablesToRestore) then
            ShowMessage('Restore was successful')
         else
            ShowMessage('Restore failed');
         end;
   finally
      TablesToRestore.Free;
   end;
end;

Information Remote databases can only reference backup files that are accessible from the database server on which the database resides. You must specify the path to the backup file in a form that the database server can use to open the file.

Tracking the Restore Progress
To take care of tracking the progress of the restore we have provided the OnRestoreProgress and OnRestoreLog events within the TDBISAMDatabase component. The OnRestoreProgress event will report the progress of the restore operation and the OnRestoreLog event will report any log messages regarding the restore operation.
Image