tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Doing Firebird’s nbackup database “fixup” from .NET

4 Feb 2016 2 mins .NET, 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.

Version for Firebird 3.

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.

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.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.