diff --git a/backup/backup-cli b/backup/backup-cli index 88a4ee0..a6c2fc8 100755 --- a/backup/backup-cli +++ b/backup/backup-cli @@ -12,7 +12,7 @@ import sys @click.argument("backend-url") def init(lightning_dir, backend_url): destination = backend_url - backend = get_backend(destination) + backend = get_backend(destination, create=True) backend.version, backend.prev_version = 0, 0 backend.offsets = [512, 0] backend.version_count = 0 diff --git a/backup/backup.py b/backup/backup.py index 1e44849..f47636b 100755 --- a/backup/backup.py +++ b/backup/backup.py @@ -121,7 +121,7 @@ class Backend(object): class FileBackend(Backend): - def __init__(self, destination: str): + def __init__(self, destination: str, create: bool): self.version = None self.prev_version = None self.destination = destination @@ -129,6 +129,9 @@ class FileBackend(Backend): self.version_count = 0 self.url = urlparse(self.destination) + if os.path.exists(self.url.path) and create: + raise ValueError("Attempted to create a FileBackend, but file already exists.") + def initialize(self) -> bool: if not os.path.exists(self.url.path): self.version = 0 @@ -232,13 +235,13 @@ def resolve_backend_class(backend_url): return backend_cl -def get_backend(destination): +def get_backend(destination, create=False): backend_cl = resolve_backend_class(destination) if backend_cl is None: raise ValueError("No backend implementation found for {destination}".format( destination=destination, )) - backend = backend_cl(destination) + backend = backend_cl(destination, create=create) backend.initialize() assert(backend.version is not None) assert(backend.prev_version is not None)