Skip to content

Instantly share code, notes, and snippets.

@alexbosworth
Last active March 29, 2024 01:33
Show Gist options
  • Save alexbosworth/2c5e185aedbdac45a03655b709e255a3 to your computer and use it in GitHub Desktop.
Save alexbosworth/2c5e185aedbdac45a03655b709e255a3 to your computer and use it in GitHub Desktop.
Backup channel.backup file using systemd and inotify

LND backup script for channel.backup using inotify

Install inotify

sudo apt install inotify-tools

Create script to watch for changes and copy on change

Create a script file at the path of your choice: /path/copy-channel-backup-on-change.sh

#!/bin/bash
while true; do
    inotifywait /path/to/.lnd/data/chain/bitcoin/mainnet/channel.backup
    cp /path/to/.lnd/data/chain/bitcoin/mainnet/channel.backup /backup/path/channel.backup
done

chmod +x /path/copy-channel-backup-on-change.sh

Use systemd to run as service

Create file: sudo emacs /etc/systemd/system/backup-channels.service

[Service]
ExecStart=/path/copy-channel-backup-on-change.sh
Restart=always
RestartSec=1
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=backup-channels
User=ubuntu
Group=ubuntu

[Install]
WantedBy=multi-user.target

Start

sudo systemctl start backup-channels

Monitor

journalctl -fu backup-channels

Run at boot

sudo systemctl enable backup-channels

Check that the backup is working

touch /path/to/.lnd/data/chain/bitcoin/mainnet/channel.backup

Look to see if the backup was updated

@darwin
Copy link

darwin commented Apr 8, 2019

Shameless plug: https://github.com/darwin/lnd-auto-backup

PRs welcome :-)

@rahil471
Copy link

I have jotted down a guide based on my experience of working with SCB.

https://medium.com/@rahil471/enable-channel-backups-and-fund-recovery-on-lnd-lightning-network-3f27be42eb43

Feedbacks are welcomed.

@bereska
Copy link

bereska commented May 11, 2019

this is awesome. Trying to set it up on my raspiblitz where bitcoin and lnd were manually installed. The problem is I have 3 users: admin, bitcoin and root; and the channel.backup is in /home/bitcoin/.lnd/data/chain/bitcoin/mainnet, but I can only ssh to raspiblitz as admin. So I created copy-channel-backup-on-change.sh in /home/bitcoin as root for the script to copy channel.backup to /home/admin/backup so as I could rsync it there from a remote machine. The question is how to create a systemd service for it to work correctly? Thank you

@bereska
Copy link

bereska commented May 13, 2019

never mind, sorry for wasting your time if any, i figured it all out. My channel.back is being rsynced to two remotes. Thanks for guidance

@sangaman
Copy link

sangaman commented Dec 20, 2019

What are the pros/cons of using this approach vs lsyncd? Or is it basically the same?

@triplea-dev
Copy link

The benefit of this (the above) method is that it uses standard OS tools (cp file).
You could mount a backup disk (remote or cloud storage for example) and just copy the file to that mount point.
Easier to trust when you know exactly what's happening.

@OliverOffing
Copy link

This is great, thanks @alexbosworth.

Worth noting that people might want to add the script a call to deadmanssnitch.com just in case.

@btckai
Copy link

btckai commented Jun 12, 2023

Hi. I am trying to get a copy of my scb. i am running umbrel on linux ubuntu 20.04. everything works as you described. But:

inotify is making a backup every minute of my channel.backup file - even when there is no change in file. i dont know how to handle this.

@S0wbear83
Copy link

Hi. I am trying to get a copy of my scb. i am running umbrel on linux ubuntu 20.04. everything works as you described. But:

inotify is making a backup every minute of my channel.backup file - even when there is no change in file. i dont know how to handle this.

you can change it like this to wait for modifications or attribute changes:
inotifywait -e modify,attrib /path/to/.lnd/data/chain/bitcoin/mainnet/channel.backup

@btckai
Copy link

btckai commented Jun 17, 2023

Hi. I am trying to get a copy of my scb. i am running umbrel on linux ubuntu 20.04. everything works as you described. But:
inotify is making a backup every minute of my channel.backup file - even when there is no change in file. i dont know how to handle this.

you can change it like this to wait for modifications or attribute changes: inotifywait -e modify,attrib /path/to/.lnd/data/chain/bitcoin/mainnet/channel.backup

I changed it to:

event=$(inotifywait --format '%e' /home/umbrel/umbrel/xxxxx) || exit
[ "$event" = "MODIFY" ] && cp /home/umbrel/umbrel/xxxxxxx home/umbrel/umbrel/backup/xxxxx
done

will try your command too. thank you for that. awesome work!

@Hermit-Tango
Copy link

never mind, sorry for wasting your time if any, i figured it all out. My channel.back is being rsynced to two remotes. Thanks for guidance

Hey I have a few questions, do you mind answering them if you got the time?? I just set up my Raspiblitz node too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment