3.2 Channel backup for LND
We set up a local or remote "Static Channel Backup" for Lightning. A monitoring script keeps it up-to-date to enable the recovery of your Lightning funds in case of hardware failure.
Why are Lightning channel backups important?
The Static Channels Backup (SCB) is a feature of LND that allows for the on-chain recovery of lightning channel balances in the case of a bricked node. Despite its name, it does not allow the recovery of your LN channels but increases the chance that you'll recover all (or most) of your off-chain (local) balances.
The SCB contains all the necessary channel information used for the recovery process called Data Loss Protection (DLP). It is a safe backup mechanism with no risk of provoking penalty transactions that could lead to losing channel balances. The SCB contains all necessary peer and channel information, allowing LND to send a request to force-close the channel on their end to all your previous online peers. Without this method, you would need to contact your peers manually or wait for them to force-close on their own eventually.
This SCB-based recovery method has several consequences worth bearing in mind:
This method relies on the goodwill of the peer: a malicious peer could refuse to force close the channel, and the funds would remain locked up.
Recovery only works with online peers: LND cannot send a request to force-close the channel if a peer is offline. Therefore, the funds in that channel will remain locked up until this peer comes back online, or possibly forever if that peer doesn't come back.
The backup needs to be up-to-date: Since LND needs to know about your peers and channels, the SCB needs to be updated every time you open a new channel.
You need to set up an automated SCB update mechanism that:
Creates or updates your SCB file each time you open a channel (or close one, although this is less important).
Stores the SCB file in a different backup location to ensure that it is available in case of a failing SSD.
You can read more about SCBs in this section of Mastering the Lightning Network
Choose your preferred backup method(s)
This guide covers two automated backup methods:
LOCAL: store the backup on a USB thumbdrive or microSD card plugged into your Pi
REMOTE: send the encrypted backup to a private GitHub repository
Method | Requires hardware | Requires GitHub account | Protects against | Relies on 3rd-party |
---|---|---|---|---|
LOCAL | YES | NO | Drive failure only | NO |
REMOTE | NO | YES | Drive failure & widespread node damage | YES |
We recommend using both methods, but you can choose either one of them, depending on your requirements and preferences. Whatever method you choose:
Follow the "Preparations" section first, then
Follow the optional local and/or remote backup sections.
Finally, follow the "Run SCB-Backup" section that works for whatever method you've chosen.
Requirements
Preparations
We prepare a shell script that automatically updates the LND SCB file on a change in your backup location(s).
Install inotify-tools
Installing inotify-tools
allows us to use inotify
, an application that monitors files and directories for changes.
We will use it to monitor the channel.backup
file and detect updates by LND each time a channel is opened or closed.
With user
admin
, installinotify-tools
Create script
We create a shell script to monitor channel.backup
and make a copy of our backup locations if it changes.
Create a new shell script file
Check the following lines of code and paste them into the text editor. By default, both local and remote backup methods are disabled. We will enable one or both of them in the next sections, depending on your preferences. Save and exit
Make the script executable
Option 1: Local backup
Follow this section if you want a local backup. If you only want a remote backup, skip to the next section.
Storage device size
The channel.backup
file is very small in size (<<1 MB) so even the smallest USB thumbdrive or microSD card will do the job.
Formatting
To ensure that the storage device does not contain malicious code, we will format it on our local computer (select a name easy to recognize like "SCB backup" and choose the FAT filesystem). The following external guides explain how to format your USB thumbdrive or microSD card on Windows, macOS, or Linux.
Once formatted, plug the storage device into your MiniBolt.
Set up a mounting point for the storage device
Create the mounting directory
Make it immutable
List active block devices
Copy the
UUID
of your backup device into a text editor on your local computer (e.g. here123456
)
Get the "lnd" user identifier (UID) and the "lnd" group identifier (GID) from the
/etc/passwd
database of all user accounts
Copy these values into a text editor on your local computer (e.g. here GID
XXXX
and UIDYYYY
)
Edit your Filesystem Table configuration file
Add the following as a new line at the end, replacing
123456
,XXXX
andYYYY
with your ownUUID
,GID
andUID
Mount the drive and check the file system
Ensure that
/mnt/static-channel-backup-external
is listed
Enable the local backup function in the script
Enable the local backup in the script by changing the variable value for
LOCAL_BACKUP_ENABLED
at line 14 totrue
. Save and exit
Option 2: Remote backup preparations
Follow this section if you want a remote backup. If you already set up a local backup, and don't want a remote backup, skip to the next section.
Create a GitHub repository
Go to GitHub, sign up for a new user account, or log in with an existing one. If you don't want GitHub to know your identity and IP address about your Lightning node, it is recommended to create a new account even if you have an existing one, and use Tor Browser for this and follow the steps
Create a new repository
Type the following repository name:
remote-lnd-backup
Select "Private" (rather than the default "Public")
Click on "Create repository"
Clone the repository to your node
Using the
lnd
user
Create a pair of SSH keys
When prompted, press "Enter" to confirm the default SSH directory and press "Enter" again to not set up a passphrase
Display the public key and take note
Go back to the GitHub repository webpage
Click on "Settings", then "Deploy keys", then "Add deploy key"
Type a title (e.g. "SCB")
In the "Key" box, copy/paste the string generated above starting (e.g.
ssh-rsa 1234abcd... lnd@minibolt
)Tick the box "
Allow write access
" to enable this key to push changes to the repositoryClick "Add key"
Set up global Git configuration values (the name and email are required but can be dummy values)
(Optional) Add this step if you want to preserve your privacy with GitHub servers if not, jump to the next step directly -> (
$ cd ~/.lnd
)
Move to the LND data folder and clone your newly created empty repository
Replace
<YourGitHubUsername>
with your own GitHub username. When prompted"Are you sure you want to continue connecting (yes/no/[fingerprint])?"
type "yes" and enter
Example of expected output:
Exit the
lnd
session to return to theadmin
user session
Enable the remote backup function in the script
Enable the remote backup in the script by changing the variable value for
REMOTE_BACKUP_ENABLED
line 15 totrue
. Save and exit
Create systemd service
We set up the backup script as a systemd service to run in the background and optionally start automatically on system startup.
Still as user
admin
, create a new service file
Paste the following lines. Save and exit
Enable autoboot (optional)
Prepare “scb-backup” monitoring by the systemd journal and check the logging output. You can exit monitoring at any time with Ctrl-C
Run
To keep an eye on the software movements, start your SSH program (eg. PuTTY) a second time, connect to the MiniBolt node, and log in as "admin". Commands for the second session start with the prompt $2
(which must not be entered).
Start the service
Example of expected output on the first SSH session with $ journalctl -f -u btcrpcexplorer
⬇️
The automated backup is now up and running. To test if everything works, we now cause the default
channel.backup
file to change. Then we check if a copy gets stored at the intended backup location(s). Simulate achannel.backup
file change with thetouch
command (don't worry! It simply updates the timestamp of the file but not its content)
Switch back again to the first SSH session. In the logs, you should see new entries similar to these (depending on which backup methods you enabled)
If you get the next error:
-> Replace the line 41 git push "--set-upstream origin
main"
to "git push --set-upstream origin
master"
in the script , and try again
If you enabled the local backup, check the content of your local storage device. It should now contain a backup file with the date/time corresponding to the test made just above
Example of expected output:
If you enabled the remote backup, check your GitHub repository (in the
[ <> Code ]
tab). It should now contain the latest timestamped backup file
You're set! Each time you open a new channel or close an existing one, the monitoring script will automatically save a timestamped copy of the backup file to your backup location(s)
Uninstall
Uninstall service & user
Ensure you are logged in with the user admin
, stop, disable autoboot (if enabled), and delete the service
Uninstall script
Delete the script installed
Last updated