2018-09-05 16:39:07 +02:00
#!/bin/bash
echo ""
2018-07-29 12:21:52 +02:00
2018-09-16 23:07:04 +02:00
# --> TODO: Check https://getbitcoinblockchain.com/
2018-07-29 12:21:52 +02:00
# *** BITCOIN Torrent ***
2018-10-08 00:05:00 +02:00
bitcoinTorrent = "raspiblitz-bitcoin-2018-10-06"
bitcoinTorrentsize = 259000000
2018-07-29 12:21:52 +02:00
# *** LITECOIN Torrent ***
2018-07-29 21:43:16 +02:00
litecoinTorrent = "raspiblitz-litecoin-2018-07-29"
2018-09-06 17:06:59 +02:00
litecoinTorrentsize = 10240000
2018-07-29 12:21:52 +02:00
# load network
network = ` cat .network`
2018-09-21 21:29:14 +02:00
## experimental redirect if bitcoin
2018-10-13 16:59:31 +02:00
if [ " $network " = "bitcoin" ] ; then
./50torrentHDD.bitcoin.sh
exit 1
fi
2018-09-16 23:07:04 +02:00
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-07-29 12:21:52 +02:00
# settings based on network
torrent = $bitcoinTorrent
2018-09-06 17:06:59 +02:00
size = $bitcoinTorrentsize
2018-07-29 12:21:52 +02:00
if [ " $network " = "litecoin" ] ; then
torrent = $litecoinTorrent
2018-09-06 17:06:59 +02:00
size = $litecoinTorrentsize
2018-07-29 21:43:16 +02:00
fi
2018-07-29 12:21:52 +02:00
2018-09-06 17:06:59 +02:00
# screen background monitoring settings
2018-09-06 18:44:48 +02:00
name = "Torrent"
2018-09-06 17:22:04 +02:00
targetDir = "/mnt/hdd/torrent"
2018-09-06 17:06:59 +02:00
targetSize = $size
2018-09-14 23:46:12 +02:00
sessionDir = "/home/admin/.rtorrent.session/"
2018-09-15 10:15:31 +02:00
command = " sudo rtorrent -n -d ${ targetDir } -s ${ sessionDir } /home/admin/assets/ ${ torrent } .torrent "
2018-09-16 23:07:04 +02:00
# 2 screen sessions - differnt rtorrent session dir?
#sudo rtorrent -n -d /mnt/hdd/torrent -s /home/admin/.rtorrent.session/ https://getbitcoinblockchain.com/blockchain.torrent
#sudo rtorrent -n -d /mnt/hdd/torrent -s /home/admin/.rtorrent.session/ https://getbitcoinblockchain.com/update.torrent
2018-07-29 12:21:52 +02:00
2018-09-14 23:46:12 +02:00
# starting screen session if needed
2018-09-06 17:06:59 +02:00
echo " checking if ${ name } has a running screen session "
screen -wipe 1>/dev/null
isRunning = $( screen -S ${ name } -ls | grep " ${ name } " -c )
echo " isRunning( ${ isRunning } ) "
if [ ${ isRunning } -eq 0 ] ; then
echo "Starting screen session"
2018-09-05 16:39:07 +02:00
sudo mkdir ${ targetDir } 2>/dev/null
2018-09-14 23:46:12 +02:00
sudo mkdir ${ sessionDir } 2>/dev/null
2018-09-06 18:44:48 +02:00
screenCommand = " screen -S ${ name } -L screen.log -dm ${ command } "
echo " ${ screenCommand } "
bash -c " ${ screenCommand } "
2018-09-05 16:39:07 +02:00
else
2018-09-06 17:06:59 +02:00
echo "Continue screen session"
2018-07-29 12:21:52 +02:00
fi
2018-09-06 17:06:59 +02:00
sleep 3
2018-09-14 23:46:12 +02:00
# monitor screen session
2018-09-06 17:06:59 +02:00
screenDump = "... started ..."
actualSize = 0
2018-09-14 23:46:12 +02:00
torrentComplete = 0
2018-09-06 17:06:59 +02:00
while :
do
2018-09-14 23:46:12 +02:00
# check if completed by inspecting rtorrent session files
2018-09-15 10:22:37 +02:00
torrentComplete = $( cat /home/admin/.rtorrent.session/*.torrent.rtorrent | grep ':completei1' -c)
2018-09-14 23:46:12 +02:00
if [ ${ torrentComplete } -eq 1 ] ; then
echo "OK - torrent finished"
2018-09-06 17:06:59 +02:00
break
fi
# calculate progress and write it to file for LCD to read
freshSize = $( du -s ${ targetDir } | head -n1 | awk '{print $1;}' )
if [ ${# actualSize } -eq 0 ] ; then
freshSize = 0
fi
progress = $( echo " scale=2; $freshSize *100/ $targetSize " | bc)
2018-09-14 11:36:46 +02:00
echo $progress > " . ${ name } .progress "
2018-09-06 17:06:59 +02:00
actualSize = $freshSize
# display info screen
clear
echo "****************************************************"
echo " Monitoring Screen Session: ${ name } "
echo " Progress: ${ progress } % ( ${ actualSize } of ${ targetSize } ) "
echo " If needed press key x to stop ${ name } "
echo "NOTICE: This can take multiple hours or days !!"
echo "Its OK to close terminal now and SSH back in later."
echo "****************************************************"
screen -S ${ name } -X hardcopy .${ name } .out
2018-09-06 18:02:22 +02:00
newScreenDump = $( cat .${ name } .out | grep . | tail -8)
2018-09-06 17:06:59 +02:00
if [ ${# newScreenDump } -gt 0 ] ; then
screenDump = $newScreenDump
fi
echo " $screenDump "
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 ""
echo " Aborting ${ name } "
break
fi
2018-09-05 16:39:07 +02:00
2018-09-06 17:06:59 +02:00
done
# clean up
rm -f .${ name } .out
rm -f .${ name } .progress
# quit session if still running
2018-09-16 23:07:04 +02:00
isRunning = $( screen -S ${ name } -ls | grep " ${ name } " -c )
2018-09-06 17:06:59 +02:00
if [ ${ isRunning } -eq 1 ] ; then
# get the PID of screen session
sessionPID = $( screen -ls | grep " ${ name } " | cut -d "." -f1 | xargs)
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
screen -S ${ name } -X quit 1>/dev/null
sleep 3
echo "cleaning screen"
screen -wipe 1>/dev/null
sleep 3
fi
# the path torrent will download to
2018-09-06 17:22:04 +02:00
targetPath = " ${ targetDir } / ${ torrent } "
2018-09-06 17:06:59 +02:00
echo " path to downloaded data is ${ targetPath } "
2018-09-05 16:39:07 +02:00
# calculate progress and write it to file for LCD to read
2018-09-06 17:06:59 +02:00
finalSize = $( du -s ${ targetDir } 2>/dev/null | head -n1 | awk '{print $1;}' )
if [ ${# finalSize } -eq 0 ] ; then
finalSize = 0
fi
echo " final size is ${ finalSize } of targeted size ${ targetSize } "
2018-09-05 16:39:07 +02:00
# check result
2018-09-06 17:06:59 +02:00
if [ ${ finalSize } -lt ${ targetSize } ] ; then
2018-09-05 16:39:07 +02:00
# Download failed
2018-09-06 17:06:59 +02:00
sleep 3
echo -ne '\007'
dialog --title " WARNING " --yesno "The download failed or is not complete. Maybe try again (later). Do you want keep already downloaded data for next try?" 8 57
response = $?
case $response in
1) sudo rm -rf ${ targetDir } ; ;
esac
./00mainMenu.sh
exit 1;
else
2018-09-15 16:01:55 +02:00
# Download worked / just move, copy on USB2 >4h
2018-09-15 16:00:41 +02:00
echo "*** Moving Files ***"
2018-09-15 10:18:06 +02:00
echo "START"
date +%s
2018-09-15 16:00:41 +02:00
sudo mv ${ targetPath } /mnt/hdd/${ network }
2018-09-06 17:06:59 +02:00
echo "OK"
2018-09-15 10:18:06 +02:00
date +%s
2018-07-29 12:21:52 +02:00
2018-09-05 16:39:07 +02:00
# continue setup
2018-09-06 17:06:59 +02:00
./60finishHDD.sh
fi