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.
Not a proper connection string with a multi-client database of one or more users (in
versions prior to 6.x ) When connecting via TCP/IP, the path to database must be pointed
server name: drive:/path/databasename /for servers on UNIX platform servername:
/path/databasename /, according to NETBEUI protocol
\\servername\drive:\path\databasename. Even when connecting database from the
computer, on which database is located and server is running one should use 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.
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.
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.
Exceeding size restriction of database file (not a database!). For pre-InterRase 6 versions
and some InterBase 6 beta database file limit is 4Gb, for InterBase 6.5 and all releases of
Firebird (1.0, 1.5, 2.0, 2.1) - 32Tb. When approaching database size to a limit value, an
additional file must be created.
Exhaustion of free disk space when working with database.
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 thefollowing way (look table
1).
Version Page size=1024 Page size=2048 Page size=4096 Page size=8192
Pre 6
248
504
1016
2040
6.0.x 124
257
508
102
Table 1: Critical number of generators in early InterBase versions
For all Borland InterBase servers exceeding of permissible number of
transactions without executing backup/restore. One can know the number of
transactions happened in database from the time of last creation by invoking the utility gstat
with a key h- parameter NEXT TRANSACTION ID will be desired
quantity of transactions. According to Ann W.Harrison the critical number of
transactions depends on page size, and has the following values (look table 2):
Database page size Critical number of transactions
1024 byte
2048 byte
4096 byte
8192 byte
to know more about backup then look the chapter Backup and restore). Its the most
trusted way against database corruption. Only backup gives 100% guarantee of database
security. As its described above, as the result of backup we can get a useless copy (a copy
that cant be restored), thats why restoring a base from the copy mustnt be performed by
writing over the script and backup must be done according to definite rules. Firstly, backup
must be executed as more often as possible, secondly it must be serial and thirdly, backup
copies must be checked for restoring capability.
Often backup means that its necessary to make a backup copy rather often, for example
once in twenty-four hours. The less data period is between the database backup, the fewer
data will be lost as a result of fault. Sequence of backup means that the number of backups
must increase and must be stored at least for a week. If there is a possibility, its necessary
to write backups to special devices like streamer, but if there is not just copy them to the
other computer. The history of backup copies will help to discover hidden corruptions and
cope with the error that arose long ago and showed up unexpectedly. One has to check,
whether it is possible to restore the received backup without errors or not. It can be checked
only in one way - through the test restore process. It should be said that restore process takes
3 times more time than backup, and its difficult to execute restore validation every dayfor
large databases, because it may interrupt the users work for a few ho urs (nightbreak may
not be enough).
It wou ld be better if big organizations didnt save on matches and left one computer for
these aims.
In this case, if server must work with serious load 24 hours 7 days a week, we canuse SHADOW
mechanism for taking snapshots from database and further backup operations from the
immediate copy. Backup process and database restoring is described in detail in chapter
Backup and restore. When creatinga backup and then restoring database from it, recreation
of all data in database is happening. This process (backup/restore or b/r) contributes to the
correction of most non-fatal errorsin database, connected with hard disk corruptions,
detecting problems with integrityin database, cleaning database from garbage (old versions
and fragments of records, incomplete transactions), decreasing database size considerably.
Regular b/r is a guarantee of InterBase database security. If database is working, then it is
recommended to execute b/r every week. To tell the truth, there are some illustrations
about InterBase databases that are intensively used for same years without backup/restore.
Nevertheless, to be on a safe side its desirable to perform this procedure, especially as it can
be easily automated (look chapter Backup)
If its impossible to perform backup/restore often for some reasons, then one can use the tool
gfix for checking and restoring database. gfix allows to check and remove many errors
without b/r.
2.8. Command line tool gfix
Command line tool gfix is used for checking and restoring database. Besides, gfix can also
execute various activities of database control: changing database dialect, setting and
canceling the mode read-only, setting cache size for a concrete database and also some
important functions (you can know about them in InterBase 6 Operations Guide [4]) gfix is
committed in a command line mode and has the following syntax:
Gfix [ options] db name
Options is a set of options for executing gfix , db name is a name of database over which
operations will be performed, defined by set of options. Table 3 represents options gfix
related to database repairing:
Option
Description
f[ull]
i[gnore]
m[end]
n[o_update]
pas[sword]
user
v[alidate]
-m[ode]
the data youve got in the external source. Its better to store data in script (almost all GUI
give this function), if only the table doesnt 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 youll have to write this trivial program especially for
data recovery aims.
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
wont be needed any more. Of course, if you dont have a structure of metadata, same to
the corrupted, its necessary to input a protocol of all operations that you are doing with a
damaged database-source.
If you dont 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 thats 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. Its 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 dont have a complete copy of metadata, you should
extract the texts of stored procedures, triggers, constraints and definition of indexes.
2.11. Restoring of hopeless database
In general, restoring of database can be very troublesome and difficult and thats why its
better to make a backup copy of database than restore the damaged data and whatever
happened, you shouldnt get despaired because a solution can be found in the most difficult
situations. And now well consider 2 cases.
The first case (a classic problem). A backup that cant be restored because of having NULL
values in the column with constraints NOT NULL (restore process was run over the working
file). The working file was erased and restore process was interrupted because of error. And
as a result of thoughtless actions we got a great number of useless data (that cant be
restored) instead of backup copy. But the solution was found. The programmer managed to
recollect what table and what column had constraints NOT NULL. Backup file was loaded to
hexadecimal editor. And a combination of bytes, corresponded to definition of this column,
was found there by searching. After innumerous experiments it turned out that constraint
NOT NULL adds 1 somewhere near the column name. In HEX-editor this 1 was corrected to
0 and backup copy was restored. After that case programmer memorized once and for all
how to execute backup process and restore.
The second case. The situation was catastrophic. Database corrupted on the phase of
extension because of lack of disk space. When increasing the database size, server creates
series of critically important pages (for example, transaction inventory page and page
inventory page, additional pages for RDB$Pages relation) and writes them down to the end of
database As a result, database didnt open either by administration facilities or by utility
GBAK. And when we tried to connect database, error message (Unexpected end of file)
appeared.
When we run utility gfix strange things were happening: The program was working in an
endless cycle. When gfix was working, server was writing errors to log (file InterBase log) with
high speed (around 100 Kb per second). As a result, log file filled all the free disk space very
quickly. We even had to write a program that 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. Its 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 thats 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 wasnt difficult because the sizeof
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 example, a
string like VARCHAR containing ABC string, it stores sequence of following values: string
length (2 bytes), in our case it is 0003, and then symbols themselves and then checksum. We
had to write analyzer of string as well as other database types that converted data from
hexadecimal format into ordinary view. We managed to extract up to 80% of information from
several critical tables using a manual method of analyzing database contents. Later on the
basis of experience Oleg Kulkov and Alexey Kovyazin, one of the authors of this book,
developed the utility InterBase Surgeon that performs direct access to database, bypassing
the InterBase engine and allows to read directly and interpret the data within InterBase
database in a proper way.
Using InterBase Surgeon, we manage to detect causes of corruption and restore up to 90% of
absolutely unrecoverable databases that cant be open by InterBase and restored by standard
methods.
You can download this program from official program site www.ib-aid.com.
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.
Copyright 2004 IBSurgeon Team. All rights reserved.