From 4c146bf2a4bdeaeacaee8315af82d4b182833914 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 4 Apr 2020 16:20:46 +0200 Subject: [PATCH] backend: Use th backup URL in backup-cli and add snapshot type to FileBackend --- backup/backup-cli | 12 +++++++----- backup/backup.py | 19 ++++++++++++------- backup/test_backup.py | 31 ++++++++++++------------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/backup/backup-cli b/backup/backup-cli index 9206a2a..8eff4a5 100755 --- a/backup/backup-cli +++ b/backup/backup-cli @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from backup import FileBackend +from backup import FileBackend, get_backend import os import click import json @@ -7,10 +7,10 @@ import json @click.command() @click.argument("lightning-dir", type=click.Path(exists=True)) -@click.argument("backup-dir", type=click.Path(exists=True)) -def init(lightning_dir, backup_dir): - destination = 'file://' + os.path.join(backup_dir, 'backup.dbak') - backend = FileBackend(destination) +@click.argument("backend-url") +def init(lightning_dir, backend_url): + destination = backend_url + backend = get_backend(destination) backend.version, backend.prev_version = 0, 0 backend.offsets = [512, 0] backend.version_count = 0 @@ -23,6 +23,8 @@ def init(lightning_dir, backup_dir): 'backend_url': destination, })) + # TODO Take a snapshot + print("Initialized backup backend {destination}, you can now start c-lightning".format( destination=destination, )) diff --git a/backup/backup.py b/backup/backup.py index f722602..8e017a9 100755 --- a/backup/backup.py +++ b/backup/backup.py @@ -132,10 +132,11 @@ class FileBackend(Backend): with open(self.url.path, 'ab') as f: f.seek(self.offsets[0]) f.write(length) + f.write(typ) f.write(payload) self.prev_version, self.offsets[1] = self.version, self.offsets[0] self.version = entry.version - self.offsets[0] += 4 + len(payload) + self.offsets[0] += 5 + len(payload) self.write_metadata() return True @@ -163,6 +164,15 @@ def resolve_backend_class(backend_url): return backend_cl +def get_backend(destination): + backend_cl = resolve_backend_class(destination) + if backend_cl is None: + raise ValueError("No backend implementation found for {destination}".format( + destination=destination, + )) + return backend_cl(destination) + + def abort(reason: str) -> None: plugin.log(reason) plugin.rpc.stop() @@ -246,12 +256,7 @@ def on_init(options: Mapping[str, str], plugin: Plugin, **kwargs): if not plugin.db_path.startswith('sqlite3'): abort("The backup plugin only works with the sqlite3 database.") - # Let's initialize the backed. First we need to figure out which backend to use. - backend_cl = resolve_backend_class(destination) - if backend_cl is None: - abort("Could not find a backend for scheme {p.scheme}".format(p=p)) - - plugin.backend = backend_cl(destination) + plugin.backend = get_backend(destination) if not plugin.backend.initialize(): abort("Could not initialize the backup {}, please use 'backup-cli' to initialize the backup first.".format(destination)) diff --git a/backup/test_backup.py b/backup/test_backup.py index e9bb2e6..4da95f8 100644 --- a/backup/test_backup.py +++ b/backup/test_backup.py @@ -12,20 +12,13 @@ cli_path = os.path.join(os.path.dirname(__file__), "backup-cli") def test_start(node_factory, directory): - bdest = os.path.join(directory, 'lightning-1', 'backup.dbak') - opts = { - 'plugin': plugin_path, - 'backup-destination': 'file://' + bdest, - } - - subprocess.check_call([cli_path, "init", directory, directory]) bpath = os.path.join(directory, 'lightning-1', 'regtest') - bdest = os.path.join(bpath, 'backup.dbak') + bdest = 'file://' + os.path.join(bpath, 'backup.dbak') os.makedirs(bpath) - subprocess.check_call([cli_path, "init", bpath, bpath]) + subprocess.check_call([cli_path, "init", bpath, bdest]) opts = { 'plugin': plugin_path, - 'backup-destination': 'file://' + bdest, + 'backup-destination': bdest, } l1 = node_factory.get_node(options=opts, cleandir=False) @@ -48,12 +41,12 @@ def test_tx_abort(node_factory, directory): """ bpath = os.path.join(directory, 'lightning-1', 'regtest') - bdest = os.path.join(bpath, 'backup.dbak') + bdest = 'file://' + os.path.join(bpath, 'backup.dbak') os.makedirs(bpath) - subprocess.check_call([cli_path, "init", bpath, bpath]) + subprocess.check_call([cli_path, "init", bpath, bdest]) opts = { 'plugin': plugin_path, - 'backup-destination': 'file://' + bdest, + 'backup-destination': bdest, } l1 = node_factory.get_node(options=opts, cleandir=False) l1.stop() @@ -78,12 +71,12 @@ def test_failing_restore(nf, directory): """ bpath = os.path.join(directory, 'lightning-1', 'regtest') - bdest = os.path.join(bpath, 'backup.dbak') + bdest = 'file://' + os.path.join(bpath, 'backup.dbak') os.makedirs(bpath) - subprocess.check_call([cli_path, "init", bpath, bpath]) + subprocess.check_call([cli_path, "init", bpath, bdest]) opts = { 'plugin': plugin_path, - 'backup-destination': 'file://' + bdest, + 'backup-destination': bdest, } l1 = node_factory.get_node(options=opts, cleandir=False) l1.stop() @@ -103,12 +96,12 @@ def test_intermittent_backup(node_factory, directory): """ bpath = os.path.join(directory, 'lightning-1', 'regtest') - bdest = os.path.join(bpath, 'backup.dbak') + bdest = 'file://' + os.path.join(bpath, 'backup.dbak') os.makedirs(bpath) - subprocess.check_call([cli_path, "init", bpath, bpath]) + subprocess.check_call([cli_path, "init", bpath, bdest]) opts = { 'plugin': plugin_path, - 'backup-destination': 'file://' + bdest, + 'backup-destination': bdest, } l1 = node_factory.get_node(options=opts, cleandir=False)