mirror of
https://github.com/aljazceru/plugins.git
synced 2025-12-20 06:34:20 +01:00
backup: If a database exists write an intial snapshot
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from backup import FileBackend, get_backend
|
from backup import FileBackend, get_backend, Change
|
||||||
import os
|
import os
|
||||||
import click
|
import click
|
||||||
import json
|
import json
|
||||||
|
import sqlite3
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@@ -17,6 +19,7 @@ def init(lightning_dir, backend_url):
|
|||||||
backend.write_metadata()
|
backend.write_metadata()
|
||||||
|
|
||||||
lock_file = os.path.join(lightning_dir, "backup.lock")
|
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:
|
with open(lock_file, "w") as f:
|
||||||
f.write(json.dumps({
|
f.write(json.dumps({
|
||||||
@@ -25,6 +28,28 @@ def init(lightning_dir, backend_url):
|
|||||||
|
|
||||||
# TODO Take a snapshot
|
# 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(
|
print("Initialized backup backend {destination}, you can now start c-lightning".format(
|
||||||
destination=destination,
|
destination=destination,
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -54,6 +54,26 @@ def test_start_no_init(node_factory, directory):
|
|||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
|
def test_init_not_empty(node_factory, directory):
|
||||||
|
"""We want to add backups to an existing lightning node.
|
||||||
|
|
||||||
|
backup-cli init should start the backup with an initial snapshot.
|
||||||
|
"""
|
||||||
|
bpath = os.path.join(directory, 'lightning-1', 'regtest')
|
||||||
|
bdest = 'file://' + os.path.join(bpath, 'backup.dbak')
|
||||||
|
l1 = node_factory.get_node()
|
||||||
|
l1.stop()
|
||||||
|
|
||||||
|
out = subprocess.check_output([cli_path, "init", bpath, bdest])
|
||||||
|
assert(b'Found an existing database' in out)
|
||||||
|
assert(b'Successfully written initial snapshot' in out)
|
||||||
|
|
||||||
|
# Now restart and add the plugin
|
||||||
|
l1.daemon.opts['plugin'] = plugin_path
|
||||||
|
l1.start()
|
||||||
|
l1.daemon.wait_for_log(r'plugin-backup.py: Versions match up')
|
||||||
|
|
||||||
|
|
||||||
def test_tx_abort(node_factory, directory):
|
def test_tx_abort(node_factory, directory):
|
||||||
"""Simulate a crash between hook call and DB commit.
|
"""Simulate a crash between hook call and DB commit.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user