lightningd / plugins

Community curated plugins for core-lightning
BSD 3-Clause "New" or "Revised" License
269 stars 129 forks source link

backup: cli tool failed to restore backup #158

Closed marsu-dev closed 3 years ago

marsu-dev commented 3 years ago

Setup:

SQLite

Steps to reproduce:

backup-cli restore file:///var/lib/lightning/backup/testnet.db /var/lib/lightning/testnet/lightningd.sqlite3

Error:

The lightningd.sqlite3 file is not restored, backup-cli crashed with error:

unrecognized token: "1891733WHERE"

Traceback:

662it [01:24,  7.85it/s]
Traceback (most recent call last):
  File "/usr/local/libexec/c-lightning/plugins/backup/backup-cli", line 76, in <module>
    cli()
  File "/usr/lib/python3.8/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.8/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.8/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.8/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.8/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/libexec/c-lightning/plugins/backup/backup-cli", line 64, in restore
    backend.restore(restore_destination)
  File "/usr/local/libexec/c-lightning/plugins/backup/backup.py", line 122, in restore
    self._restore_transaction(c.transaction)
  File "/usr/local/libexec/c-lightning/plugins/backup/backup.py", line 101, in _restore_transaction
    cur.execute(q.decode('UTF-8'))
sqlite3.OperationalError: unrecognized token: "1891733WHERE"
m-schmoock commented 3 years ago

@michel-foucault

cdecker commented 3 years ago

This is the result of an error that is fixed in c-lightning with #4090.

The problem was that when expanding the DB statement in order to pass it to the db_write hook we would expand them without checking that they are still valid SQL.

However, if the backup was created before the fix was applied they might still have incorrectly expanded statements. So we can either build a tool to update the queries contained in the backup (iterate through the backup file and copy over statements to a new file, then swap it into place with the original file) or we rewrite on-the-fly while restoring.

FWIW, there is just a space missing between 1891733 and the WHERE, so depending on the size of the file this could be done manually as well. @michel-foucault if you're happy to share the backup (for testnet that should not be much of a concern, and we can't actually move coins without hsm_secret) we can build the rewrite tool and provide a durable solution for you :-)

cdecker commented 3 years ago

I added a fix in the form of PR #172 which will rewrite the statements that have been expanded wrongly on the fly, so once you update to that branch backup-cli restore should work out of the box.

@michel-foucault: could you try that branch and see if it fixes the issue?

marsu-dev commented 3 years ago

Great, Thanks for the fix.

Since I have used Postgres backend, with multiple database for multiple nodes, that cover my needs for the backup in production.

I have the backup and I will try to upgrade the code and restore from the backup (w & w/o patching it) for report.