mirror of
https://github.com/aljazceru/plugins.git
synced 2025-12-20 14:44:20 +01:00
77 lines
2.1 KiB
Python
Executable File
77 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from backup import FileBackend, get_backend, Change
|
|
import os
|
|
import click
|
|
import json
|
|
import sqlite3
|
|
import sys
|
|
|
|
|
|
@click.command()
|
|
@click.argument("lightning-dir", type=click.Path(exists=True))
|
|
@click.argument("backend-url")
|
|
def init(lightning_dir, backend_url):
|
|
destination = backend_url
|
|
backend = get_backend(destination, create=True)
|
|
backend.version, backend.prev_version = 0, 0
|
|
backend.offsets = [512, 0]
|
|
backend.version_count = 0
|
|
backend.write_metadata()
|
|
|
|
lock_file = os.path.join(lightning_dir, "backup.lock")
|
|
db_file = os.path.join(lightning_dir, "lightningd.sqlite3")
|
|
|
|
with open(lock_file, "w") as f:
|
|
f.write(json.dumps({
|
|
'backend_url': destination,
|
|
}))
|
|
|
|
# TODO Take a snapshot
|
|
|
|
data_version = 0
|
|
if os.path.exists(db_file):
|
|
print("Found an existing database at {db_file}, initializing the backup with a snapshot".format(db_file=db_file))
|
|
# Peek into the DB to see if we have
|
|
db = sqlite3.connect(db_file)
|
|
cur = db.cursor()
|
|
rows = cur.execute("SELECT intval FROM vars WHERE name = 'data_version'")
|
|
data_version = rows.fetchone()[0]
|
|
|
|
snapshot = Change(
|
|
version=data_version,
|
|
snapshot=open(db_file, 'rb').read(),
|
|
transaction=None
|
|
)
|
|
if not backend.add_change(snapshot):
|
|
print("Could not write snapshot to backend")
|
|
sys.exit(1)
|
|
else:
|
|
print("Successfully written initial snapshot to {destination}".format(destination=destination))
|
|
else:
|
|
print("Database does not exist yet, created an empty backup file")
|
|
|
|
print("Initialized backup backend {destination}, you can now start c-lightning".format(
|
|
destination=destination,
|
|
))
|
|
|
|
|
|
@click.command()
|
|
@click.argument("backend-url")
|
|
@click.argument("restore-destination")
|
|
def restore(backend_url, restore_destination):
|
|
destination = backend_url
|
|
backend = get_backend(destination)
|
|
backend.restore(restore_destination)
|
|
|
|
|
|
@click.group()
|
|
def cli():
|
|
pass
|
|
|
|
|
|
cli.add_command(init)
|
|
cli.add_command(restore)
|
|
|
|
if __name__ == "__main__":
|
|
cli()
|