tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Doing Firebird's nbackup database "fixup" from .NET

Published 4 Feb 2016 in .NET and Firebird

The nbackup tool and more importantly the alter database [begin|end] backup commands are a great way to do a quick backup of Firebird database. You just copy the file (or files if you’re using database splitting) and you’re done. The small problem is with restore.

When you copy the backup to where it should be, Firebird will deny using it. Because the database is actually in “backup mode” (it was in that state as you copied it) and it’s missing so-called “delta” file. The solution is to call nbackup -F. But what if you don’t have nbackup available? Currently there’s no API to do that. But nothing is lost.

The nbackup is just physically accessing the file and flipping the state. Can that be done in .NET purely? Of course it can. Comparing the two files before and after “fixup” there’s a change at offset 0x00002B. So the code just needs to do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public bool Fixup(string databaseFile)
{
    try
    {
        using (var file = File.Open(databaseFile, FileMode.Open))
        {
            file.Position = 0x00002B;
            if (file.ReadByte() == 0x05)
            {
                file.Position--;
                file.WriteByte(0x01);
            }
        }
        return true;
    }
    catch (IOException)
    {
        return false;
    }
}

The code is first checking what’s on above mentioned offset. If the file looks locked it flips the state to unlocked.

It’s very raw approach. There’s no other checking whether the file is really Firebird database or whether there isn’t something else around. Just repeating what the nbackup does (from outside view). Use it at your own risk.