Professional Documents
Culture Documents
NOTICE: This document is the chapter from the book "The InterBase World" which was written by
Alexey Kovyazin and Serg Vostrikov.
The chapter from book "The InterBase World" devoted to the database repairing.
real pity.
Here we'd like to offer you the draft of one chapter of this book devoted to recovery
of InterBase/Firebird databases.
the same line renaming servername for localhost. One cannot use mapped drive
in the connection line. If you break one of these rules, server considers that it
works with different databases and database corruption is guaranteed.
4. File copy or other file access to database when server is running. The execution
of the command “shut-down” or disconnecting the users in usual way is not a
guarantee that server is doing nothing with the database, if sweep interval is not
set to “0”, garbage collection can be executed. Generally the garbage collection
is executed immediatelly after the last user disconnects from database. Usually it
takes several seconds, but if before it many DELETE or UPDATE operations
were committed, a process may be longer.
5. Using unstable InterBase server versions 5.1-.5.5.Borland Company officially
admitted that there were several errors in these servers and a steady upgrate 5.6
removed only after an output of certified InterBase 6 was in free-running mode
for all clients of servers 5.1-5.5 on its site.
6. Exceeding size restriction of database file. For most existing, at the moment of
writing of these lines, servers of UNIX platform it is 2 Gb, for windows NT/2000-4
Gb, but it is recommended to take 2 Gb. When approaching database size to a
limit value, an additional file must be created.
7. Exhaustion of free disk space when working with database.
8. For Borland InterBase servers versions under 6.0.1.6 - exceeding of restriction to
a number of generators according to Borland InterBase R & D defined in the
following way (look table 1).
Version Page size=1024 Page size=2048 Page size=4096 Page size=8192
Pre 6 248 504 1016 2040
It’s necessary that you know about some mistakes made by database developers
that can lead to impossibility of database recovery from a backup copy (*.gbk files
created by gbak program). First of all this is a careless use of constraints on
database level. Typical example is constraints NOT NULL. Let’s suppose that we
have a table filled with the number of records. Now we’ll add to this table using
ALTER TABLE command one more column and point that it mustn’t contain
non-defined values NULL. Something like this:
ALTER TABLE sometable Field/INTEGER NOT NULL
And in this case there will be no server’s error as it could be expected. This
metadata modification will be committed and we won’t receive any error or warning
message that creates an illusion of normality of this situation.
However, if we backup database and try to restore it from a backup copy, we’ll
receive an error message at the phase of restoring (because Nulls are inserted into
the column that has NOT NULL constraint, and the process of restoring will be
interrupted. (The important note provided by Craig Stuntz - with version InterBase
7.1 constraints are ignored by default during restore (this can be controlled by a
command-line switch) and nearly any non-corrupt backup can be restored. It's
always a good idea to do a test restore after making a backup, but this problem
should pretty much go away in version 7.1. ) This backup copy can’t be restored. If
restoring was directed to file having the same name as the existing database (during
restoring existing database working file was being rewritten) we’ll lose the whole
information.
It is connected with the fact that constraints NOT NULL are implemented by system
triggers that check only arriving data. During restoring the data from backup copy
are inserted into the empty just created tables - here we can find inadmissible
NULLs in the column with constraint NOT NULL.
Some developers consider that such InterBase behavior to be incorrect, but other
one will be unable to add a field with NOT NULL restriction to the database table
A question about required value by default and filling with it at the moment of
creation was widely discussed by Firebird architects, but wasn’t accepted because
of the fact that programmer is obviously going to fill it according to the algorithm,
rather complicated and maybe iterative. But there is no guarantee, whether he’ll be
able to distinguish the records ignored by previous iteration from unfilled records or
not.
The similar problem can be caused by garbage collection fault because of setting
not a correct path to database (the cause of corruption 3) at the time of connection
and file access to database files when server is working with it (the cause of
corruption 4) and records whole filled with Null can appear in some tables. It’s very
difficult to detect these records, because they don’t correspond to integrity control
restrictions, and operator Select just doesn’t see them, although they get into backup
copy. If it is impossible to restore for this reason, one should run gfix program (look
below), find and delete these records using non-indexed fields as search conditions,
after it retry to make a backup copy and restore database from it. In conclusion we
can say that there is great number of causes of database corruption and you should
always be ready for worst - that your database will be damaged for that or other
reason. You also must be ready to restore and save valuable information. And now
we’ll consider precautions that guarantee InterBase database security, as well as
methods of repairing damaged databases.
–w[rite] {sync | async} Option that turns on and off the mode
synchronous/asynchronous forced writes to
database. sync – to turn synchronous writes on
(FW ON); async –to turn asynchronous writes
on (FW OFF);
Table 1: gfix tool options for database restoring
There are some typical examples of using gfix:
gfix –w sync –user SYSDBA –pass masterkey firstbase.gdb
In this example we set for our test database firstbase.gdb synchronous writes mode
(FW ON). (Of course, it is useful before the corruption occurs). And below is the first
command that you should use to check database after the corruption occurs:
gfix –v –full –user SYSDBA –pass masterkey firstbase.gdb
In this example we start checking our test database (option –v) and indicate that
fragments of records must be checked as well (option -full). Of course, it is more
convenient to set various options for checking and restoring process by any GUI, but
we’ll consider the functions of database recovery using command line tools. These
tools are included to InterBase and you can be sure that their behavior will be the
same on all OS running InterBase. It is very important that they always be near.
Besides, the existing tools, allowing to execute database administrating from a
client’s computer use Services API for it, that isn’t supported by InterBase server
Classic architecture. That means you may use third party products with server’s
architecture SuperServer.
If there are some errors with backup, you should start it in another configuration:
gbak –b –v –ig -g –user SYSDBA –password corruptbase.gdb
corruptbase.gbk
Where option – g will switch off garbage collection during backup. If often helps to
solve a problem with backup.
Also it may be possible to make a backup of database, if before it we set database in
read-only mode. This mode prevents from writing any modifications to database and
sometimes helps to perform backup of damaged database. For setting database to
read-only mode, you should use the following command: gfix –m read _only
–user SYSDBA –password masterkey Disk:\Path\file.gdb
After it you should try again to perform database backup using the parameters given
above.
If backup was done successfully, you should restore the database from backup
copy. You should use the following command:
gbak –c –user SYSDBA –password masterkey Disk:\Path\backup.gbk
Disk:\Path\newbase,gdb
When you are restoring the database, you may have some problems, especially
when creating the indexes.
In this case options –inactive and -one_at_a_time should be added to restore
command. These options deactivate indexes in creating from database backup and
commit data confirmation for every table.
2.10. How you can try to extract the data from a corrupted database
It is possible that the operations given above will not lead to database recovery.
It means that database is seriously damaged or it cannot be restored as a single
whole, or a great number of efforts must be made for it is recovery. For example,
one can execute a modification of system metadata, use non-documented functions
and so on. It is a very hard, long-lasting and ungrateful work with doubtful chances
for success. And if it is possible, try to evade it and use other methods. If a damaged
database opens and allows to perform reading and modification operations with
some data, you should use this possibility and save the data by copying them to a
new base, and “ say god-bye” to the old one for good.
So, before transferring the data from the old database, it’s necessary to create a
abase destination. If database hasn’t been changed for a long time, then you can
use the old backup, from which metadata can be extracted for creating a database
destination. On a basis of these metadata one has to create a data destination and
start copying the data. The main task is to extract the data from a damaged
database. Then we’ll have to allocate the data in a new base, but it’s not very
difficult, even if we’ll have to restore database structure from memory.
When extracting data from tables, you should use the following algorithm of
operations:
1. At first you should try to execute SELECT* from table N. If it went normally you
could save the data you’ve got in the external source. It’s better to store data in
script (almost all GUI give this function), if only the table doesn’t contain
BLOB-fields. If there are BLOB-fields in the table, then data from them should be
saved to another database by client program that will play a role of a mediator.
Maybe you’ll have to write this trivial program especially for data recovery aims.
2. If you failed to retrieve all data, you should delete all the indexes and try again.
Virtually, indexes can be deleted from all the tables from the beginning of
restoring, because they won’t be needed any more. Of course, if you don’t have
a structure of metadata, same to the corrupted, it’s necessary to input a protocol
of all operations that you are doing with a damaged database-source.
3. If you don’t manage to read all the data from the table after deleting the indexes,
one can try to do range query by primary key. It means to choose definite range
of data. For example:
SELECT * FROM table N WHERE field_PK >=0 and field_PK <=10000
Field_PK here is a primary key.
InterBase has page data organization and that’s why range query of values may be
rather effective, although it seems to be something like shamanism.
Nevertheless it works because we can expel data from query from damaged pages
and read fortunately the other ones. You can recall our thesis that there is no
defined order of storing records in SQL. Really, nobody guarantees that not an
ordered query during restarts will return the records in the same order, but
nevertheless physical records are stored within the database in defined internal
order. It’s obviously that server will not mix the records just for abiding
SQL-standard. One can try to use this internal order extracting data from damaged
database (if you want to know more information about data pages and their
correlations, then look chapter “Structure of InterBase database”). Vitaliy Barmin,
one of the experienced Russian InterBase-developers reported that in this way he
managed to restore up to 98% information from unrecoverable database (there were
a great number of damaged pages). Thus, data from a damaged database must be
moved to a new database or into external sources like SQL-scripts. When you copy
the data, pay attention to generators values in damaged database (they must be
saved for restarting a proper work in new database. If you don’t have a complete
copy of metadata, you should extract the texts of stored procedures, triggers,
constraints and definition of indexes.
erased this log by timer. This process lasted for a long time – gfix was working for
more than 16 hours without any results.
Log was filled up with errors of the following view: “Page XXX doubly allocated”. In
starting InterBase sourses (in file val.#) there is a short description of this error. It
says that this error appears when the same data page is used twice.
It’s obviously that this error is a result of corruption of critically important pages.
As a result, after several days of unfortunate experiments attempts to restore the
data in standard ways were left. And that’s why we had to use low-level analysis of
data stored in damaged database.
Alexander Kozelskiy, a chief of Information technologies department East View
Publications Inc, is the author of the idea how to extract information from similar
unrecoverable databases.
The method of restoring that we got as a result of researches was based on the fact
that database has page organization and data from every table are collected by data
pages. Every data page contains identifier of the table for which it stores data. It was
especially important to restore data from several critical tables. There were data
from the similar tables, received from an old backup copy that worked perfectly and
could be a pattern. Database-pattern was loaded to editor of hexadecimal sources
and then we searched for the patterns of those data that interested us. These data
were copied to buffer in hexadecimal format and then remains of damaged database
were loaded to the editor. A sequence of bytes corresponded to the pattern was
found in damaged database, and page was analyzed (on which this sequence was
found).
At first we defined the beginning page, but it wasn’t difficult because the size of
database file is divisible by data page size. A number of current byte divided by page
size – 8192 bytes, approximates the result to integer (and got the number of current
page). Then multiplied the number of current page-by-page size and got the number
of byte corresponded to the beginning of current page. Having analyzed the header,
we defined the type of page (for pages with data the type is 5 – look file ods.h from
set of starting InterBase sources and also the chapter “The structure of InterBase
Database”) as well as identifier of necessary table.
Then a program was written, that analyzed the whole database, collected all the
pages for necessary table into one single piece and move it to file.
Thus, when we got the data we needed in first term, we started analyzing contents
of selected pages. InterBase is widely using data compression for saving place. For
3. Thanks
I'd like to tender thanks to all who help me to create this guide:
Craig Stuntz, Alexander Nevsky, Konstantin Sipachev, Tatjana Sipacheva and all
other kind and knowledgeable people of InterBase and Firebird comminity.
If you have any suggestions or questions about this chapter, please feel free
to email.
© 2002 AIexey Kovyazin, Serge Vostrikov.