raspiblitz/home.admin/config.scripts/lnd.rescue.sh

279 lines
8.7 KiB
Bash
Raw Normal View History

2019-03-16 23:47:22 +01:00
#!/bin/bash
2019-04-26 14:14:27 +02:00
source /mnt/hdd/raspiblitz.conf
2019-03-16 23:47:22 +01:00
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
2019-04-26 16:36:03 +02:00
echo "# small rescue script to to backup or restore LND data"
echo "# -> backup all LND data in a tar.gz file for download:"
echo "# lnd.rescue.sh backup [?no-download]"
echo "# -> upload a LND data tar.gz file to replace LND data:"
echo "# lnd.rescue.sh restore"
echo "# -> download the LND channel.backup file from SD card:"
echo "# lnd.rescue.sh scb-down"
echo "# -> upload the LND channel.backup to recover wallet:"
echo "# lnd.rescue.sh scb-up"
2019-03-16 23:47:22 +01:00
exit 1
fi
localip=$(ip addr | grep 'state UP' -A2 | egrep -v 'docker0|veth' | grep 'eth0\|wlan0' | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
2019-03-16 23:47:22 +01:00
mode="$1"
if [ ${mode} = "backup" ]; then
################################
# BACKUP
################################
2020-06-24 21:15:06 +02:00
echo "# *** LND.RESCUE --> BACKUP"
2019-03-16 23:47:22 +01:00
# stop LND
echo "# Stopping lnd..."
2019-03-16 23:47:22 +01:00
sudo systemctl stop lnd
sleep 5
echo "# OK"
2019-03-16 23:47:22 +01:00
echo
# zip it
sudo tar -zcvf /home/admin/lnd-rescue.tar.gz /mnt/hdd/lnd 1>&2
sudo chown admin:admin /home/admin/lnd-rescue.tar.gz 1>&2
2019-03-16 23:47:22 +01:00
2019-04-16 13:22:29 +01:00
# delete old backups
rm /home/admin/lnd-rescue-*.tar.gz 2>/dev/null 1>/dev/null
2019-04-16 13:22:29 +01:00
2019-03-16 23:47:22 +01:00
# name with md5 checksum
2019-03-17 00:19:31 +01:00
md5checksum=$(md5sum /home/admin/lnd-rescue.tar.gz | head -n1 | cut -d " " -f1)
mv /home/admin/lnd-rescue.tar.gz /home/admin/lnd-rescue-${md5checksum}.tar.gz 1>&2
echo "file='lnd-rescue-${md5checksum}.tar.gz'"
echo "path='/home/admin/'"
byteSize=$(ls -l /home/admin/lnd-rescue-${md5checksum}.tar.gz | awk '{print $5}')
echo "size=${byteSize}"
if [ ${byteSize} -lt 100 ]; then
echo "error='backup is empty'"
echo
echo "# *****************************"
echo "# * BACKUP ERROR *"
echo "# *****************************"
echo "# The byte size of the created rescue-file is too small (${byteSize}) - might be empty!"
echo "# If you plan any update or recovery please stop and report this error to dev team. Thx."
exit 0
fi
2019-03-16 23:47:22 +01:00
2019-04-16 18:05:03 +01:00
# stop here in case of 'no-download' option
if [ "${2}" == "no-download" ]; then
echo "# No download of LND data requested."
2019-04-16 18:05:03 +01:00
exit 0
fi
2019-03-16 23:47:22 +01:00
# offer SCP for download
2019-04-26 14:04:51 +02:00
clear
2019-03-17 03:33:42 +01:00
echo
2020-06-25 23:32:41 +02:00
echo "****************************"
echo "* DOWNLOAD THE RESCUE FILE *"
echo "****************************"
2019-03-16 23:47:22 +01:00
echo
2020-02-14 14:35:11 +01:00
echo "ON YOUR LAPTOP - RUN IN NEW TERMINAL:"
2019-12-10 20:29:15 +01:00
echo "scp -r 'admin@${localip}:/home/admin/lnd-rescue-*.tar.gz' ./"
2019-03-16 23:47:22 +01:00
echo ""
2020-02-23 19:44:27 +01:00
echo "Use password A to authenticate file transfer."
echo "Check for correct file size after transfer: ${byteSize} byte"
2019-03-16 23:47:22 +01:00
echo
2020-06-25 23:32:41 +02:00
echo "BEWARE: Your Lightning node is now stopped. It's safe to backup the data and"
2020-02-18 14:16:08 +01:00
echo "restore it on a fresh RaspiBlitz. But once this Lightning node gets started"
echo "again or rebooted its not adviced to restore the backup file anymore because"
echo "it cointains then outdated channel data & can lead to loss of channel funds."
2019-03-16 23:47:22 +01:00
elif [ ${mode} = "restore" ]; then
################################
# RESTORE
################################
2019-04-26 16:36:03 +02:00
echo "# LND.RESCUE --> RESTORE"
2019-03-16 23:47:22 +01:00
echo ""
2019-04-26 16:36:03 +02:00
# delete old backups
2019-04-16 13:22:29 +01:00
rm /home/admin/lnd-rescue-*.tar.gz
2019-03-16 23:47:22 +01:00
filename=""
while [ ${#filename} -eq 0 ]
do
2019-03-17 00:24:06 +01:00
countZips=$(sudo ls /home/admin/lnd-rescue-*.tar.gz 2>/dev/null | grep -c 'lnd-rescue')
2019-03-16 23:47:22 +01:00
if [ ${countZips} -lt 1 ]; then
echo "**************************"
2020-02-14 14:35:11 +01:00
echo "* UPLOAD THE RESCUE FILE *"
2019-03-16 23:47:22 +01:00
echo "**************************"
echo "If you have a lnd-rescue backup file on your laptop you can now"
2019-04-16 02:57:18 +01:00
echo "upload it and restore the your latest LND state."
echo
echo "CAUTION: Dont restore old LND states - risk of loosing funds!"
2019-03-16 23:47:22 +01:00
echo
echo "To make upload open a new terminal on your laptop,"
echo "change into the directory where your lnd-rescue file is and"
echo "COPY, PASTE AND EXECUTE THE FOLLOWING COMMAND:"
2019-12-10 22:03:24 +01:00
echo "scp -r ./lnd-rescue-*.tar.gz admin@${localip}:/home/admin/"
2019-03-16 23:47:22 +01:00
echo ""
2020-02-23 19:44:27 +01:00
echo "Use password A to authenticate file transfer."
2020-02-14 14:35:11 +01:00
echo "PRESS ENTER when upload is done."
2019-03-16 23:47:22 +01:00
fi
if [ ${countZips} -gt 1 ]; then
echo "!! WARNING !!"
echo "There are multiple lnd-rescue files in directory /home/admin."
2019-04-16 13:22:29 +01:00
echo "Make sure you upload only one tar.gz-file and start again."
2019-03-17 00:24:06 +01:00
echo
2019-04-16 13:22:29 +01:00
echo "PRESS ENTER to continue."
read key
2019-03-16 23:47:22 +01:00
exit 1
fi
if [ ${countZips} -eq 1 ]; then
2019-03-17 00:28:14 +01:00
clear
2019-03-17 00:29:58 +01:00
echo
echo "**************************"
echo "* RESTORING BACKUP FILE *"
echo "**************************"
echo
2019-03-17 00:31:20 +01:00
2019-03-16 23:47:22 +01:00
filename=$(sudo ls /home/admin/lnd-rescue-*.tar.gz)
echo "OK -> found file to restore: ${filename}"
# checksum test
2019-03-16 23:47:22 +01:00
md5checksum=$(md5sum ${filename} | head -n1 | cut -d " " -f1)
isCorrect=$(echo ${filename} | grep -c ${md5checksum})
if [ ${isCorrect} -eq 1 ]; then
echo "OK -> checksum looks good: ${md5checksum}"
else
echo "!!! FAIL -> Checksum not correct."
2020-06-25 23:32:41 +02:00
echo "Maybe transfer failed? Continue at your own risk!"
echo "It is recommended to abort and upload again!"
2019-03-16 23:47:22 +01:00
fi
# overrride test
2019-04-26 14:14:27 +02:00
oldWalletExists=$(sudo ls /mnt/hdd/lnd/data/chain/${network}/${chain}net/wallet.db 2>/dev/null | grep -c "wallet.db")
if [ ${oldWalletExists} -gt 0 ]; then
echo
echo "WARNING: This will delete/overwrite the LND state/funds of this RaspiBlitz."
fi
2019-03-16 23:47:22 +01:00
echo
2019-04-16 13:22:29 +01:00
echo "PRESS ENTER to start restore. Enter x & ENTER to cancel."
2019-03-16 23:47:22 +01:00
fi
read key
2019-04-16 13:22:29 +01:00
if [ "${key}" == "x" ]; then
exit 1
fi
2019-03-16 23:47:22 +01:00
done
# stop LND
echo "Stopping lnd..."
2019-03-17 00:28:14 +01:00
sudo systemctl stop lnd
2019-03-16 23:47:22 +01:00
sleep 5
echo "OK"
echo
# clean DIR
echo "Cleaning LND data ..."
sudo rm -r /mnt/hdd/lnd/*
echo "OK"
echo
# unpack zip
2019-03-17 00:31:20 +01:00
echo "Restoring LND data from ${filename} ..."
2019-03-17 00:32:03 +01:00
sudo tar -xf ${filename} -C /
2019-03-17 00:34:25 +01:00
sudo chown -R bitcoin:bitcoin /mnt/hdd/lnd
2019-03-16 23:47:22 +01:00
echo "OK"
echo
# check if LND needs update
# (if RaspiBlitz has an optional LND version update, then install it
# the newer LND version can always handle older data)
echo "Checking LND version ..."
source <(sudo -u admin /home/admin/config.scripts/lnd.update.sh info)
if [ ${lndUpdateInstalled} -eq 0 ]; then
echo "Installing available LND update ... (newer version can handle more wallet formats)"
sudo -u admin /home/admin/config.scripts/lnd.update.sh verified
else
echo "OK"
fi
echo
2019-03-16 23:47:22 +01:00
# start LND
echo "Starting lnd..."
sudo systemctl start lnd
echo "OK"
echo
echo "DONE - please check if LND starts up correctly with restored state and funds."
2019-04-16 02:57:18 +01:00
echo "Keep in mind that some channels maybe forced closed in the meanwhile."
2019-03-16 23:47:22 +01:00
echo
2019-04-26 16:36:03 +02:00
elif [ ${mode} = "scb-down" ]; then
echo
echo "****************************"
echo "* DOWNLOAD THE BACKUP FILE *"
echo "****************************"
echo
echo "RUN THE FOLLOWING COMMAND ON YOUR LAPTOP IN NEW TERMINAL:"
echo "scp -r admin@${localip}:/home/admin/.lnd/data/chain/${network}/${chain}net/channel.backup ./"
echo ""
2020-02-23 19:44:27 +01:00
echo "Use password A to authenticate file transfer."
2019-04-26 16:36:03 +02:00
echo
echo "NOTE: Use this file when setting up a fresh RaspiBlitz by choosing"
echo "option OLD WALLET and then SCB+SEED -> Seed & channel.backup file"
echo "Will just recover on-chain & channel-funds, but closing all channels"
elif [ ${mode} = "scb-up" ]; then
gotFile=-1
while [ ${gotFile} -lt 1 ]
do
# show info
clear
sleep 1
echo "**********************************"
echo "* UPLOAD THE channel.backup FILE *"
echo "**********************************"
echo
if [ ${gotFile} -eq -1 ]; then
echo "If you have the channel.backup file on your laptop or on"
echo "another server you can now upload it to the RaspiBlitz."
elif [ ${gotFile} -eq 0 ]; then
echo "NO channel.backup FOUND IN /home/admin"
echo "Please try upload again."
fi
echo
echo "To make upload open a new terminal and change,"
echo "into the directory where your lnd-rescue file is and"
echo "COPY, PASTE AND EXECUTE THE FOLLOWING COMMAND:"
echo "scp ./channel.backup admin@${localip}:/home/admin/"
echo ""
2020-02-23 19:44:27 +01:00
echo "Use password A to authenticate file transfer."
2019-04-26 16:36:03 +02:00
echo "PRESS ENTER when upload is done. Enter x & ENTER to cancel."
# wait user interaction
echo "Please upload file. Press ENTER to try again or (x & ENTER) to cancel."
read key
if [ "${key}" == "x" ]; then
# EXIT with CODE 1 --> USER CANCEL
echo "# CANCEL upload"
exit 1
fi
# test upload
gotFile=$(ls /home/admin/channel.backup | grep -c 'channel.backup')
done
# EXIT with CODE 1 --> FILE UPLOADED
2020-02-19 10:56:53 +01:00
echo
2019-04-26 16:36:03 +02:00
echo "# OK channel.backup uploaded"
2020-02-19 10:56:53 +01:00
sleep 2
2019-04-26 16:36:03 +02:00
exit 0
2019-03-16 23:47:22 +01:00
else
echo "unknown parameter '${mode}' - exit"
2020-06-25 23:32:41 +02:00
fi