2018-09-05 16:39:07 +02:00
#!/bin/bash
echo ""
2018-07-29 12:21:52 +02:00
2018-11-18 12:10:25 +01:00
# see background_downloadBlockchain.md for info
# why there are two torrent files
2018-07-29 12:21:52 +02:00
2018-12-06 14:36:02 +01:00
## get basic info
2019-02-13 02:33:27 +01:00
source /home/admin/raspiblitz.info
2018-07-29 12:21:52 +02:00
2019-03-17 16:29:59 +01:00
# if setup was done - remove old data
if [ " ${ setupStep } " = "100" ] ; then
2019-03-28 10:49:22 +08:00
echo "stopping services ..."
2019-03-17 16:29:59 +01:00
sudo systemctl stop lnd
sudo systemctl stop ${ network } d
fi
2018-09-14 23:46:12 +02:00
# make sure rtorrent is available
sudo apt-get install rtorrent -y
2018-09-06 19:49:54 +02:00
echo ""
2018-11-18 12:10:25 +01:00
echo ""
echo "*** Torrent Files ***"
# torrent files that are available
# in directory /home.admin/assets/
# WITHOUT THE '.torrent' ENDING
bitcoinBase = "raspiblitz-bitcoin1-2018-10-13-base"
2019-01-16 13:23:54 +01:00
bitcoinUpdate = "raspiblitz-bitcoin1-2019-01-16-update"
2018-11-18 12:10:25 +01:00
2018-11-18 14:06:26 +01:00
litecoinBase = "raspiblitz-litecoin1-2018-11-18-base"
litecoinUpdate = "raspiblitz-litecoin1-2018-11-18-update"
2018-11-18 12:10:25 +01:00
# set final based on selected network
2018-11-19 00:57:38 +01:00
baseTorrentFile = ${ bitcoinBase }
updateTorrentFile = ${ bitcoinUpdate }
2018-07-29 12:21:52 +02:00
if [ " $network " = "litecoin" ] ; then
2018-11-20 17:53:21 +01:00
baseTorrentFile = ${ litecoinBase }
updateTorrentFile = ${ litecoinUpdate }
2018-07-29 21:43:16 +02:00
fi
2018-11-18 12:10:25 +01:00
echo " base : ${ baseTorrentFile } "
echo " update : ${ updateTorrentFile } "
sleep 1
2018-07-29 12:21:52 +02:00
2018-09-06 17:22:04 +02:00
targetDir = "/mnt/hdd/torrent"
2018-11-18 12:10:25 +01:00
sessionDir = "/home/admin/.rtorrent.session"
sudo mkdir ${ sessionDir } 2>/dev/null
##############################
# CHECK TORRENT 1 "BLOCKCHAIN"
##############################
echo "*** checking torrent 1: base blockchain"
torrentComplete1 = $( cat ${ sessionDir } /blockchain/*.torrent.rtorrent | grep ':completei1' -c)
echo " torrentComplete1( ${ torrentComplete1 } ) "
if [ ${ torrentComplete1 } -eq 0 ] ; then
# check if screen session for this torrent
isRunning1 = $( screen -S blockchain -ls | grep "blockchain" -c )
echo " isRunning1( ${ isRunning1 } ) "
if [ ${ isRunning1 } -eq 0 ] ; then
# start torrent download in screen session
echo "starting torrent: blockchain"
command1 = " sudo rtorrent -n -d ${ targetDir } -s ${ sessionDir } /blockchain/ /home/admin/assets/ ${ baseTorrentFile } .torrent "
sudo mkdir ${ targetDir } 2>/dev/null
sudo mkdir ${ sessionDir } /blockchain/ 2>/dev/null
screenCommand = " screen -S blockchain -L screen.log -dm ${ command1 } "
echo " ${ screenCommand } "
bash -c " ${ screenCommand } "
fi
2018-07-29 12:21:52 +02:00
fi
2018-11-18 12:10:25 +01:00
sleep 2
##############################
# CHECK TORRENT 2 "UPDATE"
##############################
echo "*** checking torrent 2: update blockchain"
torrentComplete2 = $( cat ${ sessionDir } /update/*.torrent.rtorrent | grep ':completei1' -c)
echo " torrentComplete2( ${ torrentComplete2 } ) "
if [ ${ torrentComplete2 } -eq 0 ] ; then
# check if screen session for this torrent
isRunning2 = $( screen -S update -ls | grep "update" -c )
echo " isRunning2( ${ isRunning2 } ) "
if [ ${ isRunning2 } -eq 0 ] ; then
# start torrent download in screen session
echo "starting torrent: update"
command2 = " sudo rtorrent -n -d ${ targetDir } -s ${ sessionDir } /update/ /home/admin/assets/ ${ updateTorrentFile } .torrent "
sudo mkdir ${ targetDir } 2>/dev/null
sudo mkdir ${ sessionDir } /update/ 2>/dev/null
screenCommand = " screen -S update -L screen.log -dm ${ command2 } "
echo " ${ screenCommand } "
bash -c " ${ screenCommand } "
fi
fi
sleep 2
##############################
# MONITOR PROGRESS
##############################
2018-09-06 17:06:59 +02:00
sleep 3
2018-09-14 23:46:12 +02:00
# monitor screen session
2018-11-18 12:10:25 +01:00
screenDump1 = "... started ..."
screenDump2 = "... started ..."
torrentComplete1 = 0
torrentComplete2 = 0
2018-09-06 17:06:59 +02:00
while :
do
# display info screen
clear
echo "****************************************************"
2018-11-18 12:10:25 +01:00
echo "Monitoring Screen Session: Torrent base+update"
2018-09-06 17:06:59 +02:00
echo "NOTICE: This can take multiple hours or days !!"
echo "Its OK to close terminal now and SSH back in later."
2018-11-18 12:10:25 +01:00
echo "If u see the torrents 100% downloaded and verified,"
echo "press X to continue. Also press x to abort download"
echo "before 100% if you want to switch to another option."
2018-09-06 17:06:59 +02:00
echo "****************************************************"
2018-11-18 12:10:25 +01:00
echo ""
# display torrent 1 info
echo "*** 1) Status Torrent 'blockchain':"
torrentComplete1 = $( cat ${ sessionDir } /blockchain/*.torrent.rtorrent | grep ':completei1' -c)
if [ ${ torrentComplete1 } -eq 0 ] ; then
screen -S blockchain -X hardcopy .blockchain.out
newScreenDump = $( cat .blockchain.out | head -6 | tail -3 )
if [ ${# newScreenDump } -gt 0 ] ; then
screenDump1 = $newScreenDump
fi
echo " $screenDump1 "
else
echo "Completed"
fi
echo ""
# display torrent 2 info
echo "*** 2) Status Torrent 'update':"
torrentComplete2 = $( cat ${ sessionDir } /update/*.torrent.rtorrent | grep ':completei1' -c)
if [ ${ torrentComplete2 } -eq 0 ] ; then
screen -S update -X hardcopy .update.out
newScreenDump = $( cat .update.out| head -6 | tail -3 )
if [ ${# newScreenDump } -gt 0 ] ; then
screenDump2 = $newScreenDump
fi
echo " $screenDump2 "
else
echo "Completed"
fi
echo ""
# check if both torrents completed
if [ ${ torrentComplete1 } -eq 1 ] ; then
if [ ${ torrentComplete2 } -eq 1 ] ; then
echo "OK - all torrents finished"
break
fi
2018-09-06 17:06:59 +02:00
fi
2018-09-05 16:39:07 +02:00
2018-09-06 17:06:59 +02:00
# wait 2 seconds for key input
read -n 1 -t 2 keyPressed
2018-09-05 16:39:07 +02:00
2018-09-06 17:06:59 +02:00
# check if user wants to abort session
if [ " ${ keyPressed } " = "x" ] ; then
echo ""
2018-11-18 12:10:25 +01:00
echo "Aborting"
2018-09-06 17:06:59 +02:00
break
fi
2018-09-05 16:39:07 +02:00
2018-09-06 17:06:59 +02:00
done
# clean up
2018-11-18 12:10:25 +01:00
rm -f .blockchain.out
rm -f .update.out
2018-09-06 17:06:59 +02:00
2018-11-18 12:10:25 +01:00
##############################
# AFTER PARTY & CLEAN UP
##############################
# quit session1
isRunning = $( screen -S blockchain -ls | grep "blockchain" -c )
2018-09-06 17:06:59 +02:00
if [ ${ isRunning } -eq 1 ] ; then
# get the PID of screen session
2018-11-18 12:10:25 +01:00
sessionPID = $( screen -ls | grep "blockchain" | cut -d "." -f1 | xargs)
2018-09-06 17:06:59 +02:00
echo " killing screen session PID( ${ sessionPID } ) "
# kill all child processes of screen sceesion
2018-09-06 18:44:48 +02:00
sudo pkill -P ${ sessionPID }
2018-09-06 19:48:39 +02:00
echo "proccesses killed"
2018-09-06 17:06:59 +02:00
sleep 3
# tell the screen session to quit and wait a bit
2018-11-18 12:10:25 +01:00
screen -S blockchain -X quit 1>/dev/null
2018-09-06 17:06:59 +02:00
sleep 3
echo "cleaning screen"
screen -wipe 1>/dev/null
sleep 3
fi
2018-11-18 12:10:25 +01:00
# quit session2
isRunning = $( screen -S update -ls | grep "update" -c )
if [ ${ isRunning } -eq 1 ] ; then
# get the PID of screen session
sessionPID = $( screen -ls | grep "update" | cut -d "." -f1 | xargs)
echo " killing screen session PID( ${ sessionPID } ) "
# kill all child processes of screen sceesion
sudo pkill -P ${ sessionPID }
echo "proccesses killed"
sleep 3
# tell the screen session to quit and wait a bit
screen -S update -X quit 1>/dev/null
sleep 3
echo "cleaning screen"
screen -wipe 1>/dev/null
sleep 3
fi
2018-09-05 16:39:07 +02:00
2018-11-18 12:10:25 +01:00
# check torrent success
echo ""
echo "*** Torrent Data Check ***"
torrentError = 0
torrentComplete1 = $( cat ${ sessionDir } /blockchain/*.torrent.rtorrent | grep ':completei1' -c)
torrentComplete2 = $( cat ${ sessionDir } /update/*.torrent.rtorrent | grep ':completei1' -c)
if [ ${ torrentComplete1 } -eq 0 ] ; then
torrentError = 1
fi
if [ ${ torrentComplete2 } -eq 0 ] ; then
torrentError = 2
2018-09-06 17:06:59 +02:00
fi
2018-11-18 12:10:25 +01:00
# the path torrent was download to
targetPath1 = " ${ targetDir } / ${ baseTorrentFile } "
targetPath2 = " ${ targetDir } / ${ updateTorrentFile } "
# check that path exists
contentPath1 = $( sudo ls ${ targetPath1 } 2>/dev/null)
2019-01-16 19:16:47 +01:00
contentPath2 = $( sudo ls ${ targetPath2 } 2>/dev/null)
2018-11-18 12:10:25 +01:00
if [ ${# contentPath1 } -eq 0 ] ; then
torrentError = 3
fi
if [ ${# contentPath2 } -eq 0 ] ; then
torrentError = 4
fi
if [ ${ torrentError } -gt 0 ] ; then
2018-09-05 16:39:07 +02:00
2018-11-18 12:10:25 +01:00
# User Cancel --> Torrent incomplete
2018-09-06 17:06:59 +02:00
sleep 3
echo -ne '\007'
2019-03-14 13:18:04 +01:00
dialog --title " WARNING ( ${ torrentError } ) " --yesno "The Torrent download failed or is not complete - maybe try COPY option. Do you want keep already downloaded torrent data?" 8 57
2018-09-06 17:06:59 +02:00
response = $?
case $response in
2018-11-19 22:08:28 +01:00
1) sudo rm -rf ${ targetDir } ; sudo rm -rf ${ sessionDir } ; ;
2018-09-06 17:06:59 +02:00
esac
./00mainMenu.sh
exit 1;
2018-11-18 12:10:25 +01:00
fi
2018-09-06 17:06:59 +02:00
2019-02-16 11:01:53 +01:00
# if setup was done - remove old data
if [ " ${ setupStep } " = "100" ] ; then
echo "stopping servcies ..."
sudo systemctl stop lnd
sudo systemctl stop ${ network } d
sudo systemctl disable ${ network } d
sudo cp -f /mnt/hdd/${ network } /${ network } .conf /home/admin/assets/${ network } .conf
sudo rm -rfv /mnt/hdd/${ network } /* 2>/dev/null
2019-03-17 03:41:32 +01:00
sudo rm /mnt/hdd/${ network } /debug.log
2019-02-16 11:01:53 +01:00
fi
2019-03-17 03:41:32 +01:00
# Download worked / just move, copy on USB2 would be >4h
2018-11-18 12:10:25 +01:00
echo ""
echo "*** Moving Files ***"
date +%s
2019-02-17 00:22:42 +01:00
echo "can take 10-60 minutes... please wait"
2018-11-18 12:10:25 +01:00
sudo mkdir /mnt/hdd/${ network } 2>/dev/null
sudo mv ${ targetPath1 } /* /mnt/hdd/${ network } /
sudo cp -r ${ targetPath2 } /* /mnt/hdd/${ network } /
sudo rm -r ${ targetDir }
echo "OK"
date +%s
2018-09-06 17:06:59 +02:00
2019-02-16 11:01:53 +01:00
if [ " ${ setupStep } " = "100" ] ; then
sudo cp /home/admin/assets/${ network } .conf /mnt/hdd/${ network } /${ network } .conf
2019-03-18 00:46:15 +01:00
rpcpass = $( sudo cat /mnt/hdd/lnd/lnd.conf | grep " ${ network } d.rpcpass " | cut -d "=" -f2)
sudo sed -i " s/^rpcpassword=.*/rpcpassword= ${ rpcpass } /g " /mnt/hdd/${ network } /${ network } .conf 2>/dev/null
2019-02-16 11:01:53 +01:00
sudo chown -R bitcoin:bitcoin /mnt/hdd/${ network } /
sudo systemctl enable ${ network } d
2019-03-18 00:18:30 +01:00
echo "DONE - rebooting: sudo shutdown -r now"
sudo shutdown -r now
2019-02-16 11:01:53 +01:00
else
2019-02-14 16:39:01 +01:00
# set SetupState
sudo sed -i "s/^setupStep=.*/setupStep=50/g" /home/admin/raspiblitz.info
# continue setup
./60finishHDD.sh
2019-03-28 10:49:22 +08:00
fi