Search code examples
pythonfirebirdrestorefdb

How to restore a Firebird database in a Python script using FDB?


Firebird 3.0.4 is installed and Python v3.7.7 on a Windows10 64 bits system.

I can restore a database with the following command:

gbak.exe -r -USER user -PASSWORD password database.fdk database.fdb

I would like to do the same using fdb (Firebird Embedded) in a Python script but it does not work!

conn = fdb.services.connect(host='localhost', user='user', password='password', fb_library_name=API)
conn.restore(database.fbk, database.fdb)
restore_report = conn.readlines()

----> 1 conn = fdb.services.connect(host='localhost', user='user', password='password', fb_library_name=API)
2 conn.restore(database.fbk, database.fdb)
3 restore_report = con.readlines()
4 restore_report

TypeError: connect() got an unexpected keyword argument 'fb_library_name'

conn = fdb.services.connect(host='localhost', user='user', password='password')
conn.restore(database.fbk, database.fdb)
restore_report = conn.readlines()

DatabaseError: ('Services/isc_service_attach:\n- SQLCODE: -902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)


Solution

  • Assuming you want to use Firebird Embedded (judging by your previous question), the following works for me:

    import fdb
    
    fdb.load_api('C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
    
    def report_progress(line):
        print(line)
    
    svc = fdb.services.connect(user='sysdba', password='masterkey')
    svc.restore('c:/db/somedatabase.fbk', 'c:/db/somedatabase.fdb', callback=report_progress)
    

    That is, explicitly load the fbclient.dll associated with Firebird embedded (see also Firebird connection on a local database impossible within a Python script), do not specify a hostname in the fdb.services.connect so it will use the Firebird Embedded service manager instead of trying to connect to the localhost Firebird Server (which isn't running in your case).

    Instead of the callback I use to report the progress, you can also use the readlines() or wait() methods.