raspiblitz/home.admin/XXcopyStation.sh

319 lines
10 KiB
Bash
Raw Normal View History

2019-05-18 21:25:46 +02:00
#!/bin/bash
# command info
2019-05-18 21:58:30 +02:00
if [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
2019-05-18 21:25:46 +02:00
echo "# Turns the RaspiBlitz into HDD CopyStation Mode"
echo "# lightning is deactivated during CopyStationMode"
echo "# reboot RaspiBlitz to set back to normal mode"
exit 1
fi
####### CONFIG #############
# where to find the BITCOIN data directory (no trailing /)
pathBitcoinBlockchain="/mnt/hdd/bitcoin"
# where to find the LITECOIN data directory (no trailing /)
pathLitecoinBlockchain="/mnt/hdd/litecoin"
# where to find the RaspiBlitz HDD template directory (no trailing /)
2020-02-24 18:38:57 +01:00
pathTemplateHDD="/mnt/hdd/app-storage/templateHDD"
2019-05-18 21:25:46 +02:00
# 0 = ask before formatting/init new HDD
# 1 = auto-formatting every new HDD that needs init
2019-05-18 23:19:10 +02:00
nointeraction=1
2019-05-18 21:25:46 +02:00
# override values if XXcopyStation.conf files exists
# use when you run this outside RaspiBlitz
2019-05-20 00:55:25 +02:00
# - clean Ubuntu install
# - install bitcoind as systemd service
# - disable automount: https://askubuntu.com/questions/89244/how-to-disable-automount-in-nautiluss-preferences#102601
# - clone the github to get script (or download)
# - set your pathes bitcoin/template in conf file
2019-05-18 21:25:46 +02:00
source ./XXcopyStation.conf 2>/dev/null
2019-05-20 00:55:25 +02:00
# -- start script with parameter "-foreground"
2019-05-18 21:25:46 +02:00
####### SCRIPT #############
# check sudo
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (with sudo)"
exit 1
fi
2020-02-24 18:40:25 +01:00
# get HDD info
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status)
# check if HDD is mounted
if [ ${isMounted} -eq 0 ]; then
2020-02-24 18:43:03 +01:00
echo "error='HDD is not mounted'"
2020-02-24 18:40:25 +01:00
exit 1
fi
# check if HDD is big enough
if [ ${hddGigaBytes} -lt 800 ]; then
2020-02-24 18:43:03 +01:00
echo "# To run copy station (+/- 1TB needed)"
echo "error='HDD is too small'"
2020-02-24 18:40:25 +01:00
exit 1
fi
2020-02-24 18:43:03 +01:00
# check that path information is valid
if [ -d "$pathBitcoinBlockchain" ]; then
echo "# OK found $pathBitcoinBlockchain"
else
echo "# FAIL path of 'pathBitcoinBlockchain' does not exists: ${pathBitcoinBlockchain}"
echo "error='pathBitcoinBlockchain nit found'"
exit 1
fi
2020-02-24 18:40:25 +01:00
2019-05-18 21:25:46 +02:00
# make sure that its running in screen
# call with '-foreground' to prevent running in screen
if [ "$1" != "-foreground" ]; then
screenPID=$(screen -ls | grep "copystation" | cut -d "." -f1 | xargs)
if [ ${#screenPID} -eq 0 ]; then
# start copystation in sreen
2020-02-24 23:09:42 +01:00
echo "# starting copystation screen session"
screen -S copystation -dm /home/admin/XXcopyStation.sh -foreground
screen -d -r
2019-05-18 23:37:34 +02:00
exit 0
2019-05-18 21:25:46 +02:00
else
2020-02-24 23:09:42 +01:00
echo "# changing into running copystation screen session"
2019-05-19 01:46:52 +02:00
screen -d -r
2019-05-18 23:37:34 +02:00
exit 0
2019-05-18 21:25:46 +02:00
fi
fi
clear
2020-02-24 23:09:42 +01:00
echo "# ******************************"
echo "# RASPIBLITZ COPYSTATION SCRIPT"
echo "# ******************************"
2019-05-18 21:25:46 +02:00
echo
echo "*** CHECKING CONFIG"
# check that path information is valid
if [ -d "$pathTemplateHDD" ]; then
2020-02-24 23:09:42 +01:00
echo "# OK found $pathTemplateHDD"
2019-05-18 21:25:46 +02:00
else
2020-02-24 23:09:42 +01:00
echo "# Creating: ${pathTemplateHDD}"
2019-05-18 21:25:46 +02:00
mkdir ${pathTemplateHDD}
chmod 777 ${pathTemplateHDD}
fi
# make sure that lnd is stopped (if runnning)
systemctl stop lnd 2>/dev/null
systemctl stop background 2>/dev/null
2020-02-24 23:09:42 +01:00
#if [ "${nointeraction}" == "1" ]; then
# echo "setting RaspiBlitz LCD info"
# sudo sed -i "s/^state=.*/state=copystation/g" /home/admin/raspiblitz.info 2>/dev/null
# sudo sed -i "s/^message=.*/message='Disconnect target HDDs!'/g" /home/admin/raspiblitz.info 2>/dev/null
# echo "Disconnect target HDDs! .. 30sec until continue."
# sleep 30
#else
# echo
# echo "*** INIT HDD SCAN"
# echo "Please make sure that no HDDs that you want to sync later to are not connected now."
# echo "PRESS ENTER when ready."
# read key
#fi
2019-05-18 21:25:46 +02:00
# finding system drives (the drives that should not be synced to)
2020-02-24 23:09:42 +01:00
echo "# OK - the following drives detected as the system drive: $datadisk"
2019-05-18 21:25:46 +02:00
echo
2019-05-18 23:19:10 +02:00
if [ "${nointeraction}" == "1" ]; then
2020-02-24 23:09:42 +01:00
sudo sed -i "s/^message=.*/message='Connect target HDDs ..'/g" /home/admin/raspiblitz.info 2>/dev/null
2019-05-18 21:25:46 +02:00
sleep 5
fi
# BASIC IDEA:
# 1. get fresh data from bitcoind --> template data
# 2. detect HDDs
# 3. sync HDDs with template data
# repeat
echo
echo "*** RUNNING ***"
lastBlockchainUpdateTimestamp=1
while :
do
################################################
# 1. get fresh data from bitcoind for template data
# only execute every 30min
nowTimestamp=$(date +%s)
secondsDiff=$(echo "${nowTimestamp}-${lastBlockchainUpdateTimestamp}" | bc)
2020-02-24 23:09:42 +01:00
echo "# seconds since last update from bitcoind: ${secondsDiff}"
2019-05-18 21:25:46 +02:00
echo
if [ ${secondsDiff} -gt 3000 ]; then
echo "******************************"
echo "Bitcoin Blockchain Update"
echo "******************************"
# stop blockchains
2020-02-24 23:09:42 +01:00
echo "# Stopping Blockchain ..."
2019-05-18 21:25:46 +02:00
systemctl stop bitcoind 2>/dev/null
systemctl stop litecoind 2>/dev/null
sleep 10
# sync bitcoin
2020-02-24 23:09:42 +01:00
echo "# Syncing Bitcoin to template folder ..."
2019-05-18 21:25:46 +02:00
2019-05-19 02:20:32 +02:00
sudo sed -i "s/^message=.*/message='Updating Template: Bitcoin'/g" /home/admin/raspiblitz.info 2>/dev/null
2019-05-18 23:19:10 +02:00
2019-05-18 21:25:46 +02:00
# make sure the bitcoin directory in template folder exists
if [ ! -d "$pathTemplateHDD/bitcoin" ]; then
2020-02-24 23:09:42 +01:00
echo "# creating the bitcoin subfolder in the template folder"
2019-05-18 21:25:46 +02:00
mkdir ${pathTemplateHDD}/bitcoin
chmod 777 ${pathTemplateHDD}/bitcoin
fi
2020-02-24 23:09:42 +01:00
# do the sync to the template folder for BITCOIN
2020-02-28 21:40:05 +01:00
rsync -a --info=progress2 --delete ${pathBitcoinBlockchain}/chainstate ${pathBitcoinBlockchain}/blocks ${pathTemplateHDD}/bitcoin
2019-05-18 21:25:46 +02:00
2020-02-28 21:48:30 +01:00
litecoindirsize=$(sudo du -s -b /mnt/hdd/litecoin | awk '$1=$1' | cut -d " " -f1)
2020-02-28 21:40:05 +01:00
if [ -d "${pathLitecoinBlockchain}" ] && [ ${litecoindirsize} -gt 1000000000 ]; then
2019-05-18 21:25:46 +02:00
2019-05-19 02:25:06 +02:00
# sync litecoin
2020-02-24 23:09:42 +01:00
echo "# Syncing Litecoin ..."
2019-05-18 21:25:46 +02:00
2020-02-24 23:09:42 +01:00
echo "# creating the litecoin subfolder in the template folder"
2019-05-19 02:25:06 +02:00
mkdir ${pathTemplateHDD}/litecoin 2>/dev/null
chmod 777 ${pathTemplateHDD}/litecoin 2>/dev/null
2019-05-18 23:19:10 +02:00
2019-05-19 02:25:06 +02:00
sudo sed -i "s/^message=.*/message='Updating Template: Litecoin'/g" /home/admin/raspiblitz.info 2>/dev/null
2019-05-18 21:25:46 +02:00
2020-02-24 23:09:42 +01:00
# do the sync to the template folder for LITECOIN
2020-02-28 21:40:05 +01:00
rsync -a --info=progress2 --delete ${pathLitecoinBlockchain}/chainstate ${pathLitecoinBlockchain}/blocks ${pathTemplateHDD}/litecoin
2019-05-18 21:25:46 +02:00
fi
# restart bitcoind (to let further setup while syncing HDDs)
2020-02-24 23:09:42 +01:00
echo "# Restarting Blockchain ..."
2019-05-18 21:25:46 +02:00
systemctl start bitcoind 2>/dev/null
systemctl start litecoind 2>/dev/null
# update timer
lastBlockchainUpdateTimestamp=$(date +%s)
fi
################################################
# 2. detect connected HDDs and loop thru them
2020-02-28 23:07:44 +01:00
echo
echo "**************************************"
2020-02-28 23:13:18 +01:00
echo "SYNCING TEMPLATE -> CONNECTED HDD/SSDs"
2020-02-28 23:07:44 +01:00
echo "**************************************"
2020-03-05 01:21:15 +01:00
echo "NOTE: Only use to prepare fresh HDDs"
2020-02-28 23:07:44 +01:00
2019-05-18 21:25:46 +02:00
sleep 4
echo "" > ./.syncinfo.tmp
lsblk -o NAME | grep "^sd" | while read -r detectedDrive ; do
2020-02-24 23:09:42 +01:00
isSystemDrive=$(echo "${datadisk}" | grep -c "${detectedDrive}")
2019-05-18 21:25:46 +02:00
if [ ${isSystemDrive} -eq 0 ]; then
# check if drives 1st partition is named BLOCKCHAIN & in EXT4 format
isNamedBlockchain=$(lsblk -o NAME,FSTYPE,LABEL | grep "${detectedDrive}" | grep -c "BLOCKCHAIN")
isFormatExt4=$(lsblk -o NAME,FSTYPE,LABEL | grep "${detectedDrive}" | grep -c "ext4")
# init a fresh device
if [ ${isNamedBlockchain} -eq 0 ] || [ ${isFormatExt4} -eq 0 ]; then
echo "*** NEW EMPTY HDD FOUND ***"
echo "Device: ${detectedDrive}"
echo "isNamedBlockchain: ${isNamedBlockchain}"
echo "isFormatExt4:" ${isFormatExt4}
# check if size is OK
size=$(lsblk -o NAME,SIZE -b | grep "^${detectedDrive}" | awk '$1=$1' | cut -d " " -f 2)
echo "size: ${size}"
if [ ${size} -lt 250000000000 ]; then
whiptail --title "FAIL" --msgbox "
THE DEVICE IS TOO SMALL <250GB
Please remove device and PRESS ENTER
" 9 46
else
2020-02-24 23:09:42 +01:00
# if config value "nointeraction=1" default to format
if [ "${nointeraction}" != "1" ]; then
whiptail --title "Format HDD" --yes-button "Format" --no-button "Cancel" --yesno "
Found new HDD. Do you want to FORMAT now?
Lable of device with: ${detectedDrive}
" 10 54
choice=$?
2019-05-18 21:25:46 +02:00
else
2020-02-24 23:09:42 +01:00
choice=0
sudo sed -i "s/^message=.*/message='Formatting new HDD: ${formatPartition}'/g" /home/admin/raspiblitz.info 2>/dev/null
fi
2019-05-18 21:25:46 +02:00
2020-02-24 23:09:42 +01:00
# on cancel
if [ "${choice}" != "0" ]; then
whiptail --title "Format HDD" --msgbox "
2019-05-18 21:25:46 +02:00
OK NO FORMAT - Please remove decive now.
2020-02-24 23:09:42 +01:00
" 8 46
exit 1
2019-05-18 21:25:46 +02:00
fi
2020-02-24 23:09:42 +01:00
# format the HDD
echo "Starting Formatting of device ${detectedDrive} ..."
sudo /home/admin/config.scripts/blitz.datadrive.sh format ext4 ${detectedDrive}
2019-05-18 21:25:46 +02:00
fi
fi # end init new HDD
################################################
2020-02-24 23:09:42 +01:00
# 3. sync HDD with template data
2019-05-18 21:25:46 +02:00
partition=$(lsblk -o NAME,FSTYPE,LABEL | grep "${detectedDrive}" | grep "BLOCKCHAIN" | cut -d ' ' -f 1 | tr -cd "[:alnum:]")
if [ ${#partition} -gt 0 ]; then
# temp mount device
echo "mounting: ${partition}"
mkdir /mnt/hdd2 2>/dev/null
sudo mount -t ext4 /dev/${partition} /mnt/hdd2
# rsync device
mountOK=$(lsblk -o NAME,MOUNTPOINT | grep "${detectedDrive}" | grep -c "/mnt/hdd2")
if [ ${mountOK} -eq 1 ]; then
2020-02-24 23:09:42 +01:00
sudo sed -i "s/^message=.*/message='Syncing Template -> ${partition}'/g" /home/admin/raspiblitz.info 2>/dev/null
2020-02-28 21:40:05 +01:00
rsync -a --info=progress2 --delete ${pathTemplateHDD}/* /mnt/hdd2
2020-02-28 21:22:41 +01:00
sudo chmod -R 777 /mnt/hdd2
2019-05-20 01:05:57 +02:00
rm -r /mnt/hdd2/lost+found 2>/dev/null
2019-05-18 21:25:46 +02:00
echo "${partition} " >> ./.syncinfo.tmp
else
2020-02-24 23:09:42 +01:00
echo "# FAIL: was not able to mount --> ${partition}"
2019-05-18 21:25:46 +02:00
fi
# unmount device
sudo umount -l /mnt/hdd2
fi
fi
done
clear
echo "**** SYNC LOOP DONE ****"
synced=$(cat ./.syncinfo.tmp | tr '\r\n' ' ')
echo "HDDs ready synced: ${synced}"
echo "*************************"
echo "Its safe to disconnect/remove HDDs now."
2020-02-29 09:31:59 +01:00
echo "Or connect a new HDD/SSD for syncing."
2019-05-18 21:25:46 +02:00
echo "To stop copystation script: CTRL+c"
echo ""
2019-05-20 15:21:24 +02:00
sudo sed -i "s/^message=.*/message='Ready HDDs: ${synced}'/g" /home/admin/raspiblitz.info 2>/dev/null
2019-05-18 21:25:46 +02:00
sleep 25
clear
echo "starting new sync loop"
sleep 5
done