LNBits Backup and Restore database (#3412)

* LNBits Backup and Restore database

Add Backup and Restore methods and  menu entries

* LNBits backup target

based on blitz.backupdevice.sh switch backup target to /mnt/backup_manual per default

* Add postgresql script

Script to install or uninstall postgresql

Backup a single database with sql dump file

Restore a single database from sql dump file

* Unpack backup file only for SQLite

For postgresql just use the dedicated script

* Support multiple backup files

After backup file was written, keep only last 3 backups

Restore the recent backup found

* improve backup and restore with config and path

make use of raspiblitz config to retrieve db state for backup and restore

default manual backup path set to /mnt/hdd/app-data/backup

* add info command to postgresql script

retrieve database directory and database names

* update backup path for postgresql script

default path is /mnt/hdd/app-data/backup

this will survive reflash sd card

* improve user feedback

* add dialog for restore and improvements

dialog with confirm restore process

timestamps for backup file names

small improvements for user feedback

only resume if unpack backup succeed

* fix sqlite backup file name

* fix restore path and permissions

* add dialog to choose backup from a list

list all available backups to start restore in a dialog

* fix backup list for sqlite

* fix restore menu

* Update LNBits version with bigint fix for migration

if migrate to postgresql, use lnbits version with latest bigint fixes

* Update bonus.postgresql.sh

* fix postgres backup permissions
This commit is contained in:
ChuckNorrison
2022-12-14 10:40:15 +01:00
committed by GitHub
parent 7141cb1674
commit 1c26120056
2 changed files with 249 additions and 11 deletions

View File

@@ -19,6 +19,8 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "bonus.lnbits.sh prestart"
echo "bonus.lnbits.sh repo [githubuser] [branch]"
echo "bonus.lnbits.sh sync"
echo "bonus.lnbits.sh backup"
echo "bonus.lnbits.sh restore [?FILE]"
echo "bonus.lnbits.sh migrate"
exit 1
fi
@@ -209,6 +211,12 @@ Consider adding a IP2TOR Bridge under OPTIONS."
OPTIONS+=(SWITCH-LND "Switch: Use LND as funding source")
fi
# Backup database
OPTIONS+=(BACKUP "Backup database")
if [ -d /mnt/hdd/app-data/backup ]; then
OPTIONS+=(RESTORE "Restore database")
fi
# Migrate SQLite to PostgreSQL
if [ -e /mnt/hdd/app-data/LNBits/database.sqlite3 ]; then
OPTIONS+=(MIGRATE-DB "Migrate SQLite to PostgreSQL database")
@@ -265,6 +273,66 @@ Consider adding a IP2TOR Bridge under OPTIONS."
read key
exit 0
;;
BACKUP)
clear
/home/admin/config.scripts/bonus.lnbits.sh backup
echo
echo "Backup done"
echo "PRESS ENTER to continue"
read key
exit 0
;;
RESTORE)
clear
# check if backup exist
source <(/home/admin/_cache.sh get LNBitsDB)
if [ "${LNBitsDB}" == "PostgreSQL" ]; then
backup_target="/mnt/hdd/app-data/backup/lnbits_db"
backup_file=$(ls -t $backup_target/*.sql | head -n1)
else
backup_target="/mnt/hdd/app-data/backup/lnbits_sqlite"
backup_file=$(ls -t $backup_target/*.tar | head -n1)
fi
if [ "$backup_file" = "" ]; then
echo "ABORT - No Backup found to restore from"
exit 1
else
# build dialog to choose backup file from menu
OPTIONS_RESTORE=()
counter=0
cd $backup_target
for f in `find *.* -maxdepth 1 -type f`; do
[[ -f "$f" ]] || continue
counter=$(($counter+1))
OPTIONS_RESTORE+=($counter "$f")
done
WIDTH_RESTORE=66
CHOICE_HEIGHT_RESTORE=$(("${#OPTIONS_RESTORE[@]}/2+1"))
HEIGHT_RESTORE=$((CHOICE_HEIGHT_RESTORE+7))
CHOICE_RESTORE=$(dialog --clear \
--title " LNbits - Backup restore" \
--ok-label "Select" \
--cancel-label "Back" \
--menu "Choose one of the following backups:" \
$HEIGHT_RESTORE $WIDTH_RESTORE $CHOICE_HEIGHT_RESTORE \
"${OPTIONS_RESTORE[@]}" \
2>&1 >/dev/tty)
# start restore with selected backup
clear
if [ "$CHOICE_RESTORE" != "" ]; then
backup_file=${backup_target}/${OPTIONS_RESTORE[$(($CHOICE_RESTORE*2-1))]}
/home/admin/config.scripts/bonus.lnbits.sh restore "${backup_file}"
echo
echo "Restore done"
echo "PRESS ENTER to continue"
read key
fi
exit 0
fi
;;
MIGRATE-DB)
clear
dialog --title "MIGRATE LNBITS" --yesno "
@@ -285,7 +353,7 @@ This can fail for unknown circumstances. Revert of this process is possible afte
read key
fi
exit 0
;;
;;
*)
clear
exit 0
@@ -965,12 +1033,97 @@ if [ "$1" = "0" ] || [ "$1" = "off" ]; then
exit 0
fi
# backup
if [ "$1" = "backup" ]; then
source <(/home/admin/_cache.sh get LNBitsDB)
echo "# Start Backup LNBits ${LNBitsDB} database"
if [ "${LNBitsDB}" == "PostgreSQL" ]; then
# postgresql backup
sudo /home/admin/config.scripts/bonus.postgresql.sh backup lnbits_db
else
# sqlite backup
backup_target="/mnt/hdd/app-data/backup/lnbits_sqlite"
backup_file="lnbits_sqlite_`date +%d`-`date +%m`-`date +%Y`_`date +%H`-`date +%M`_fs.tar"
if [ ! -d $backup_target ]; then
sudo mkdir -p $backup_target 1>&2
fi
# Delete old backups (keep last 3 backups)
sudo chown -R admin:admin $backup_target
ls -tp $backup_target/*.tar | grep -v '/$' | tail -n +4 | tr '\n' '\0' | xargs -0 rm -- 2>/dev/null
cd $backup_target
sudo tar -cf $backup_file -C "/mnt/hdd/app-data" LNBits/
echo "OK - Backup finished, file saved as ${backup_target}/${backup_file}"
fi
sudo systemctl start lnbits
exit 0
fi
# restore
if [ "$1" = "restore" ]; then
source <(/home/admin/_cache.sh get LNBitsDB)
if [ "${LNBitsDB}" == "PostgreSQL" ]; then
echo "# Restore PostgreSQL database"
if [ "$2" != "" ]; then
backup_file=$2
sudo /home/admin/config.scripts/bonus.postgresql.sh restore lnbits_db lnbits_user raspiblitz "${backup_file}"
else
sudo /home/admin/config.scripts/bonus.postgresql.sh restore lnbits_db lnbits_user raspiblitz
fi
else
backup_target="/mnt/hdd/app-data/backup/lnbits_sqlite"
if [ ! -d $backup_target ]; then
echo "# ABORT - No backups found"
exit 1
else
echo "# Restore SQLite database"
cd $backup_target
if [ "$2" != "" ]; then
if [ -e $2 ]; then
backup_file=$2
else
echo "ABORT - File not found (${2})"
exit 1
fi
else
# find recent backup
backup_file=$(ls -t $backup_target/*.tar | head -n1)
fi
echo "Start restore from backup ${backup_file}"
# unpack backup file
sudo tar -xf $backup_file || exit 1
echo "Unpack backup successful, backup current db now ..."
# backup current db
/home/admin/config.scripts/bonus.lnbits.sh backup
# apply backup data
sudo rm -R /mnt/hdd/app-data/LNBits/
sudo chown -R lnbits:lnbits LNBits/
sudo mv LNBits/ /mnt/hdd/app-data/
echo "Remove restored backup file"
sudo rm -f $backup_file
echo "OK - Apply backup data successful"
fi
fi
sudo systemctl start lnbits
exit 0
fi
# revert migrate to postgresql
if [ "$1" = "migrate" ] && [ "$2" = "revert" ]; then
/home/admin/config.scripts/blitz.conf.sh set LNBitsMigrate "on"
revertMigration
exit 0
fi
# migrate
if [ "$1" = "migrate" ]; then
if [ -e /mnt/hdd/app-data/LNBits/database.sqlite3 ]; then
@@ -997,7 +1150,7 @@ if [ "$1" = "migrate" ]; then
sudo bash -c "echo 'LNBITS_DATA_FOLDER=/mnt/hdd/app-data/LNBits' >> /home/lnbits/lnbits/.env"
#sudo -u lnbits git checkout ${tag}
sudo -u lnbits git reset --hard f3b720b690c533b4b28793209f5a71fd01b9af6e # good tested after BIGINT fix (#1030)
sudo -u lnbits git reset --hard 4f05c6c12e284d4a322a9041d19f66d01afa205b # good tested after BIGINT fix (#1135)
/home/admin/config.scripts/bonus.lnbits.sh sync || exit 1
# stop after sync was done
sudo systemctl stop lnbits
@@ -1041,7 +1194,7 @@ if [ "$1" = "migrate" ]; then
revertMigration
exit 1
else
echo "# Convert successful"
echo "# OK - Convert successful"
fi
# cleanup old sqlite data directory
@@ -1058,9 +1211,9 @@ if [ "$1" = "migrate" ]; then
# setting value in raspi blitz config
/home/admin/config.scripts/blitz.conf.sh set LNBitsMigrate "off"
echo "# OK migration done"
echo "# OK - migration done"
else
echo "# No SQLite data found to migrate from"
echo "# ABORT - No SQLite data found to migrate from"
fi
sudo systemctl start lnbits
@@ -1068,4 +1221,4 @@ if [ "$1" = "migrate" ]; then
fi
echo "FAIL - Unknown Parameter $1"
exit 1
exit 1

95
home.admin/config.scripts/bonus.postgresql.sh Executable file → Normal file
View File

@@ -4,11 +4,20 @@
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "config script to install PostgreSQL"
echo "bonus.postgresql.sh [on|off]"
echo "bonus.postgresql.sh [backup] [database]"
echo "bonus.postgresql.sh [restore] [database] [user] [password]"
echo "bonus.postgresql.sh [info]"
exit 1
fi
command=$1
db_name=$2
db_user=$3
db_user_pw=$4
db_backupfile=$5
# switch on
if [ "$1" = "1" ] || [ "$1" = "on" ]; then
if [ "$command" = "1" ] || [ "$command" = "on" ]; then
# https://github.com/rootzoll/raspiblitz/issues/3218
echo "# Install PostgreSQL"
@@ -80,15 +89,91 @@ if [ "$1" = "1" ] || [ "$1" = "on" ]; then
fi
# switch off
if [ "$1" = "0" ] || [ "$1" = "off" ]; then
if [ "$command" = "0" ] || [ "$command" = "off" ]; then
# setting value in raspiblitz config
echo "*** REMOVING POSTGRESQL ***"
sudo apt remove -y postgresql
sudo systemctl stop postgresql 2>/dev/null
sudo systemctl stop postgresql
sudo systemctl disable postgresql
echo "OK PostgreSQL removed."
exit 0
fi
echo "FAIL - Unknown Parameter $1"
exit 1
# backup
backup_target="/mnt/hdd/app-data/backup/$db_name"
backup_file="${db_name}_`date +%d`-`date +%m`-`date +%Y`_`date +%H`-`date +%M`_dump"
if [ ! -d $backup_target ]; then
sudo mkdir -p $backup_target 1>&2
fi
# https://www.postgresql.org/docs/current/backup-dump.html
if [ "$command" = "backup" ] && [ "$db_name" != "" ]; then
echo "*** BACKUP POSTGRESQL $db_name ***"
sudo -u postgres pg_dump $db_name > $backup_target/${backup_file}.sql || exit 1
# Delete old backups (keep last 3 backups)
sudo chown -R admin:admin $backup_target
ls -tp $backup_target/*.sql | grep -v '/$' | tail -n +4 | tr '\n' '\0' | xargs -0 rm -- 2>/dev/null
echo "OK - backup finished, file saved as $backup_target/${backup_file}.sql"
exit 0
fi
# restore
if [ "$command" = "restore" ] && [ "$db_name" != "" ] && [ "$db_user" != "" ] && [ "$db_user_pw" != "" ]; then
echo "*** RESTORE POSTGRESQL $db_name ***"
# find recent backup
if [ "$db_backupfile" != "" ]; then
backup_file=$db_backupfile
else
backup_file=$(ls -t $backup_target/*.sql | head -n1)
fi
if [ ! -e $backup_file ]; then
echo "FAIL - sql file to restore not found in ${backup_target}"
exit 1
else
echo "Start restore from backup ${backup_file}"
fi
# clean up
echo "# Clean up old database"
sudo -u postgres psql -c "drop database $db_name;" || exit 1
sudo -u postgres psql -c "drop user $db_user;"
# create database and user
echo "# Create fresh database"
sudo -u postgres psql -c "create database $db_name;"
sudo -u postgres psql -c "create user $db_user with encrypted password '$db_user_pw';"
sudo -u postgres psql -c "grant all privileges on database $db_name to $db_user;"
# restore dump
echo "# Import SQL Dump"
sudo mkdir -p $backup_target/logs 1>&2
sudo chown -R postgres:postgres $backup_file
sudo -u postgres psql $db_name < ${backup_file} > $backup_target/logs/sql_import.log || exit 1
echo "$backup_target/sql_import.log written"
echo "OK - database $db_name restored from ${backup_file}"
exit 0
fi
if [ "$command" = "info" ]; then
check=$(sudo -u postgres psql -c "show data_directory;" | grep data_directory)
if [ "$check" = "" ]; then
echo "show data_directory failed, PostgreSQL not installed?!"
exit 1
else
sudo -u postgres psql -c "show data_directory;"
sudo -u postgres psql -c "SELECT datname FROM pg_database;"
fi
exit 0
fi
echo "FAIL - Unknown Parameter $command"
exit 1