From 31f4a547a9d4e8a7450589877c163203462b9905 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Thu, 23 Dec 2021 13:06:32 +0100 Subject: [PATCH] Backup LND SCB to S3 on change closes #359 --- nodes/bitcoin-2.json | 9 ++-- .../kosmos-bitcoin/recipes/lnd-scb-s3.rb | 47 +++++++++++++++++++ .../templates/lnd-channel-backup-s3.sh.erb | 7 +++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 site-cookbooks/kosmos-bitcoin/recipes/lnd-scb-s3.rb create mode 100644 site-cookbooks/kosmos-bitcoin/templates/lnd-channel-backup-s3.sh.erb diff --git a/nodes/bitcoin-2.json b/nodes/bitcoin-2.json index aaa9fcf..5340527 100644 --- a/nodes/bitcoin-2.json +++ b/nodes/bitcoin-2.json @@ -23,6 +23,7 @@ "kosmos-bitcoin::source", "kosmos-bitcoin::c-lightning", "kosmos-bitcoin::lnd", + "kosmos-bitcoin::lnd-scb-s3", "kosmos-bitcoin::rtl", "kosmos-bitcoin::lndhub", "kosmos_postgresql::hostsfile", @@ -48,6 +49,9 @@ "git::default", "git::package", "golang::default", + "backup::default", + "logrotate::default", + "kosmos-bitcoin::aws-client", "kosmos-nodejs::default", "nodejs::nodejs_from_package", "nodejs::repo", @@ -70,9 +74,7 @@ "nginx::commons_dir", "nginx::commons_script", "nginx::commons_conf", - "kosmos-nginx::firewall", - "backup::default", - "logrotate::default" + "kosmos-nginx::firewall" ], "platform": "ubuntu", "platform_version": "20.04", @@ -94,6 +96,7 @@ "recipe[kosmos-bitcoin::source]", "recipe[kosmos-bitcoin::c-lightning]", "recipe[kosmos-bitcoin::lnd]", + "recipe[kosmos-bitcoin::lnd-scb-s3]", "recipe[kosmos-bitcoin::rtl]", "recipe[kosmos-bitcoin::lndhub]", "role[btcpay]" diff --git a/site-cookbooks/kosmos-bitcoin/recipes/lnd-scb-s3.rb b/site-cookbooks/kosmos-bitcoin/recipes/lnd-scb-s3.rb new file mode 100644 index 0000000..25d794d --- /dev/null +++ b/site-cookbooks/kosmos-bitcoin/recipes/lnd-scb-s3.rb @@ -0,0 +1,47 @@ +# +# Cookbook:: kosmos-bitcoin +# Recipe:: lnd-scb-s3 +# +# Static Channel Backup for LND channel states +# + +include_recipe "kosmos-bitcoin::aws-client" + +package "inotify-tools" + +backup_script_path = "/opt/lnd-channel-backup-s3.sh" + +template backup_script_path do + source "lnd-channel-backup-s3.sh.erb" + mode '0740' + variables lnd_dir: node['lnd']['lnd_dir'], + bitcoin_network: node['bitcoin']['network'], + s3_bucket: node['backup']['s3']['bucket'], + s3_scb_dir: "#{node['name']}/lnd/#{node['bitcoin']['network']}" + notifies :restart, "systemd_unit[lnd-channel-backup.service]", :delayed +end + +systemd_unit 'lnd-channel-backup.service' do + content({ + Unit: { + Description: 'LND Static Channel Backup', + Documentation: ['https://gist.github.com/alexbosworth/2c5e185aedbdac45a03655b709e255a3'], + Requires: 'lnd.service', + After: 'lnd.service' + }, + Service: { + User: 'root', + Group: 'root', + Type: 'simple', + ExecStart: backup_script_path, + Restart: 'always', + RestartSec: 1 + }, + Install: { + WantedBy: 'multi-user.target' + } + }) + verify false + triggers_reload true + action [:create, :enable, :start] +end diff --git a/site-cookbooks/kosmos-bitcoin/templates/lnd-channel-backup-s3.sh.erb b/site-cookbooks/kosmos-bitcoin/templates/lnd-channel-backup-s3.sh.erb new file mode 100644 index 0000000..d5b1c80 --- /dev/null +++ b/site-cookbooks/kosmos-bitcoin/templates/lnd-channel-backup-s3.sh.erb @@ -0,0 +1,7 @@ +#!/bin/bash +set -xe -o pipefail + +while true; do + inotifywait <%= @lnd_dir %>/data/chain/bitcoin/<%= @bitcoin_network %>/channel.backup + aws s3 cp <%= @lnd_dir %>/data/chain/bitcoin/<%= @bitcoin_network %>/channel.backup "s3://<%= @s3_bucket %>/<%= @s3_scb_dir %>/channel.backup" +done